# Matrix multiplication and row vectors Classic List Threaded 24 messages 12
Open this post in threaded view
|

## Matrix multiplication and row vectors

 I notice that multmatrix() takes matrices in the traditional format where, for example, translation terms are the in the last column.  However, matrix maths in OpenSCAD user land is done with transposed matrices, where translation terms are on the last row.I understand this is because vectors are treated as rows instead of columns when doing the multiply. It is inconvenient when at the end of  calculating some transformation I want to pass the result to multmatrix and have to transpose it.Experimentally it seems that if I reverse the order of multiplication, so that instead of row vector times transformation matrix I have matrix times row vector then I can use traditional format transformation matrices and pass them directly to multmatrix. Is this correct? It seems too simple.m = translate([1,2,3]);m2 = transpose(m);echo(m);echo(m2);echo([4,5,6, 1] * m);echo(m2 * [4,5,6,1]);ECHO: [[1, 0, 0, 0], [0, 1, 0, 0], [0, 0, 1, 0], [1, 2, 3, 1]] ECHO: [[1, 0, 0, 1], [0, 1, 0, 2], [0, 0, 1, 3], [0, 0, 0, 1]] ECHO: [5, 7, 9, 1] ECHO: [5, 7, 9, 1] If I just rewrite all my transformations in conventional order and swap just the multiplication order with row vectors, will everything work out? _______________________________________________ OpenSCAD mailing list [hidden email] http://lists.openscad.org/mailman/listinfo/discuss_lists.openscad.org
Open this post in threaded view
|

## Re: Matrix multiplication and row vectors

 This statement seems to imply the answer is yes:  https://en.wikipedia.org/wiki/Matrix_multiplication#TransposeOn Mon, 1 Apr 2019 at 14:28, nop head <[hidden email]> wrote:I notice that multmatrix() takes matrices in the traditional format where, for example, translation terms are the in the last column.  However, matrix maths in OpenSCAD user land is done with transposed matrices, where translation terms are on the last row.I understand this is because vectors are treated as rows instead of columns when doing the multiply. It is inconvenient when at the end of  calculating some transformation I want to pass the result to multmatrix and have to transpose it.Experimentally it seems that if I reverse the order of multiplication, so that instead of row vector times transformation matrix I have matrix times row vector then I can use traditional format transformation matrices and pass them directly to multmatrix. Is this correct? It seems too simple.m = translate([1,2,3]);m2 = transpose(m);echo(m);echo(m2);echo([4,5,6, 1] * m);echo(m2 * [4,5,6,1]);ECHO: [[1, 0, 0, 0], [0, 1, 0, 0], [0, 0, 1, 0], [1, 2, 3, 1]] ECHO: [[1, 0, 0, 1], [0, 1, 0, 2], [0, 0, 1, 3], [0, 0, 0, 1]] ECHO: [5, 7, 9, 1] ECHO: [5, 7, 9, 1] If I just rewrite all my transformations in conventional order and swap just the multiplication order with row vectors, will everything work out? _______________________________________________ OpenSCAD mailing list [hidden email] http://lists.openscad.org/mailman/listinfo/discuss_lists.openscad.org
Open this post in threaded view
|

## Re: Matrix multiplication and row vectors

Open this post in threaded view
|

