Fast polynomial and rational evaluation library
Project description
Rust Poly
Fast polynomial and rational evaluation for Python written in Rust.
Quick example of computing a stiffness matrix:
from rust_poly import ExpressionTree as Polynomial, from_terms
import numpy as np
terms = {"x1": 0, "x2": 1, "x1a": 2, "x2a": 3,
"x1b": 4, "x2b": 5, "x1c": 6, "x2c": 7,
"nu": 8, "none": None}
def get_term(term):
t = [0] * (len(terms) - 1)
if terms[term] is not None:
t[terms[term]] = 1
return tuple(t)
def get_term_array(term):
return np.array(get_term(term))
def get_poly(**kwargs):
return from_terms(
{get_term(key): val for key, val in kwargs.items()}
)
# Stiffness matrix interpolation functions
Na = (get_poly(x2=1, x2b=-1) *get_poly(x1c=1, x1b=-1)
-get_poly(x1=1, x1b=-1) *get_poly(x2c=1, x2b=-1)
) / (get_poly(x2a=1, x2b=-1) *get_poly(x1c=1, x1b=-1)
-get_poly(x1a=1, x1b=-1) *get_poly(x2c=1, x2b=-1))
Nb = (get_poly(x2=1, x2c=-1) *get_poly(x1a=1, x1c=-1)
-get_poly(x1=1, x1c=-1) *get_poly(x2a=1, x2c=-1)
) / (get_poly(x2b=1, x2c=-1) *get_poly(x1a=1, x1c=-1)
-get_poly(x1b=1, x1c=-1) *get_poly(x2a=1, x2c=-1))
Nc = (get_poly(x2=1, x2a=-1) *get_poly(x1b=1, x1a=-1)
-get_poly(x1=1, x1a=-1) *get_poly(x2b=1, x2a=-1)
) / (get_poly(x2c=1, x2a=-1) *get_poly(x1b=1, x1a=-1)
-get_poly(x1c=1, x1a=-1) *get_poly(x2b=1, x2a=-1))
# Strain-displacement matrix
B = np.full((3, 6), Polynomial.zero(len(terms) - 1) / Polynomial.one(len(terms) - 1))
B[0, 0] = Na.deriv(get_term_array('x1'))
B[0, 2] = Nb.deriv(get_term_array('x1'))
B[0, 4] = Nc.deriv(get_term_array('x1'))
B[1, 1] = Na.deriv(get_term_array('x2'))
B[1, 3] = Nb.deriv(get_term_array('x2'))
B[1, 5] = Nc.deriv(get_term_array('x2'))
B[2, 0] = Na.deriv(get_term_array('x2'))
B[2, 1] = Na.deriv(get_term_array('x1'))
B[2, 2] = Nb.deriv(get_term_array('x2'))
B[2, 3] = Nb.deriv(get_term_array('x1'))
B[2, 4] = Nc.deriv(get_term_array('x2'))
B[2, 5] = Nc.deriv(get_term_array('x1'))
# Material constitutive matrix (plane stress)
D = np.full(
(3, 3),
Polynomial.zero(len(terms) - 1) / Polynomial.one(len(terms) - 1)
)
D[0, 0] = D[1, 1] = get_poly(nu=0, none=1)
D[1, 0] = D[0, 1] = get_poly(nu=1)
D[2, 2] = get_poly(nu=-0.5, none=0.5)
D = D / (get_poly(none=1, nu=1) * get_poly(none=1, nu=-1))
# Coordinate Determinant
Det = (
get_poly(x1a=1) * get_poly(x2b=1, x2c=-1)
+ get_poly(x1b=1) * get_poly(x2c=1, x2a=-1)
+ get_poly(x1c=1) * get_poly(x2a=1, x2b=-1)
)
# Compute an abstract representation for the stiffness matrix of a constant
# strain triangular element
K = (B.T @ D @ B) / Det
# Specialize the stiffness matrix for a particular element
coords = np.array([0, -1, 2, 0, 0, 1, 0.25])
indices = np.array([2, 3, 4, 5, 6, 7, 8])
vfunc = np.vectorize(lambda a: a.partial(indices, coords).expand().to_constant())
K_special = vfunc(K)
expected = np.array([
[ 2.5 , 1.25 , -2. , -1.5 , -0.5 , 0.25 ],
[ 1.25 , 4.375, -1. , -0.75 , -0.25 , -3.625],
[-2. , -1. , 4. , 0. , -2. , 1. ],
[-1.5 , -0.75 , 0. , 1.5 , 1.5 , -0.75 ],
[-0.5 , -0.25 , -2. , 1.5 , 2.5 , -1.25 ],
[ 0.25 , -3.625, 1. , -0.75 , -1.25 , 4.375]
]) / (0.9375 * 64)
assert np.allclose(expected, K_special)
Project details
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distributions
File details
Details for the file rust_poly-0.1.3.tar.gz
.
File metadata
- Download URL: rust_poly-0.1.3.tar.gz
- Upload date:
- Size: 8.2 MB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: maturin/0.12.7-beta.8
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 0dbbff3b9d4bf06e955d32fcbd1c043fdefa9609f6153f0f86064a8e925b1d6a |
|
MD5 | fc52ba6f72b49d365488ec69cda248fa |
|
BLAKE2b-256 | 99942d0f1ba6a04b13c2d47ba688d1c8a3727d288bd39db56556cfcc418dedf4 |
File details
Details for the file rust_poly-0.1.3-pp37-pypy37_pp73-manylinux_2_5_x86_64.manylinux1_x86_64.whl
.
File metadata
- Download URL: rust_poly-0.1.3-pp37-pypy37_pp73-manylinux_2_5_x86_64.manylinux1_x86_64.whl
- Upload date:
- Size: 551.4 kB
- Tags: PyPy, manylinux: glibc 2.5+ x86-64
- Uploaded using Trusted Publishing? No
- Uploaded via: maturin/0.12.7-beta.8
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 69129b2075f5c46c4186115391f6fe38abbeb967d462e06c9b990427ea291a0a |
|
MD5 | 581cb87ce4055a14c044cb5e2c98d311 |
|
BLAKE2b-256 | 76d44ceef4503b5dfd37c0ff91b590678da25b38e074f3bf3402a08b85f659f0 |
File details
Details for the file rust_poly-0.1.3-cp310-cp310-manylinux_2_5_x86_64.manylinux1_x86_64.whl
.
File metadata
- Download URL: rust_poly-0.1.3-cp310-cp310-manylinux_2_5_x86_64.manylinux1_x86_64.whl
- Upload date:
- Size: 549.8 kB
- Tags: CPython 3.10, manylinux: glibc 2.5+ x86-64
- Uploaded using Trusted Publishing? No
- Uploaded via: maturin/0.12.7-beta.8
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 58d58548cf8a69c27a3cb2b37b18a9db998c5c772bbc8ef8f1d60a5d5b92bef8 |
|
MD5 | edd5c64c6d9a44e6e4665a9e79c710c1 |
|
BLAKE2b-256 | c625794f900d0aa82a808d9075fdd0e2894babb029e8bb11963ec46c4807e71a |
File details
Details for the file rust_poly-0.1.3-cp39-cp39-manylinux_2_5_x86_64.manylinux1_x86_64.whl
.
File metadata
- Download URL: rust_poly-0.1.3-cp39-cp39-manylinux_2_5_x86_64.manylinux1_x86_64.whl
- Upload date:
- Size: 550.6 kB
- Tags: CPython 3.9, manylinux: glibc 2.5+ x86-64
- Uploaded using Trusted Publishing? No
- Uploaded via: maturin/0.12.7-beta.8
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 8871129e14e13017fd597892ad8ebcb36c6111ada099e2fb3d8261745e964678 |
|
MD5 | afb91b5641f5b66fb31ad3f07a0060ea |
|
BLAKE2b-256 | eb55fe99634e3283455c8ca509553923c8da06ec292906673f05f99540daf532 |
File details
Details for the file rust_poly-0.1.3-cp38-cp38-manylinux_2_5_x86_64.manylinux1_x86_64.whl
.
File metadata
- Download URL: rust_poly-0.1.3-cp38-cp38-manylinux_2_5_x86_64.manylinux1_x86_64.whl
- Upload date:
- Size: 550.5 kB
- Tags: CPython 3.8, manylinux: glibc 2.5+ x86-64
- Uploaded using Trusted Publishing? No
- Uploaded via: maturin/0.12.7-beta.8
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | c167c6d559d08f30cbacb23643d4a5a1fe6c3cca4bff670cf0b2119ed43ba913 |
|
MD5 | ea9b9ccbdcfa46d1b3ae275a56d34306 |
|
BLAKE2b-256 | bf08f500e3adb09440b72516104b06552bca1e1bb732024853ba244e6adb0b2f |
File details
Details for the file rust_poly-0.1.3-cp37-cp37m-manylinux_2_5_x86_64.manylinux1_x86_64.whl
.
File metadata
- Download URL: rust_poly-0.1.3-cp37-cp37m-manylinux_2_5_x86_64.manylinux1_x86_64.whl
- Upload date:
- Size: 550.7 kB
- Tags: CPython 3.7m, manylinux: glibc 2.5+ x86-64
- Uploaded using Trusted Publishing? No
- Uploaded via: maturin/0.12.7-beta.8
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | bfde782f2abde1e7d3e0f280068bba93eb6e4617098dcb0b9c6db70773803269 |
|
MD5 | 5cb04b04fe8f170f30e52615bb981e3a |
|
BLAKE2b-256 | 96252a87db5cd272cbd66909e1de7e6f8082e582a6889369d2a7b452edda0085 |
File details
Details for the file rust_poly-0.1.3-cp36-cp36m-manylinux_2_5_x86_64.manylinux1_x86_64.whl
.
File metadata
- Download URL: rust_poly-0.1.3-cp36-cp36m-manylinux_2_5_x86_64.manylinux1_x86_64.whl
- Upload date:
- Size: 543.4 kB
- Tags: CPython 3.6m, manylinux: glibc 2.5+ x86-64
- Uploaded using Trusted Publishing? No
- Uploaded via: maturin/0.12.7-beta.8
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 91b393dc10dfb4a0dd2da3725144779efe37f50329398a209eec0df360a6110b |
|
MD5 | 7ef302a56383f4009e742b0251254cd5 |
|
BLAKE2b-256 | 4a2a363f67e1712b269e08cff42fe46ec6cd9c2536ab8d16bbbf85cc8d34975e |