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

Block-diagonal matrix generation #366

Merged
merged 1 commit into from Jun 3, 2015
Merged

Conversation

agisga
Copy link

@agisga agisga commented May 26, 2015

Generate a NMatrix with user-specified square matrices as blocks along its diagonal.
(I wrote such a method for my GSoC project to use for the generation of random effects covariance matrices.)

Example:

a = NMatrix.new([2,2],[1,2,3,4])
b = NMatrix.new([1,1],[123],dtype: :int32)
c = NMatrix.new([3,3],[1,2,3,1,2,3,1,2,3], dtype: :float64)
m = NMatrix.block_diagonal(a,b,c,dtype: :int64, stype: :yale)
    #      => 
    #      [
    #        [1, 2,   0, 0, 0, 0]
    #        [3, 4,   0, 0, 0, 0]
    #        [0, 0, 123, 0, 0, 0]
    #        [0, 0,   0, 1, 2, 3]
    #        [0, 0,   0, 1, 2, 3]
    #        [0, 0,   0, 1, 2, 3]
    #      ]

# a = NMatrix.new([2,2],[1,2,3,4])
# b = NMatrix.new([1,1],[123],dtype: :int32)
# c = NMatrix.new([3,3],[1,2,3,1,2,3,1,2,3], dtype: :float64)
# m = NMatrix.block_diagonal(a,b,c,dtype: :int64, stype: :yale)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fix the spaces in this comment, i.e.:

a = NMatrix.new([2, 2], [1, 2, 3, 4])
b = NMatrix.new([1, 1], [123], dtype: :int32)
c = NMatrix.new([3, 3], [1, 2, 3, 1, 2, 3, 1, 2, 3], dtype: :float64)
m = NMatrix.block_diagonal(a, b, c, dtype: :int64, stype: :yale)

@translunar
Copy link
Member

After you fix the line notes, could you please rebase into a single commit?

NMatrix#block_diagonal added

Arrays as input to #block_diagonal

Fixed line notes; and also allowed single numbers as arguments for #block_diagonal
@agisga
Copy link
Author

agisga commented May 30, 2015

Additionally to what we have discussed, I have added the possibility to specify the contents of the matrix with single numbers. For example:

NMatrix.block_diag(1, 2.0, 3, [[4,5], [6,7]], dtype: :byte)
# => 
# [ [1, 0, 0, 0, 0]
#   [0, 2, 0, 0, 0]
#   [0, 0, 3, 0, 0]
#   [0, 0, 0, 4, 5]
#   [0, 0, 0, 6, 7] ]

translunar added a commit that referenced this pull request Jun 3, 2015
Block-diagonal matrix generation
@translunar translunar merged commit b6c6a87 into SciRuby:master Jun 3, 2015
@agisga agisga mentioned this pull request Jul 21, 2015
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

3 participants