2 added 138 characters in body
source | link

Edit: removed confusing code


Yes there is a shortcut, here's some Python code:


import numpy as np

d = 3
D = np.repeat(np.identity(d), d, axis=1)
print('D:')
print D
print

print('x:')
x = np.arange(d*d, dtype=float)
print x
print

print('D * x:')
print D.dot(x)
print

print('shortcut:')
print x.reshape((d, d)).sum(axis=1)
print



import numpy as np

d = 3


D = np.repeat(np.identity(d), d, axis=1)
print('D:')
print D
print


print('x:')
x = np.arange(d*d, dtype=float)
print x
print


print('D * x:')
print D.dot(x)
print


print('shortcut:')
print x.reshape((d, d)).sum(axis=1)
print

output:


D:
[[ 1.  1.  1.  0.  0.  0.  0.  0.  0.]
 [ 0.  0.  0.  1.  1.  1.  0.  0.  0.]
 [ 0.  0.  0.  0.  0.  0.  1.  1.  1.]]
D:
[[ 1.  1.  1.  0.  0.  0.  0.  0.  0.]
 [ 0.  0.  0.  1.  1.  1.  0.  0.  0.]
 [ 0.  0.  0.  0.  0.  0.  1.  1.  1.]]


x:
[ 0.  1.  2.  3.  4.  5.  6.  7.  8.]
 
D * x:
[  3.  12.  21.]
D * x:
[  3.  12.  21.]


shortcut:
[  3.  12.  21.]

Yes there is a shortcut, here's some Python code:


import numpy as np

d = 3
D = np.repeat(np.identity(d), d, axis=1)
print('D:')
print D
print

print('x:')
x = np.arange(d*d, dtype=float)
print x
print

print('D * x:')
print D.dot(x)
print

print('shortcut:')
print x.reshape((d, d)).sum(axis=1)
print

output:


D:
[[ 1.  1.  1.  0.  0.  0.  0.  0.  0.]
 [ 0.  0.  0.  1.  1.  1.  0.  0.  0.]
 [ 0.  0.  0.  0.  0.  0.  1.  1.  1.]]

x:
[ 0.  1.  2.  3.  4.  5.  6.  7.  8.]
 
D * x:
[  3.  12.  21.]

shortcut:
[  3.  12.  21.]

Edit: removed confusing code


Yes there is a shortcut, here's some Python code:



import numpy as np

d = 3


D = np.repeat(np.identity(d), d, axis=1)
print('D:')
print D
print


print('x:')
x = np.arange(d*d, dtype=float)
print x
print


print('D * x:')
print D.dot(x)
print


print('shortcut:')
print x.reshape((d, d)).sum(axis=1)
print

output:


D:
[[ 1.  1.  1.  0.  0.  0.  0.  0.  0.]
 [ 0.  0.  0.  1.  1.  1.  0.  0.  0.]
 [ 0.  0.  0.  0.  0.  0.  1.  1.  1.]]


x:
[ 0.  1.  2.  3.  4.  5.  6.  7.  8.]


D * x:
[  3.  12.  21.]


shortcut:
[  3.  12.  21.]
1
source | link

Yes there is a shortcut, here's some Python code:


import numpy as np

d = 3
D = np.repeat(np.identity(d), d, axis=1)
print('D:')
print D
print

print('x:')
x = np.arange(d*d, dtype=float)
print x
print

print('D * x:')
print D.dot(x)
print

print('shortcut:')
print x.reshape((d, d)).sum(axis=1)
print

output:

D:
[[ 1.  1.  1.  0.  0.  0.  0.  0.  0.]
 [ 0.  0.  0.  1.  1.  1.  0.  0.  0.]
 [ 0.  0.  0.  0.  0.  0.  1.  1.  1.]]

x:
[ 0.  1.  2.  3.  4.  5.  6.  7.  8.]

D * x:
[  3.  12.  21.]

shortcut:
[  3.  12.  21.]