Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added "append" function for matrices #22

Merged
merged 3 commits into from
Jul 17, 2018
Merged

Added "append" function for matrices #22

merged 3 commits into from
Jul 17, 2018

Conversation

anshuman23
Copy link
Owner

@josevalim, as per our discussion, added a function that can add a row to a matrix one at a time, similar to Python's list based append. Since this is a standalone functionality that can be used irrespective of the CSV loading function we are going to implement, I thought of pushing it separately. Also, this operation is fast, since old data is maintained and we just "push back" the new row at the end after reallocation of memory.

Some basic usage:

iex(1)> mat = Tensorflex.create_matrix(4,4,[[123,431,23,1],[1,2,3,4],[5,6,7,8],[768,564,44,5]])
%Tensorflex.Matrix{
  data: #Reference<0.3675359429.1377959937.238799>,
  ncols: 4,
  nrows: 4
}

iex(2)> mat = Tensorflex.append_to_matrix(mat, [[1,1,1,1]])
%Tensorflex.Matrix{
  data: #Reference<0.3675359429.1377959937.238799>,
  ncols: 4,
  nrows: 5
}

iex(3)> Tensorflex.matrix_to_lists mat
[
  [123.0, 431.0, 23.0, 1.0],
  [1.0, 2.0, 3.0, 4.0],
  [5.0, 6.0, 7.0, 8.0],
  [768.0, 564.0, 44.0, 5.0],
  [1.0, 1.0, 1.0, 1.0]
]

iex(4)> Tensorflex.matrix_pos(mat,5,3) 
1.0

Incorrect usage (having more than one row, or lower/higher number of columns than the original matrix) will raise:

iex(5)> mat = Tensorflex.append_to_matrix(mat, [[1,2]])
** (ArgumentError) data columns must be same as matrix and number of rows must be 1
    (tensorflex) lib/tensorflex.ex:46: Tensorflex.append_to_matrix/2

iex(5)> mat = Tensorflex.append_to_matrix(mat, [[1,1,1,1],[2,2,2,2]])
** (ArgumentError) data columns must be same as matrix and number of rows must be 1
    (tensorflex) lib/tensorflex.ex:46: Tensorflex.append_to_matrix/2

@anshuman23 anshuman23 merged commit f9962d5 into master Jul 17, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

1 participant