Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

correct format of matrix #83

Closed
kagarwala opened this issue Sep 18, 2013 · 6 comments
Closed

correct format of matrix #83

kagarwala opened this issue Sep 18, 2013 · 6 comments

Comments

@kagarwala
Copy link

what's the correct format of matrix...??? getting confused with examples and code...

math.matrix([[5, 6], [1, 1]]) - this creates a matrix...

but following in parser it doesn't work...
math.parse("matrix([[5, 6], [1, 1]])matrix([[5, 6], [1, 1]])").eval()
math.parse("[[5, 6], [1, 1]]
[[5, 6], [1, 1]]").eval()

following work...
math.parse("matrix([[5, 6]; [1, 1]])matrix([[5, 6]; [1, 1]])").eval()....OR
math.parse("[[5, 6]; [1, 1]]
[[5, 6]; [1, 1]]").eval()...OR without inner [s...

And also...toString() in matrix returns matrix with rows separated by commas...
What am I missing?

@josdejong
Copy link
Owner

The syntax of the expression parser differs from JavaScript. It is aimed at mathematicians, not at programmers. It's syntax is similar to math applications. Main differences:

  • You have direct access to all functions in the math namespace, you call a function like sqrt(4), not math.sqrt(4)
  • A matrix is entered between square brackets. You can only enter 2D matrices. Columns are separated by a comma, rows by a semicolon. So math.eval('[1,2;3,4]') returns a 2x2 matrix [[1,2],[3,4]]
  • Matrix indexes are zero based, use round brackets, and include the indexes upper-bound. So if math.eval('A = [1,2;3,4]'), math.eval('A(1,1:2)') returns [[1,2]].
  • Matrices inside matrices will be merged/concatenated, like [A, B] to concatenate a matrix A and B.
  • In the expression parser you can use the range operator :, like math.eval('1:4').

The syntax of the expression parser is not yet stable. We recently decided to make the library zero-based, but make the expression parser one-based, see #66. And I'm for example not yet happy with being able to enter 2D matrices only (no vectors or n-dimensional matrices).

@kagarwala
Copy link
Author

Thanks for the explanation...
The part I was getting confused in Matrix...
Suppose.
parser takes [1,2;3,4] as a 2X2 matrix...But again toString on that matrix gives you [[1, 2], [3, 4]]...

this is creating an issue, where I need to take toString() output of a previous calculation ...as an input of another calculation...

So, what's the reason we are using different syntax...???

@josdejong
Copy link
Owner

So, what's the reason we are using different syntax...???

See #66.

As for the matrix notation: on a JavaScript level you just use the regular (nested) Array notation. The toString function outputs in this same notation. There isn't yet a consistent solution on the level of the expression parser, that's why I'm still looking around for a better solution.

this is creating an issue, where I need to take toString() output of a previous calculation ...as an input of another calculation...

Sounds like you are feeding stringified results back in the expression parser. Maybe you can use variables instead, which you can provide to the eval function in a custom scope (see example code in the readme under Eval).

@kagarwala
Copy link
Author

Sounds like you are feeding stringified results back in the expression parser.

That's correct...I tried with using the scope...

a = math.eval("[[1,2],[1,2]]")
b = math.eval("[[1,2],[1,2]]")
scope = {a:a,b:b}
math.eval("a*b", scope)
RangeError: Dimensions mismatch in multiplication. Columns of A must match rows of B (A is 1x4, B is 1x4, 4 != 1)

In array notation I am unable to use the matrix node for further calculation....
But everything works fine if I stick to to parser's notaion...

math.eval("[1,2;1,2]*[1,2;1,2]")

Even if I use the scoping thing to reuse my results, as in the above example....It seems it won't work out...
Currently, the feasible solution to this looks like chage the toString to format matrix node in a way which will be picked by parser...

@josdejong
Copy link
Owner

You are still mixing the notations up :). Try this:

a = math.eval("[1,2;1,2]")
b = math.eval("[1,2;1,2]")
scope = {a:a,b:b}
math.eval("a*b", scope)

I have been thinking about the issue that you can currently only enter 2 dimensional matrices in the expression parser (matlab/octave style). To solve this, I think I have to remove the concatenation of matrices using the notation [A, B] and [A; B]. You can use the function concat instead. With this concatenation "feature" removed, we can nicely input matrices with any dimensions using the regular JavaScript style of nested Arrays!

@josdejong
Copy link
Owner

I have fixed this :). You can try the develop branch already (you will need to build the library first). Allows you to enter nested arrays JavaScript style.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants