Fast diagonal matrix creation from two lists
up vote
2
down vote
favorite
Given 2 lists I would like to create a Diagonal matrix
.
One list will fill the diagonal-constant
and the other will fill the matrix.
For example:
fast_matrix([1,2], [6,7,8])
Should output 2 matrices:
[2] # unused
[1, 6, 7, 8]
[6, 1, 7, 8]
[6, 7, 1, 8]
[6, 7, 8, 1]
and
[1] # unused
[2, 6, 7, 8]
[6, 2, 7, 8]
[6, 7, 2, 8]
[6, 7, 8, 2]
My code makes 10000 transformations in 2.5secs in my pc.
from pprint import pprint
import timeit
def not_so_fast_matrix(A, B):
rt_obj =
for i,_ in enumerate(A):
for z in range(len(B) + 1):
new_from = A.copy()
new_from.remove(A[i])
new_list = B.copy()
new_list.insert(z, A[i])
rt_obj.append({'remain': new_from, 'to_list': new_list})
return rt_obj
# pprint(not_so_fast_matrix([1,2], [6,7,8]))
A = ([1,2,3,4,5,6,7,8,9,10])
B = ([60,70,80,90,100,200,300])
t = timeit.Timer(lambda: not_so_fast_toeplitz(A, B))
print("not_so_fast_matrix took: {:.3f}secs for 10000 iterations".format(t.timeit(number=10000)))
I was wondering if it can go faster using another approach.
Circulant
from scipy.linalg
looks like what I'm after but without rolling:from scipy.linalg import circulant
print(circulant([1, 8,7,6])) # <- Should be inverted
outputs:
[[1 6 7 8]
[8 1 6 7]
[7 8 1 6]
[6 7 8 1]]
Elements were shifted (pushed) right.
python python-3.x algorithm linear-algebra
|
show 5 more comments
up vote
2
down vote
favorite
Given 2 lists I would like to create a Diagonal matrix
.
One list will fill the diagonal-constant
and the other will fill the matrix.
For example:
fast_matrix([1,2], [6,7,8])
Should output 2 matrices:
[2] # unused
[1, 6, 7, 8]
[6, 1, 7, 8]
[6, 7, 1, 8]
[6, 7, 8, 1]
and
[1] # unused
[2, 6, 7, 8]
[6, 2, 7, 8]
[6, 7, 2, 8]
[6, 7, 8, 2]
My code makes 10000 transformations in 2.5secs in my pc.
from pprint import pprint
import timeit
def not_so_fast_matrix(A, B):
rt_obj =
for i,_ in enumerate(A):
for z in range(len(B) + 1):
new_from = A.copy()
new_from.remove(A[i])
new_list = B.copy()
new_list.insert(z, A[i])
rt_obj.append({'remain': new_from, 'to_list': new_list})
return rt_obj
# pprint(not_so_fast_matrix([1,2], [6,7,8]))
A = ([1,2,3,4,5,6,7,8,9,10])
B = ([60,70,80,90,100,200,300])
t = timeit.Timer(lambda: not_so_fast_toeplitz(A, B))
print("not_so_fast_matrix took: {:.3f}secs for 10000 iterations".format(t.timeit(number=10000)))
I was wondering if it can go faster using another approach.
Circulant
from scipy.linalg
looks like what I'm after but without rolling:from scipy.linalg import circulant
print(circulant([1, 8,7,6])) # <- Should be inverted
outputs:
[[1 6 7 8]
[8 1 6 7]
[7 8 1 6]
[6 7 8 1]]
Elements were shifted (pushed) right.
python python-3.x algorithm linear-algebra
Are you aware ofscipy.linalg.toeplitz
?
– Warren Weckesser
Nov 21 at 20:24
Yes, but for whatever reason it was slower
– Panos Kal.
Nov 21 at 20:25
Have you tried it using SciPy version 1.1.0? Some changes were made in that release that improved the performance oftoeplitz
.
– Warren Weckesser
Nov 21 at 20:30
1
You can take a look at the source code on github. Your function returns a list of dictionaries, whilescipy.linalg.toeplitz
returns a numpy array, sotoeplitz
won't be a direct replacement for your function.
– Warren Weckesser
Nov 21 at 20:37
1
Hint: transposed and flattened matrix data is structured. Creation can be done in reverse direction. Something like repeat() and reshape() while taking a care about diagonal and one missing element.
– Ante
Nov 22 at 21:35
|
show 5 more comments
up vote
2
down vote
favorite
up vote
2
down vote
favorite
Given 2 lists I would like to create a Diagonal matrix
.
One list will fill the diagonal-constant
and the other will fill the matrix.
For example:
fast_matrix([1,2], [6,7,8])
Should output 2 matrices:
[2] # unused
[1, 6, 7, 8]
[6, 1, 7, 8]
[6, 7, 1, 8]
[6, 7, 8, 1]
and
[1] # unused
[2, 6, 7, 8]
[6, 2, 7, 8]
[6, 7, 2, 8]
[6, 7, 8, 2]
My code makes 10000 transformations in 2.5secs in my pc.
from pprint import pprint
import timeit
def not_so_fast_matrix(A, B):
rt_obj =
for i,_ in enumerate(A):
for z in range(len(B) + 1):
new_from = A.copy()
new_from.remove(A[i])
new_list = B.copy()
new_list.insert(z, A[i])
rt_obj.append({'remain': new_from, 'to_list': new_list})
return rt_obj
# pprint(not_so_fast_matrix([1,2], [6,7,8]))
A = ([1,2,3,4,5,6,7,8,9,10])
B = ([60,70,80,90,100,200,300])
t = timeit.Timer(lambda: not_so_fast_toeplitz(A, B))
print("not_so_fast_matrix took: {:.3f}secs for 10000 iterations".format(t.timeit(number=10000)))
I was wondering if it can go faster using another approach.
Circulant
from scipy.linalg
looks like what I'm after but without rolling:from scipy.linalg import circulant
print(circulant([1, 8,7,6])) # <- Should be inverted
outputs:
[[1 6 7 8]
[8 1 6 7]
[7 8 1 6]
[6 7 8 1]]
Elements were shifted (pushed) right.
python python-3.x algorithm linear-algebra
Given 2 lists I would like to create a Diagonal matrix
.
One list will fill the diagonal-constant
and the other will fill the matrix.
For example:
fast_matrix([1,2], [6,7,8])
Should output 2 matrices:
[2] # unused
[1, 6, 7, 8]
[6, 1, 7, 8]
[6, 7, 1, 8]
[6, 7, 8, 1]
and
[1] # unused
[2, 6, 7, 8]
[6, 2, 7, 8]
[6, 7, 2, 8]
[6, 7, 8, 2]
My code makes 10000 transformations in 2.5secs in my pc.
from pprint import pprint
import timeit
def not_so_fast_matrix(A, B):
rt_obj =
for i,_ in enumerate(A):
for z in range(len(B) + 1):
new_from = A.copy()
new_from.remove(A[i])
new_list = B.copy()
new_list.insert(z, A[i])
rt_obj.append({'remain': new_from, 'to_list': new_list})
return rt_obj
# pprint(not_so_fast_matrix([1,2], [6,7,8]))
A = ([1,2,3,4,5,6,7,8,9,10])
B = ([60,70,80,90,100,200,300])
t = timeit.Timer(lambda: not_so_fast_toeplitz(A, B))
print("not_so_fast_matrix took: {:.3f}secs for 10000 iterations".format(t.timeit(number=10000)))
I was wondering if it can go faster using another approach.
Circulant
from scipy.linalg
looks like what I'm after but without rolling:from scipy.linalg import circulant
print(circulant([1, 8,7,6])) # <- Should be inverted
outputs:
[[1 6 7 8]
[8 1 6 7]
[7 8 1 6]
[6 7 8 1]]
Elements were shifted (pushed) right.
python python-3.x algorithm linear-algebra
python python-3.x algorithm linear-algebra
edited Nov 22 at 16:45
asked Nov 21 at 19:48
Panos Kal.
8,11784261
8,11784261
Are you aware ofscipy.linalg.toeplitz
?
– Warren Weckesser
Nov 21 at 20:24
Yes, but for whatever reason it was slower
– Panos Kal.
Nov 21 at 20:25
Have you tried it using SciPy version 1.1.0? Some changes were made in that release that improved the performance oftoeplitz
.
– Warren Weckesser
Nov 21 at 20:30
1
You can take a look at the source code on github. Your function returns a list of dictionaries, whilescipy.linalg.toeplitz
returns a numpy array, sotoeplitz
won't be a direct replacement for your function.
– Warren Weckesser
Nov 21 at 20:37
1
Hint: transposed and flattened matrix data is structured. Creation can be done in reverse direction. Something like repeat() and reshape() while taking a care about diagonal and one missing element.
– Ante
Nov 22 at 21:35
|
show 5 more comments
Are you aware ofscipy.linalg.toeplitz
?
– Warren Weckesser
Nov 21 at 20:24
Yes, but for whatever reason it was slower
– Panos Kal.
Nov 21 at 20:25
Have you tried it using SciPy version 1.1.0? Some changes were made in that release that improved the performance oftoeplitz
.
– Warren Weckesser
Nov 21 at 20:30
1
You can take a look at the source code on github. Your function returns a list of dictionaries, whilescipy.linalg.toeplitz
returns a numpy array, sotoeplitz
won't be a direct replacement for your function.
– Warren Weckesser
Nov 21 at 20:37
1
Hint: transposed and flattened matrix data is structured. Creation can be done in reverse direction. Something like repeat() and reshape() while taking a care about diagonal and one missing element.
– Ante
Nov 22 at 21:35
Are you aware of
scipy.linalg.toeplitz
?– Warren Weckesser
Nov 21 at 20:24
Are you aware of
scipy.linalg.toeplitz
?– Warren Weckesser
Nov 21 at 20:24
Yes, but for whatever reason it was slower
– Panos Kal.
Nov 21 at 20:25
Yes, but for whatever reason it was slower
– Panos Kal.
Nov 21 at 20:25
Have you tried it using SciPy version 1.1.0? Some changes were made in that release that improved the performance of
toeplitz
.– Warren Weckesser
Nov 21 at 20:30
Have you tried it using SciPy version 1.1.0? Some changes were made in that release that improved the performance of
toeplitz
.– Warren Weckesser
Nov 21 at 20:30
1
1
You can take a look at the source code on github. Your function returns a list of dictionaries, while
scipy.linalg.toeplitz
returns a numpy array, so toeplitz
won't be a direct replacement for your function.– Warren Weckesser
Nov 21 at 20:37
You can take a look at the source code on github. Your function returns a list of dictionaries, while
scipy.linalg.toeplitz
returns a numpy array, so toeplitz
won't be a direct replacement for your function.– Warren Weckesser
Nov 21 at 20:37
1
1
Hint: transposed and flattened matrix data is structured. Creation can be done in reverse direction. Something like repeat() and reshape() while taking a care about diagonal and one missing element.
– Ante
Nov 22 at 21:35
Hint: transposed and flattened matrix data is structured. Creation can be done in reverse direction. Something like repeat() and reshape() while taking a care about diagonal and one missing element.
– Ante
Nov 22 at 21:35
|
show 5 more comments
1 Answer
1
active
oldest
votes
up vote
1
down vote
accepted
Transposed and flatten matrix has a structure where elements from B are repeated. This approach uses that property to create blueprint of matrix with wrong values on diagonal, and than fill diagonal with correct value.
import numpy
def create_matrices(A, B):
# Create blueprint of result matrix
n = len(B) + 1
b = numpy.empty((n * n,), dtype=numpy.int32)
b[:-1] = numpy.repeat(B, n + 1)
b = b.reshape((n, n)).T # <- added transposition
# Change diagonal elements
for a in A:
m = b.copy()
numpy.fill_diagonal(m, a)
print(m)
create_matrices([1, 2], [6, 7, 8])
Thanks Ante. There is a problem with the output though. It returns [[1 6 6 6] [6 1 7 7] [7 7 1 8] [8 8 8 1]] when it should return [[1 6 7 8] [6 1 7 8] [6 7 1 8] [6 7 8 1]]
– Panos Kal.
Nov 23 at 10:36
@PanosKal. I forget to add transposition. Check the line b = b.reshape...
– Ante
Nov 23 at 10:39
add a comment |
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
1
down vote
accepted
Transposed and flatten matrix has a structure where elements from B are repeated. This approach uses that property to create blueprint of matrix with wrong values on diagonal, and than fill diagonal with correct value.
import numpy
def create_matrices(A, B):
# Create blueprint of result matrix
n = len(B) + 1
b = numpy.empty((n * n,), dtype=numpy.int32)
b[:-1] = numpy.repeat(B, n + 1)
b = b.reshape((n, n)).T # <- added transposition
# Change diagonal elements
for a in A:
m = b.copy()
numpy.fill_diagonal(m, a)
print(m)
create_matrices([1, 2], [6, 7, 8])
Thanks Ante. There is a problem with the output though. It returns [[1 6 6 6] [6 1 7 7] [7 7 1 8] [8 8 8 1]] when it should return [[1 6 7 8] [6 1 7 8] [6 7 1 8] [6 7 8 1]]
– Panos Kal.
Nov 23 at 10:36
@PanosKal. I forget to add transposition. Check the line b = b.reshape...
– Ante
Nov 23 at 10:39
add a comment |
up vote
1
down vote
accepted
Transposed and flatten matrix has a structure where elements from B are repeated. This approach uses that property to create blueprint of matrix with wrong values on diagonal, and than fill diagonal with correct value.
import numpy
def create_matrices(A, B):
# Create blueprint of result matrix
n = len(B) + 1
b = numpy.empty((n * n,), dtype=numpy.int32)
b[:-1] = numpy.repeat(B, n + 1)
b = b.reshape((n, n)).T # <- added transposition
# Change diagonal elements
for a in A:
m = b.copy()
numpy.fill_diagonal(m, a)
print(m)
create_matrices([1, 2], [6, 7, 8])
Thanks Ante. There is a problem with the output though. It returns [[1 6 6 6] [6 1 7 7] [7 7 1 8] [8 8 8 1]] when it should return [[1 6 7 8] [6 1 7 8] [6 7 1 8] [6 7 8 1]]
– Panos Kal.
Nov 23 at 10:36
@PanosKal. I forget to add transposition. Check the line b = b.reshape...
– Ante
Nov 23 at 10:39
add a comment |
up vote
1
down vote
accepted
up vote
1
down vote
accepted
Transposed and flatten matrix has a structure where elements from B are repeated. This approach uses that property to create blueprint of matrix with wrong values on diagonal, and than fill diagonal with correct value.
import numpy
def create_matrices(A, B):
# Create blueprint of result matrix
n = len(B) + 1
b = numpy.empty((n * n,), dtype=numpy.int32)
b[:-1] = numpy.repeat(B, n + 1)
b = b.reshape((n, n)).T # <- added transposition
# Change diagonal elements
for a in A:
m = b.copy()
numpy.fill_diagonal(m, a)
print(m)
create_matrices([1, 2], [6, 7, 8])
Transposed and flatten matrix has a structure where elements from B are repeated. This approach uses that property to create blueprint of matrix with wrong values on diagonal, and than fill diagonal with correct value.
import numpy
def create_matrices(A, B):
# Create blueprint of result matrix
n = len(B) + 1
b = numpy.empty((n * n,), dtype=numpy.int32)
b[:-1] = numpy.repeat(B, n + 1)
b = b.reshape((n, n)).T # <- added transposition
# Change diagonal elements
for a in A:
m = b.copy()
numpy.fill_diagonal(m, a)
print(m)
create_matrices([1, 2], [6, 7, 8])
edited Nov 23 at 10:39
answered Nov 23 at 10:25
Ante
4,35751942
4,35751942
Thanks Ante. There is a problem with the output though. It returns [[1 6 6 6] [6 1 7 7] [7 7 1 8] [8 8 8 1]] when it should return [[1 6 7 8] [6 1 7 8] [6 7 1 8] [6 7 8 1]]
– Panos Kal.
Nov 23 at 10:36
@PanosKal. I forget to add transposition. Check the line b = b.reshape...
– Ante
Nov 23 at 10:39
add a comment |
Thanks Ante. There is a problem with the output though. It returns [[1 6 6 6] [6 1 7 7] [7 7 1 8] [8 8 8 1]] when it should return [[1 6 7 8] [6 1 7 8] [6 7 1 8] [6 7 8 1]]
– Panos Kal.
Nov 23 at 10:36
@PanosKal. I forget to add transposition. Check the line b = b.reshape...
– Ante
Nov 23 at 10:39
Thanks Ante. There is a problem with the output though. It returns [[1 6 6 6] [6 1 7 7] [7 7 1 8] [8 8 8 1]] when it should return [[1 6 7 8] [6 1 7 8] [6 7 1 8] [6 7 8 1]]
– Panos Kal.
Nov 23 at 10:36
Thanks Ante. There is a problem with the output though. It returns [[1 6 6 6] [6 1 7 7] [7 7 1 8] [8 8 8 1]] when it should return [[1 6 7 8] [6 1 7 8] [6 7 1 8] [6 7 8 1]]
– Panos Kal.
Nov 23 at 10:36
@PanosKal. I forget to add transposition. Check the line b = b.reshape...
– Ante
Nov 23 at 10:39
@PanosKal. I forget to add transposition. Check the line b = b.reshape...
– Ante
Nov 23 at 10:39
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53419527%2ffast-diagonal-matrix-creation-from-two-lists%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Are you aware of
scipy.linalg.toeplitz
?– Warren Weckesser
Nov 21 at 20:24
Yes, but for whatever reason it was slower
– Panos Kal.
Nov 21 at 20:25
Have you tried it using SciPy version 1.1.0? Some changes were made in that release that improved the performance of
toeplitz
.– Warren Weckesser
Nov 21 at 20:30
1
You can take a look at the source code on github. Your function returns a list of dictionaries, while
scipy.linalg.toeplitz
returns a numpy array, sotoeplitz
won't be a direct replacement for your function.– Warren Weckesser
Nov 21 at 20:37
1
Hint: transposed and flattened matrix data is structured. Creation can be done in reverse direction. Something like repeat() and reshape() while taking a care about diagonal and one missing element.
– Ante
Nov 22 at 21:35