@GYeyosi and I have been working on a syntax for creating diagonal/banded
matrices which has the following enhancments:
Here is a reference using current syntax
>>> diag(Matrix([1, 2]), 3)
Matrix([
[1, 0],
[2, 0],
[0, 3]])
Changes include
1. an empty dictionary item, {}, is a directive to return to the diagonal
for filling rather than continuing from where the last element ended
>>> diag(Matrix([1, 2]), {}, 3)
Matrix([
[1, 0, 0],
[2, 0, 0],
[0, 0, 3]])
2. a kerning dictionary can be used to move the "pen" filling the matrix to
an absolute matrix address
>>> diag(Matrix([1, 2]), {'goto': (0, 1)}, 3)
Matrix([
[1, 3],
[2, 0]])
3. a kerning dictionary can be used to move the "pen" relative to the
current location with x-y deltas
>>> diag(Matrix([1, 2]), {'move': (1, 0)}, 3)
Matrix([
[1, 0],
[2, 3]])
4. a filling dictionary contains diagonal keys and the values indicate what
should be put on the corresponding diagonal.
Values can be expressions/constants, lists or functions
>>> diag({1:1, -2:3}, rows=5, cols=3) # positive diagonal are above the
main diagonal and negative below
Matrix([
[0, 1, 0],
[0, 0, 1],
[3, 0, 0],
[0, 3, 0],
[0, 0, 3]])
5. functions can take row, col args or diagonal arg'
>>> diag(-1, -2, {0: lambda d: 1+d**2}, rows=6, cols=6)
Matrix([
[-1, 0, 0, 0, 0, 0],
[ 0, -2, 0, 0, 0, 0],
[ 0, 0, 1, 0, 0, 0],
[ 0, 0, 0, 2, 0, 0],
[ 0, 0, 0, 0, 5, 0],
[ 0, 0, 0, 0, 0, 10]])
>>> diag(-1, -2, {0: lambda i,j: i+j}, rows=6, cols=6)
Matrix([
[-1, 0, 0, 0, 0, 0],
[ 0, -2, 0, 0, 0, 0],
[ 0, 0, 0, 0, 0, 0],
[ 0, 0, 0, 2, 0, 0],
[ 0, 0, 0, 0, 4, 0],
[ 0, 0, 0, 0, 0, 6]])
filling dictionaries are flexible in size and will only be as large as
necessary to hold their arguments. Here,
the list forces the dictionary entry to be 6 x 4 but the size can be
specified, too:
>>> diag(-1, -2, {0: lambda i,j: i+j, -2:[1,3,5,7]})
Matrix([
[-1, 0, 0, 0, 0, 0],
[ 0, -2, 0, 0, 0, 0],
[ 0, 0, 0, 0, 0, 0],
[ 0, 0, 0, 2, 0, 0],
[ 0, 0, 1, 0, 4, 0],
[ 0, 0, 0, 3, 0, 6],
[ 0, 0, 0, 0, 5, 0],
[ 0, 0, 0, 0, 0, 7]])
>>> diag(-1, -2, {0: lambda i,j: i+j, -2:[1,3,5,7], 'size':(6,6)})
Matrix([
[-1, 0, 0, 0, 0, 0, 0, 0],
[ 0, -2, 0, 0, 0, 0, 0, 0],
[ 0, 0, 0, 0, 0, 0, 0, 0],
[ 0, 0, 0, 2, 0, 0, 0, 0],
[ 0, 0, 1, 0, 4, 0, 0, 0],
[ 0, 0, 0, 3, 0, 6, 0, 0],
[ 0, 0, 0, 0, 5, 0, 8, 0],
[ 0, 0, 0, 0, 0, 7, 0, 10]])
Your feedback at PR16180 <https://github.com/sympy/sympy/pull/16180>would
be appreciated.
Thanks,
@smichr
--
You received this message because you are subscribed to the Google Groups
"sympy" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
To post to this group, send email to [email protected].
Visit this group at https://groups.google.com/group/sympy.
To view this discussion on the web visit
https://groups.google.com/d/msgid/sympy/a303d555-044b-4eda-b156-f78822c67a91%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.