Matrix operations

215 views
Skip to first unread message

Joe Heafner

unread,
Apr 5, 2017, 9:32:21 AM4/5/17
to GlowScript Users
In GlowScript what is the preferred method for doing matrix multiplication? I can create my own function for doing it using lists, but is there a more efficient way?

Joe Heafner
Sent from my iPhone

Bruce Sherwood

unread,
Apr 5, 2017, 10:40:37 AM4/5/17
to Glowscript Users
Some matrix methods are used internal to GlowScript but they don't constitute a useful general set, so the answer currently is that you should write your own. You might wish to emulate numpy syntax so that it would be easy to run your program in Jupyter VPython.

Note that "for" loops are expensive in true Python (hence one of the needs for numpy) but not in the JavaScript to which GlowScript VPython is transpiled.

Joe Heafner

unread,
Apr 6, 2017, 1:41:38 PM4/6/17
to GlowScript Users
Here’s the program I came up with.

http://www.glowscript.org/#/user/heafnerj/folder/Joe'sStuff/program/matrices

Note that there are actually two functions defined, matxvec() and matxmat(). To use matxvec(), the matrix is a two-dimensional entity that must be initialized prior to being passed to the function and similarly, the vector is a one-dimensional entity that must also be initialized and defined beforehand. The first block of test code at the bottom illustrates this. I wanted matxmat() to be flexible enough to recognize whether the second argument passed to it is a vector or a matrix. If I want that second argument to be a vector, I can’t define it as, for example, vector(2,1,2) as in VPython. Instead, I must define it as [[2],[1],[2]], which wouldn’t be intuitive to students.

So my question now is, is there a way to go between the two notations b=vector(2,1,2) and b=[[2],[1],[2]] for a vector without using an explicit assignment ( e.g. vector(b[0][0],b[1][0],b[2][0]) )? For what I have in mind, the vector in question is the axis property of an arrow object. I want to operate on that axis vector with a transformation (matrix) and update it accordingly. I’m not familiar with numpy so there may be a way to do it therein.


In passing, I learned that you must initialize a two-dimensional matrix as a[[]] because the a[][] syntax doesn’t work in Python. This makes sense once I stopped thinking in C/C++ and started thinking in Python.

Joe Heafner
Sent from one of my Macs

Reply all
Reply to author
Forward
0 new messages