# Vectors have an inconsistent interpretation in matrix math

11 messages
Open this post in threaded view
|

## Vectors have an inconsistent interpretation in matrix math

 Mathematically, we might expect 1-dimensional vectors to act like scalars and nx1-dimensional matrices to act like vectors. There are some important places where current implementations seem to violate these expectations:1-dimensional vectors are not scalars with respect to vectors:2 * #(1 2 3) asPMVector. "a PMVector(2 4 6)"#(2) asPMVector * #(1 2 3) asPMVector. "Error"1-dimensional vectors are not scalars with respect to matrices, and in fact have undefined behavior:2 * (PMMatrix rows: #((1 2) (3 4))). "a PMVector(2 4) a PMVector(6 8)"#(2) asPMVector * (PMMatrix rows: #((1 2) (3 4))). "a PMVector(2 4)"n-dimensional vectors are row vectors when right-multiplied by an nx1 matrix, but column vectors when left multiplied by a matrix 1xn, meaning they cannot be used as either a 1xn or an nx1 matrix in code that expects one or the other: the dimensions will be unpredictable.#(1 2) asPMVector * (PMMatrix rows: #((1) (2))). "a PMVector(5)"(PMMatrix rows: #((1 2))) * #(1 2) asPMVector. "a PMVector(5)"1x1 matrices are not scalars with respect to vectors:(PMMatrix rows: #((2))) * #(1 2) asPMVector. "Error"1xn * nx1 matrix multiplication produces a matrix, not a scalar, which behaves differently than a scalar, as per above:(PMMatrix rows: #((1 2))) * (PMMatrix rows: #((1) (2))) "a PMVector(5) <-- actually a 1x1 matrix"As long as one works only with scalars and vectors OR scalars and matrices, things seem fine. It seems like maybe either matrix-vector operations should throw errors, or vectors should behave consistently as an nx1 matrix during matrix math. It may also make sense to make the 1-dimensional vectors and matrices either just convert to scalar Numbers, or make them polymorphic with #= etc, but I'm not sure. -- You received this message because you are subscribed to the Google Groups "SciSmalltalk" group. To unsubscribe from this group and stop receiving emails from it, send an email to [hidden email]. For more options, visit https://groups.google.com/d/optout.
Open this post in threaded view
|

## Re: Vectors have an inconsistent interpretation in matrix math

 One possible solution is to reduce a size-1 vector to a scalar, exactly like a Fraction with denominator 1 is reduced to Integer (for example 2/1 -> 2).But in this case, the scalar has to be polymorphic to a vector (understand the whole vector protocol...).For nx1 and 1xn matrices, this sounds more delicate: if we reduce it to a vector, we loose the layout (we have no distinction of column/row vectors so far). In Smallapack, I chosed to have only matrix for this reason.2017-05-18 20:56 GMT+02:00 Evan Donahue :Mathematically, we might expect 1-dimensional vectors to act like scalars and nx1-dimensional matrices to act like vectors. There are some important places where current implementations seem to violate these expectations:1-dimensional vectors are not scalars with respect to vectors:2 * #(1 2 3) asPMVector. "a PMVector(2 4 6)"#(2) asPMVector * #(1 2 3) asPMVector. "Error"1-dimensional vectors are not scalars with respect to matrices, and in fact have undefined behavior:2 * (PMMatrix rows: #((1 2) (3 4))). "a PMVector(2 4) a PMVector(6 8)"#(2) asPMVector * (PMMatrix rows: #((1 2) (3 4))). "a PMVector(2 4)"n-dimensional vectors are row vectors when right-multiplied by an nx1 matrix, but column vectors when left multiplied by a matrix 1xn, meaning they cannot be used as either a 1xn or an nx1 matrix in code that expects one or the other: the dimensions will be unpredictable.#(1 2) asPMVector * (PMMatrix rows: #((1) (2))). "a PMVector(5)"(PMMatrix rows: #((1 2))) * #(1 2) asPMVector. "a PMVector(5)"1x1 matrices are not scalars with respect to vectors:(PMMatrix rows: #((2))) * #(1 2) asPMVector. "Error"1xn * nx1 matrix multiplication produces a matrix, not a scalar, which behaves differently than a scalar, as per above:(PMMatrix rows: #((1 2))) * (PMMatrix rows: #((1) (2))) "a PMVector(5) <-- actually a 1x1 matrix"As long as one works only with scalars and vectors OR scalars and matrices, things seem fine. It seems like maybe either matrix-vector operations should throw errors, or vectors should behave consistently as an nx1 matrix during matrix math. It may also make sense to make the 1-dimensional vectors and matrices either just convert to scalar Numbers, or make them polymorphic with #= etc, but I'm not sure. -- You received this message because you are subscribed to the Google Groups "SciSmalltalk" group. To unsubscribe from this group and stop receiving emails from it, send an email to [hidden email]. For more options, visit https://groups.google.com/d/optout. -- You received this message because you are subscribed to the Google Groups "SciSmalltalk" group. To unsubscribe from this group and stop receiving emails from it, send an email to [hidden email]. For more options, visit https://groups.google.com/d/optout.
