Thread: Matrix behaviour: assignment to scalar

  1. #1
    Registered User Sir Galahad's Avatar
    Join Date
    Nov 2016
    Location
    The Round Table
    Posts
    174

    Question Matrix behaviour: assignment to scalar

    I'm working on a little matrix library. But now I'm faced with a tricky interface question.

    Assigning a matrix to a scalar, does it make more sense to

    (A) set every element of the matrix to that value?

    (B) set the diagonals to that value, and the rest to zero?

    From a mathematical standpoint (B) seems logical. But thinking of actual use cases (A) might make better sense.

    Or maybe do (B) for square matrices and (A) for non-square ones? I don't know, that could be confusing too!
    The power of null.

  2. #2
    null pointer Structure's Avatar
    Join Date
    May 2019
    Posts
    303

    Thumbs up

    multiply the identity matrix by the scalar.

    "The term scalar multiplication refers to the product of a real number and a matrix. In scalar multiplication, each entry in the matrix is multiplied by the given scalar."

    Identity matrix - Wikipedia
    Last edited by Structure; 09-12-2020 at 11:12 AM.
    "without goto we would be wtf'd"

  3. #3
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,001
    Wait, you mean assign a scalar to a matrix, not assign a matrix to a scalar, right?

    If so, I think you should go with A since you noted it is more sensible for actual use cases. You shouldn't make the behaviour different for square matrices because it will be surprising to someone who encountered it previously for non-square matrices.

    However, that you can see two reasonable alternative interpretations for what assignment of a scalar means could indicate that it is best for both interpretations to be named functions so as to avoid misinterpretation.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  4. #4
    Registered User Sir Galahad's Avatar
    Join Date
    Nov 2016
    Location
    The Round Table
    Posts
    174
    Quote Originally Posted by laserlight View Post
    Wait, you mean assign a scalar to a matrix, not assign a matrix to a scalar, right?
    I guess to be semantically correct it really should be "assigning a variable a value" or "assigning a value to a variable". So yes, you are correct, assign a scalar to a matrix.


    Quote Originally Posted by laserlight View Post

    I think you should go with A since you noted it is more sensible for actual use cases. You shouldn't make the behaviour different for square matrices because it will be surprising to someone who encountered it previously for non-square matrices.
    Right, the principle of least surprise. Fair enough.

    Quote Originally Posted by laserlight View Post
    However, that you can see two reasonable alternative interpretations for what assignment of a scalar means could indicate that it is best for both interpretations to be named functions so as to avoid misinterpretation.
    Maybe something like this then:

    1) Constructor takes an optional boolean; if true (the default) all elements are set, otherwise just the diagonals.
    2) Assignment operator sets all elements to the value.
    3) A member function will be provided to create a scalar matrix.

    Seems pretty reasonable, no?
    The power of null.

  5. #5
    Registered User Sir Galahad's Avatar
    Join Date
    Nov 2016
    Location
    The Round Table
    Posts
    174
    Having thought about it for a while now, I've finally decided to simply stick with the old behaviour. It's just more consistent, mathematically. I did of course add a fill() member function for cases where each element should be set to the same value.

    All in all though I think it came out pretty well. It handles most of the basic operations, solves equations and the like, and can even be "attached" to existing stack variables (an array of doubles, for example).

    If anyone's interested in playing with it, let me know, I'll post the code on Github.
    The power of null.

  6. #6
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,001
    Quote Originally Posted by Sir Galahad
    1) Constructor takes an optional boolean; if true (the default) all elements are set, otherwise just the diagonals.
    2) Assignment operator sets all elements to the value.
    3) A member function will be provided to create a scalar matrix.
    I saw this over the weekend but got a bit distracted and forgot to reply. I was going to say that seems fine to me, although 3 seems to be the same as 1 with the boolean set to false, but maybe you meant "set a scalar matrix" rather than creating a new one.

    Quote Originally Posted by Sir Galahad
    Having thought about it for a while now, I've finally decided to simply stick with the old behaviour. It's just more consistent, mathematically. I did of course add a fill() member function for cases where each element should be set to the same value.
    Ah, the implication being that the old behaviour is B, so even though a scalar matrix isn't a scalar, by association it sounds close enough to be "more consistent, mathematically"? I think that works too, except that then you'll have to forbid assignment of a scalar to a non-square matrix, and if you do have a constructor that takes a scalar, it would be consistent for that scalar to populate a square matrix by default, which could make it a little odd to handle for non-square matrices (e.g., do you make it a must for a non-square matrix have the boolean set, or do you default to fill instead if the matrix is non-square, even though this could lead to surprises).
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  7. #7
    Registered User Sir Galahad's Avatar
    Join Date
    Nov 2016
    Location
    The Round Table
    Posts
    174
    Quote Originally Posted by laserlight View Post
    I saw this over the weekend but got a bit distracted and forgot to reply. I was going to say that seems fine to me, although 3 seems to be the same as 1 with the boolean set to false, but maybe you meant "set a scalar matrix" rather than creating a new one.


    Ah, the implication being that the old behaviour is B, so even though a scalar matrix isn't a scalar, by association it sounds close enough to be "more consistent, mathematically"? I think that works too, except that then you'll have to forbid assignment of a scalar to a non-square matrix, and if you do have a constructor that takes a scalar, it would be consistent for that scalar to populate a square matrix by default, which could make it a little odd to handle for non-square matrices (e.g., do you make it a must for a non-square matrix have the boolean set, or do you default to fill instead if the matrix is non-square, even though this could lead to surprises).

    Looking at other libraries now, filling does indeed seem to be a more typical interface. So maybe you're right. I'll give it another go...
    The power of null.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 2
    Last Post: 05-19-2014, 07:32 PM
  2. Replies: 11
    Last Post: 07-27-2013, 08:11 AM
  3. *operator overloading: scalar matrix multiplication
    By gemini_shooter in forum C++ Programming
    Replies: 4
    Last Post: 06-08-2009, 01:14 PM
  4. values assignment to matrix pointer
    By ronenk in forum C Programming
    Replies: 8
    Last Post: 03-01-2005, 11:30 AM
  5. allocation on heap, scalar/non scalar types
    By curlious in forum C++ Programming
    Replies: 10
    Last Post: 12-30-2003, 05:16 AM

Tags for this Thread