[RFC] diagonal/banded matrix construction

17 views
Skip to first unread message

Chris Smith

unread,
Mar 12, 2019, 2:14:26 AM3/12/19
to sympy
@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 would be appreciated.

Thanks,
 @smichr
Reply all
Reply to author
Forward
0 new messages