Open this post in threaded view
|

## Re: Vectors have an inconsistent interpretation in matrix math

 In reply to this post by Evan Donahue Hi Evan,reducing 1-dimensional vectors to scalars is not such a good idea:1. the whole vector protocol consists also of the Array protocol, the ArrayedCollection, the SequenceableCollection and the Collection protocol.2. a 1-dimensional vector _is_ something different than a scalar! eg a whole lot of methods take an Array as argument, even if it has only one dimension, but usually they wouldnt work with a scalar. hence you can't "expect 1-dimensional vectors to act like scalars".re: #(2) asPMVector * (PMMatrix rows: #((1 2) (3 4))). "a PMVector(2 4)"yes, this is in a way a problem and the problem is here:#(2 3) asPMVector * #(1) asPMVector."2"there exists the occasional warning here & there, that for speed reasons no size-check is made - well, probably more there than here -, but unfortunately this warning does at the moment not exist in PMVector>>productWithVector: . if this would have a size-check, this result would not happen. but it would slow down the calculations a tiny bit, since productWithVector: is called very very often. i would prefer if these things would not be made slower than necessary. one just has to pay attention and not produce nonsense code. so that your (and my) example also produces an error you would have to change PMVector>>productWithVector: to produce an SizeMismatch error or so. ok, i think this is unnecessary, but if i'm the only one who wants these things to be fast, then just change it (i can also use earlier versions for real-world-calculations if things are getting too slow for me because of all kinds of error-checks one can think of)."n-dimensional vectors...cannot be used as either a 1xn or an nx1 matrix in code that expects one or the other" you can 'practically' specify that by using "( )" around a matrix & vector multiplication with the correct sequence (or simply by using a matrix instead). where else would you really need to make that distinction for a vector? at the moment, i have to admit, i dont really see the problem.wernerOn Thu, May 18, 2017 at 8:56 PM, Evan Donahue wrote:Mathematically, we might expect 1-dimensional vectors to act like scalars and nx1-dimensional matrices to act like vectors. There are some important places where current implementations seem to violate these expectations:1-dimensional vectors are not scalars with respect to vectors:2 * #(1 2 3) asPMVector. "a PMVector(2 4 6)"#(2) asPMVector * #(1 2 3) asPMVector. "Error"1-dimensional vectors are not scalars with respect to matrices, and in fact have undefined behavior:2 * (PMMatrix rows: #((1 2) (3 4))). "a PMVector(2 4) a PMVector(6 8)"#(2) asPMVector * (PMMatrix rows: #((1 2) (3 4))). "a PMVector(2 4)"n-dimensional vectors are row vectors when right-multiplied by an nx1 matrix, but column vectors when left multiplied by a matrix 1xn, meaning they cannot be used as either a 1xn or an nx1 matrix in code that expects one or the other: the dimensions will be unpredictable.#(1 2) asPMVector * (PMMatrix rows: #((1) (2))). "a PMVector(5)"(PMMatrix rows: #((1 2))) * #(1 2) asPMVector. "a PMVector(5)"1x1 matrices are not scalars with respect to vectors:(PMMatrix rows: #((2))) * #(1 2) asPMVector. "Error"1xn * nx1 matrix multiplication produces a matrix, not a scalar, which behaves differently than a scalar, as per above:(PMMatrix rows: #((1 2))) * (PMMatrix rows: #((1) (2))) "a PMVector(5) <-- actually a 1x1 matrix"As long as one works only with scalars and vectors OR scalars and matrices, things seem fine. It seems like maybe either matrix-vector operations should throw errors, or vectors should behave consistently as an nx1 matrix during matrix math. It may also make sense to make the 1-dimensional vectors and matrices either just convert to scalar Numbers, or make them polymorphic with #= etc, but I'm not sure. -- You received this message because you are subscribed to the Google Groups "SciSmalltalk" group. To unsubscribe from this group and stop receiving emails from it, send an email to [hidden email]. For more options, visit https://groups.google.com/d/optout. -- You received this message because you are subscribed to the Google Groups "SciSmalltalk" group. To unsubscribe from this group and stop receiving emails from it, send an email to [hidden email]. For more options, visit https://groups.google.com/d/optout.