## Re: Matrix multiplication and row vectors

 Yes OpenSCAD seems unable to handle single column vectors but the transpose of a column vector is a row vector, so I think that is why it works when I swap the order. The order has been swapped and both operands have been transposed, so the result is the same but now the matrix is in conventional format.So now I have conventional transformation matrices that can be passed directly to multmatrix simply by swapping the multiplication order. The other thing that changed is the order that I have to multiply transformation matrices with each other is reversed. This seems more natural because translate () rotate() in modules becomes translate() * rotate() in functions, whereas before I had do it in reverse, which seemed wrong.I used it to speed up the preview of this box from 63 seconds to 5 seconds. All the time was taken making the holes in the sheets. In 3D it is slow because of CGAL hates lots of holes. When I made the sheets 2D I had to project the holes and that was not much faster as projection() is very slow. Now I represent the holes positions and orientation with matrices and project the holes with matrix multiplication and filter the ones that lie on the sheet like this.module drill_holes(type, t)    for(list = [corner_holes(type), side_holes(type)], p = list)        let(q = t * p)            if(abs(transform([0, 0, 0], q).z) < eps)                multmatrix(q)                    drill(screw_clearance_radius(bbox_screw(type)), 0);t is the transformation that maps the holes for each sheet to the X plane.On Mon, 1 Apr 2019 at 16:15, Ronaldo Persiano <[hidden email]> wrote:Mathematically, the product of a matrix by a row matrix is undefined (except if the matrix has just one column) but in OpenSCAD, the vector is considered a column vector instead of a row vector in the product of a matrix by a vector. On the other hand, the product of a vector by a matrix in OpenSCAD has the same meaning it has in Mathematics. So, in Mathematics, the following holds for a row vector v and matrix M :v * M  = transpose(M)*transpose(v)but in OpenSCAD we get an undefined vector in the evaluation of the right expression.Em seg, 1 de abr de 2019 às 14:33, nop head <[hidden email]> escreveu:This statement seems to imply the answer is yes:  https://en.wikipedia.org/wiki/Matrix_multiplication#TransposeOn Mon, 1 Apr 2019 at 14:28, nop head <[hidden email]> wrote:I notice that multmatrix() takes matrices in the traditional format where, for example, translation terms are the in the last column.  However, matrix maths in OpenSCAD user land is done with transposed matrices, where translation terms are on the last row.I understand this is because vectors are treated as rows instead of columns when doing the multiply. It is inconvenient when at the end of  calculating some transformation I want to pass the result to multmatrix and have to transpose it.Experimentally it seems that if I reverse the order of multiplication, so that instead of row vector times transformation matrix I have matrix times row vector then I can use traditional format transformation matrices and pass them directly to multmatrix. Is this correct? It seems too simple.m = translate([1,2,3]);m2 = transpose(m);echo(m);echo(m2);echo([4,5,6, 1] * m);echo(m2 * [4,5,6,1]);ECHO: [[1, 0, 0, 0], [0, 1, 0, 0], [0, 0, 1, 0], [1, 2, 3, 1]] ECHO: [[1, 0, 0, 1], [0, 1, 0, 2], [0, 0, 1, 3], [0, 0, 0, 1]] ECHO: [5, 7, 9, 1] ECHO: [5, 7, 9, 1] If I just rewrite all my transformations in conventional order and swap just the multiplication order with row vectors, will everything work out? _______________________________________________ OpenSCAD mailing list [hidden email] http://lists.openscad.org/mailman/listinfo/discuss_lists.openscad.org _______________________________________________ OpenSCAD mailing list [hidden email] http://lists.openscad.org/mailman/listinfo/discuss_lists.openscad.org _______________________________________________ OpenSCAD mailing list [hidden email] http://lists.openscad.org/mailman/listinfo/discuss_lists.openscad.org
Open this post in threaded view
|

## Re: Matrix multiplication and row vectors

Open this post in threaded view
|

## Re: Matrix multiplication and row vectors

Open this post in threaded view
|

## Re: Matrix multiplication and row vectors

Open this post in threaded view
|

## Re: Matrix multiplication and row vectors

Open this post in threaded view
|

## Re: Matrix multiplication and row vectors

Open this post in threaded view
|

## Re: Matrix multiplication and row vectors

Open this post in threaded view
|

## Re: Matrix multiplication and row vectors

 On Wed, Apr 03, 2019 at 11:22:54AM +0100, nop head wrote: >  I don't think there is any distinction in > maths: one dimensional matrices are vectors. It is just computer science > where matrices are often lists of lists and vectors are just lists No. In math there is a difference between column vectors and row-vectors. If you multiply a column vector with a row vector one way and you get a matrix, multiply them the other way around, you get a scalar. It is computer science that cannot simply distiguish between the two: a vector is a vector. As you say: you can make an Nx1 and an 1xN matrices to show the difference.         Roger. -- ** [hidden email] ** https://www.BitWizard.nl/ ** +31-15-2049110 ** **    Delftechpark 11 2628 XJ  Delft, The Netherlands.  KVK: 27239233    ** The plan was simple, like my brother-in-law Phil. But unlike Phil, this plan just might work. _______________________________________________ OpenSCAD mailing list [hidden email] http://lists.openscad.org/mailman/listinfo/discuss_lists.openscad.org
Open this post in threaded view
|

## Re: Matrix multiplication and row vectors

 I meant there are no 1xn or nx1 matrices in maths, they are just row or column vectors.On Wed, 3 Apr 2019 at 11:36, Rogier Wolff <[hidden email]> wrote:On Wed, Apr 03, 2019 at 11:22:54AM +0100, nop head wrote: >  I don't think there is any distinction in > maths: one dimensional matrices are vectors. It is just computer science > where matrices are often lists of lists and vectors are just lists No. In math there is a difference between column vectors and row-vectors. If you multiply a column vector with a row vector one way and you get a matrix, multiply them the other way around, you get a scalar. It is computer science that cannot simply distiguish between the two: a vector is a vector. As you say: you can make an Nx1 and an 1xN matrices to show the difference.         Roger. -- ** [hidden email] ** https://www.BitWizard.nl/ ** +31-15-2049110 ** **    Delftechpark 11 2628 XJ  Delft, The Netherlands.  KVK: 27239233    ** The plan was simple, like my brother-in-law Phil. But unlike Phil, this plan just might work. _______________________________________________ OpenSCAD mailing list [hidden email] http://lists.openscad.org/mailman/listinfo/discuss_lists.openscad.org _______________________________________________ OpenSCAD mailing list [hidden email] http://lists.openscad.org/mailman/listinfo/discuss_lists.openscad.org
Open this post in threaded view
|

