Skip to content

Add rows to matrix / Add element to Ndarray: shouldn't it add it to the existing object instead of returning a new one ? #14

@MatthiasRMS

Description

@MatthiasRMS

I'm experimenting with a new Ndarray class and while creating a class method add_element, I took a look at how the add_rows method was coded for the Matrix class, and I noticed that it wasn't modifying the object, but was returning a new one.

Instinctively, I would expect that I could do:

matrix = Crystalla::Matrix.new([1.0, 2.0, 2.0, 3.0], 2, 2)
p matrix
>> Matrix[[ 1.0, 2.0 ],
       [ 2.0, 3.0 ]]
matrix.add_rows(1, Crystalla::Matrix.new([4.5, 3.0], 1,2))
p matrix 
>> Matrix[[ 1.0, 2.0 ],
       [ 4.5, 3.0 ],
       [ 2.0, 3.0 ]]

Instead, I need to assign it to a new instance to get the modified matrix:
matrix2 = matrix.add_rows(1, Crystalla::Matrix.new([4.5, 3.0], 1,2))

Is there a particular reason why you made this choice ?
For Ndarrays, it would seem more natural to do:

array = Crystalla::Ndarray.new([1.0, 2.0, 2.0, 3.0])
array.add_element(6.0, 1)
p array 
>> Ndarray [1.0, 6.0, 2.0, 2.0, 3.0]

Let me know what you think !

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions