Fast diagonal matrix creation from two lists












2















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.










share|improve this question

























  • Are you aware of scipy.linalg.toeplitz?

    – Warren Weckesser
    Nov 21 '18 at 20:24











  • Yes, but for whatever reason it was slower

    – Panos Kal.
    Nov 21 '18 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 '18 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, so toeplitz won't be a direct replacement for your function.

    – Warren Weckesser
    Nov 21 '18 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 '18 at 21:35
















2















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.










share|improve this question

























  • Are you aware of scipy.linalg.toeplitz?

    – Warren Weckesser
    Nov 21 '18 at 20:24











  • Yes, but for whatever reason it was slower

    – Panos Kal.
    Nov 21 '18 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 '18 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, so toeplitz won't be a direct replacement for your function.

    – Warren Weckesser
    Nov 21 '18 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 '18 at 21:35














2












2








2








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.










share|improve this question
















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






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 22 '18 at 16:45







Panos Kal.

















asked Nov 21 '18 at 19:48









Panos Kal.Panos Kal.

8,27984361




8,27984361













  • Are you aware of scipy.linalg.toeplitz?

    – Warren Weckesser
    Nov 21 '18 at 20:24











  • Yes, but for whatever reason it was slower

    – Panos Kal.
    Nov 21 '18 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 '18 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, so toeplitz won't be a direct replacement for your function.

    – Warren Weckesser
    Nov 21 '18 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 '18 at 21:35



















  • Are you aware of scipy.linalg.toeplitz?

    – Warren Weckesser
    Nov 21 '18 at 20:24











  • Yes, but for whatever reason it was slower

    – Panos Kal.
    Nov 21 '18 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 '18 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, so toeplitz won't be a direct replacement for your function.

    – Warren Weckesser
    Nov 21 '18 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 '18 at 21:35

















Are you aware of scipy.linalg.toeplitz?

– Warren Weckesser
Nov 21 '18 at 20:24





Are you aware of scipy.linalg.toeplitz?

– Warren Weckesser
Nov 21 '18 at 20:24













Yes, but for whatever reason it was slower

– Panos Kal.
Nov 21 '18 at 20:25





Yes, but for whatever reason it was slower

– Panos Kal.
Nov 21 '18 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 '18 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 '18 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 '18 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 '18 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 '18 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 '18 at 21:35












1 Answer
1






active

oldest

votes


















1














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])





share|improve this answer


























  • 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 '18 at 10:36













  • @PanosKal. I forget to add transposition. Check the line b = b.reshape...

    – Ante
    Nov 23 '18 at 10:39











Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















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

























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes









1














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])





share|improve this answer


























  • 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 '18 at 10:36













  • @PanosKal. I forget to add transposition. Check the line b = b.reshape...

    – Ante
    Nov 23 '18 at 10:39
















1














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])





share|improve this answer


























  • 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 '18 at 10:36













  • @PanosKal. I forget to add transposition. Check the line b = b.reshape...

    – Ante
    Nov 23 '18 at 10:39














1












1








1







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])





share|improve this answer















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])






share|improve this answer














share|improve this answer



share|improve this answer








edited Nov 23 '18 at 10:39

























answered Nov 23 '18 at 10:25









AnteAnte

4,38251942




4,38251942













  • 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 '18 at 10:36













  • @PanosKal. I forget to add transposition. Check the line b = b.reshape...

    – Ante
    Nov 23 '18 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 '18 at 10:36













  • @PanosKal. I forget to add transposition. Check the line b = b.reshape...

    – Ante
    Nov 23 '18 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 '18 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 '18 at 10:36















@PanosKal. I forget to add transposition. Check the line b = b.reshape...

– Ante
Nov 23 '18 at 10:39





@PanosKal. I forget to add transposition. Check the line b = b.reshape...

– Ante
Nov 23 '18 at 10:39


















draft saved

draft discarded




















































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.




draft saved


draft discarded














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





















































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







Popular posts from this blog

Create new schema in PostgreSQL using DBeaver

Deepest pit of an array with Javascript: test on Codility

Costa Masnaga