Open this post in threaded view
|

## Re: Vectors have an inconsistent interpretation in matrix math

Open this post in threaded view
|

## Re: Vectors have an inconsistent interpretation in matrix math

Open this post in threaded view
|

## Re: Vectors have an inconsistent interpretation in matrix math

Open this post in threaded view
|

## Re: Vectors have an inconsistent interpretation in matrix math

 On Mon, May 22, 2017 at 12:34 AM, Apocalypse Mystic wrote:This is why, to me, suggesting that 1-vectors should just coerce to Numbers made sense.Hi Evan,ok, i'll say something about this; i'm not sure i understand your suggestion correctly, but i will try to explain the problem anyway. you say " we might expect 1-dimensional vectors to act like scalars and nx1-dimensional matrices to act like vectors".  i understand this that way: if i have a 1x1matrix, then this should act like a 1-dim vector and as such act like a scalar. since i can do 'ascalar * amatrix', i can then expect that 'a1x1matrix * a42x43Matrix' is a valid expression. well, perhaps i misunderstand it? take for example the rank concept: there  exists eg this theorem (eghttps://en.wikipedia.org/wiki/Rank_(linear_algebra) under properties) : the rank of a matrix product cannot exceed the rank of any factor. this theorem would fall overboard in your algebra. what would you do with this, throw the whole rank concept away, let the theorem fall by the wayside or change the definition of rank? if the last one, how? rank can be helpful when argumenting about linear independence, you'd need to think about this too (and quite a few other things). and im sure it is just _one_ example how this could change, how math operates, i'm just too lazy to find others.its the same thing with vectors: you seem not to like '#(2) asPMVector * #(1 2 3) asPMVector. "Error"', and it is my understanding (?) that you would prefer 'a PMVector(2 4 6)' as result instead. think block matrices and build a matrix out of several vectors. you'll get _exactly_ the same problems with your result. (just in case my assumption, that 1x1matrix * a42x43Matrix is a valid expression, was wrong.)or iow, changing the algebra of matrices in a computer language, that is intended for general use, could mean trouble; please be careful with such ideas.werner -- You received this message because you are subscribed to the Google Groups "SciSmalltalk" group. To unsubscribe from this group and stop receiving emails from it, send an email to [hidden email]. For more options, visit https://groups.google.com/d/optout.
Open this post in threaded view
|

## Re: Vectors have an inconsistent interpretation in matrix math

Open this post in threaded view
|