## Re: Matrix multiplication and row vectors

 On Wed, Apr 03, 2019 at 11:48:44AM +0100, nop head wrote: > I meant there are no 1xn or nx1 matrices in maths, they are just row or > column vectors. Oh, ok. I think in maths a 1xN or Nx1 matrix is precisely equivalent to the corresponding vector. Everything is a matrix, what is commonly known as a vector is a matrix with one dimension "1". What is commonly known as a "scalar" is a matrix with both dimensions "1"....         Roger. -- ** [hidden email] ** https://www.BitWizard.nl/ ** +31-15-2049110 ** **    Delftechpark 11 2628 XJ  Delft, The Netherlands.  KVK: 27239233    ** The plan was simple, like my brother-in-law Phil. But unlike Phil, this plan just might work. _______________________________________________ OpenSCAD mailing list [hidden email] http://lists.openscad.org/mailman/listinfo/discuss_lists.openscad.org
Open this post in threaded view
|

## Re: Matrix multiplication and row vectors

 It is... er... matter of definitions. If scalar was 1x1 matrix you could not multiply MxN matrix by scalar. On 4/3/2019 1:53 PM, Rogier Wolff wrote: > On Wed, Apr 03, 2019 at 11:48:44AM +0100, nop head wrote: >> I meant there are no 1xn or nx1 matrices in maths, they are just row or >> column vectors. > > Oh, ok. I think in maths a 1xN or Nx1 matrix is precisely equivalent > to the corresponding vector. > > Everything is a matrix, what is commonly known as a vector is a matrix > with one dimension "1". What is commonly known as a "scalar" is a > matrix with both dimensions "1".... > > Roger. > _______________________________________________ OpenSCAD mailing list [hidden email] http://lists.openscad.org/mailman/listinfo/discuss_lists.openscad.org
Open this post in threaded view
|

## Re: Matrix multiplication and row vectors

 Let me try to summarize this discussion regarding OpenSCAD codes:a) matrices in OpenSCAD are list of lists of scalars; simple vectors (list of scalars) are not matrices;b) as such, vectors can't be transposed like matrices;c) in products v*M, where v is a vector and M an appropriate matrix, v may be regarded as a row matrix;d) in products M*v, v may be regarded as a column matrix;e) v*M == T(M)*v and v*T(M) == M*v where T is the transpose operator;f) v*w is a scalar when v and w are vectors of same dimension.g) the product of a matrix 1xn by a matrix nXm is a matrix 1xm which is not a m-dimensional vector;h) T(Q*R) = T(R)*T(Q) for any matrix Q nXm and matrix R mXk where T is the transpose operator.So, OpenSCAD matrix multiplications agree the mathematical equivalent. The differences between math and OpenSCAD are just the a), b) and d) above. _______________________________________________ OpenSCAD mailing list [hidden email] http://lists.openscad.org/mailman/listinfo/discuss_lists.openscad.org
Open this post in threaded view
|

## Re: Matrix multiplication and row vectors

Open this post in threaded view
|

## Re: Matrix multiplication and row vectors

 In reply to this post by nophead nop head <[hidden email]> wrote:I would be inclined to write this using a single matrix product rather than a loop of matrix vector products like this:function transform_path(path,m) = path*transpose(m);Yes I think that might be quicker but it produces vec4s and polyhedron only seems to accept vec3. Stripping the ones with a for loop after makes it slower.Striping the ones may be avoided if the matrices are 3x4. The operator multmatrix() ignores the last row and accepts 3x4 matrices so if you drop the last row of your matrices, multmatrix() will work as expected. However, to apply a 3x4 matrix to a list of 4D points, we will need to transpose the matrix:[for(i=[0:len(p)-1]) p[i]] * transpose(M) _______________________________________________ OpenSCAD mailing list [hidden email] http://lists.openscad.org/mailman/listinfo/discuss_lists.openscad.org
Open this post in threaded view
|

## Re: Matrix multiplication and row vectors

 Ronaldo wrote > nop head < > nop.head@ > > wrote: > >> I would be inclined to write this using a single matrix product rather >>> than a loop of matrix vector products like this: >>> function transform_path(path,m) = path*transpose(m); >>> >> >> Yes I think that might be quicker but it produces vec4s and polyhedron >> only seems to accept vec3. Stripping the ones with a for loop after makes >> it slower. >> > > Striping the ones may be avoided if the matrices are 3x4. The operator > multmatrix() ignores the last row and accepts 3x4 matrices so if you drop > the last row of your matrices, multmatrix() will work as expected. > However, > to apply a 3x4 matrix to a list of 4D points, we will need to transpose > the > matrix: > > [for(i=[0:len(p)-1]) p[i]] * transpose(M) Wouldn't simply p*transpose(M) be better, where M is 3x4?   -- Sent from: http://forum.openscad.org/_______________________________________________ OpenSCAD mailing list [hidden email] http://lists.openscad.org/mailman/listinfo/discuss_lists.openscad.org
Open this post in threaded view
|

## Re: Matrix multiplication and row vectors

 Wouldn't simply p*transpose(M) be better, where M is 3x4?   It is a pity that my emphasis that p is a list of vectors has been misunderstood. _______________________________________________ OpenSCAD mailing list [hidden email] http://lists.openscad.org/mailman/listinfo/discuss_lists.openscad.org