bertiqwerty / exmex

Math parser and evaluator in Rust, capable of partial differentiation, allows the use of custom operators.
Apache License 2.0
39 stars 6 forks source link

Vector Math #53

Closed markusmoenig closed 6 months ago

markusmoenig commented 8 months ago

Hi,

I love the performance and footprint! I guess basic vector math cannot be added via custom types right ?

Thanks

bertiqwerty commented 8 months ago

Hi. You want to add/subtract/... vectors? You can do that. See https://github.com/basf/rormula

This Python library uses Exmex with a custom data type under the hood for calculating with vectors.

markusmoenig commented 8 months ago

Actually I want to have formulas for dot / cross products and multiplications. Ok, I will take a look thanks.

bertiqwerty commented 8 months ago

I see. You could use Exmex and implement those as custom operators with a custom vector data type.

markusmoenig commented 8 months ago

I will read myself into it. But is it possible to access individual vector elements (normal.x) or only the full vector ? (normal).

bertiqwerty commented 8 months ago

You can have operators transforming the vectors into scalars. For instance . could be a binary operator. Then, normal.0 could be what I think you mean with normal.x. If you want to name the first component with x then you would need a unary operator and a syntax like get_x(v). The result of this and also of, e.g, the dot-product is a scalar. You could have your data type to be an enum with vector and scalar variants or you could treat scalars as vectors with 1 element. In Rormula I did the former. The 1-element thing I have not tried myself. Now that I am thinking about it, if you want to create a new vector from scalars you probably can use ; as a binary operator, e.g., like (v.1; v.0) to swap the first to elements of a vector v. The comma , has a special meaning during parsing so you cannot use this as your own operator.

markusmoenig commented 8 months ago

I don't need to create vectors, just use them. But would be great if we could have an example how to implement vector math one day, I guess more people are interested in this.

bertiqwerty commented 7 months ago

Do you mean 3d-vectors? Or any type of dimension? And can you list all operations that you would need?

markusmoenig commented 7 months ago

3D mostly (2D would be nice but could be derived easily I guess). +, -, *, /, dot, length.

bertiqwerty commented 7 months ago

and cross-product probably? I assume that you mean * and / component-wise.

markusmoenig commented 7 months ago

I don't really need cross, but yes why not. Actually a clamp would be cool (also for f64, I don't think this is available right now).