fkleon / math-expressions

A library for parsing and evaluating mathematical expressions, supporting real numbers, vectors, and basic interval arithmetic.
https://pub.dev/packages/math_expressions
MIT License
101 stars 35 forks source link

Support evaluating Vector expressions of arbitrary length #3

Open almstrand opened 9 years ago

almstrand commented 9 years ago

Are there plans to support evaluating Vector expressions of arbitrary length?

Calling evaluate in the following example ...

ContextModel cm = new ContextModel();

Expression vector1 = new Vector([new Number(1), new Number(2), new Number(3), new Number(4), new Number(5)]);
Expression vector2 = new Vector([new Number(1), new Number(2), new Number(3), new Number(4), new Number(5)]);

Expression e = vector1 + vector2;

print(e.evaluate(EvaluationType.VECTOR, cm));

... causes UnimplementedError to be thrown:

Exception: UnimplementedError: Vector of arbitrary length (> 4) are not supported yet.
  Vector.evaluate   
  Plus.evaluate 
  ...

The expected output is "2.0,4.0,6.0,8.0,10.0". It would be great if this limit can be removed.

It seems the current implementation is 99% there. Perhaps classes Vector2, Vector3, and Vector4 can be generalized into a new class that constructs a vector of arbitrary size?

fkleon commented 9 years ago

Hi, thanks for your report!

Currently math_expressions is using vector_math under the hood for its vector representations - which is unfortunately only supporting vectors up to 4 elements.

It shouldn't be too hard to introduce a generalisation though - at least if performance is not crucial - vector_math even comes with an abstract base class backed by a simple list.

Handling of vectors in math_expressions is still a bit rough and a lot of functions don't work on vectors yet. I might be able to flesh out some basic implementation for vectors of arbitrary length relatively easily. In the meantime, pull requests are always welcome :)

almstrand commented 9 years ago

Thanks for the quick response and feedback. In thinking more about the problem it seems a preferred solution may be for the vector_math package to support n-dimensional vectors.

I filed an issue with the project to see how the community feels about supporting that.

As n-dimensional vectors have many applications, it would be a shame for this restriction to become a cause for fragmentation at the expense of the otherwise excellent vector_math package, and a cause for mostly redundant vector packages to appear on pub.dartlang.org.