2021-06-04 20:22:43 +0200 received badge ● Famous Question (source) 2021-03-12 13:36:46 +0200 received badge ● Notable Question (source) 2021-03-12 13:36:46 +0200 received badge ● Popular Question (source) 2020-11-09 03:29:59 +0200 received badge ● Notable Question (source) 2020-05-24 08:53:03 +0200 received badge ● Famous Question (source) 2020-03-06 12:22:02 +0200 received badge ● Famous Question (source) 2019-12-09 10:06:50 +0200 received badge ● Popular Question (source) 2019-10-20 10:47:02 +0200 received badge ● Famous Question (source) 2019-01-13 22:45:14 +0200 received badge ● Popular Question (source) 2019-01-13 22:45:14 +0200 received badge ● Notable Question (source) 2018-12-16 11:23:45 +0200 received badge ● Notable Question (source) 2018-10-26 19:14:03 +0200 received badge ● Notable Question (source) 2018-09-30 07:48:52 +0200 received badge ● Popular Question (source) 2018-06-15 21:47:41 +0200 received badge ● Popular Question (source) 2018-02-11 18:56:11 +0200 asked a question iteration of a matrix Hello community, I have the following question. I have a matrix defined as K. And its diagonal defined, for instance, as z = [1, 2, 3, 4, 5]. I need to change each entry of a diagonal one by one, and then check a condition using 'if' statement. For example, change z to [2, 2, 3, 4, 5] and then [1, 3, 3, 4, 5] and then [1, 2, 4, 4, 5] (increasing each element by 1) Now I have a code : for i in range(16): K1 = K K1[i,i] = K[i,i] + 1 V1 = p1 * K1 * p1t V2 = p2 * K1 * p2t if V2 < V1: print i  So the first three rows are more interesting for the problem. The problem is, this code changes each entry, not returning the previous one to the initial position. So it changes the diagonal as following: [1, 2, 3, 4, 5] to [2, 2, 3, 4, 5] to [2, 3, 3, 4, 5] to [2, 3, 4, 4, 5] etc. How can I modify this code to correct the problem? Thank you. 2017-12-13 15:08:16 +0200 asked a question saving to a .csv file In my computations I got a list of strings: vect = ['00011', '00010', '01011', '01111', '01100']  I need to save it as .csv file, each element row by row, ideally like this: 00011 00010 01011 01111 01100  I use a code: import csv C = vect with open('file_path_vec.csv', 'w') as f: c = csv.writer(f) c.writerows(C)  But what I get in my .csv looks like this: 0,0,0,1,1 0,0,0,1,0 0,1,0,1,1 0,1,1,1,1 0,1,1,0,0  Why does it add commas in between? How can I get rid of them? 2017-12-11 19:36:46 +0200 commented answer Keys of a dictionary Thank you, it works better now. I think the problem was because of the keys being matrices. Why do we need this dictionary? I need to find minimal values of my function and then find the corresponding vectors for those minimal values. Initially I thought, it could be easy to just define a dictionary like this and find corresponding vectors this way. 2017-12-11 13:31:41 +0200 commented answer Keys of a dictionary Thanks a lot for your answer and help, but it seems that the problem remained the same. Keys of a dictionary are matrices and I cannot get specific preimage. For example for your last example if I type preimage[4] it still gives me a KeyError If I work with real number instead of integers ZZ, it does not work either m = matrix( ZZ, 1, 1, [6,] ) m.set_immutable()  2017-12-10 18:54:14 +0200 received badge ● Commentator 2017-12-10 18:54:14 +0200 commented question Keys of a dictionary Here it is: v = random_vector(RR, 5) K = diagonal_matrix(v) import itertools W = itertools.product([0,1], repeat = 5) results = [] for w in W: w = vector(w) w = w.row() v = w.transpose() u = w * K * v results.append(u[0, 0]) W = itertools.product([0,1], repeat = 5) for w in W: w = vector(w) w = w.row() v = w.transpose() for u in results: u = w * K * v u.set_immutable() preimages[u] = vector(w) preimages  2017-12-10 17:42:34 +0200 commented question Keys of a dictionary I think the problem is that u is a matrix, is there a way to convert it to a number ? 2017-12-10 17:06:26 +0200 commented question Keys of a dictionary the best I can get, but the problem remains the same: v = w.transpose() for u in results: u = w * K * v u.set_immutable() preimages[u] = vector(w)  2017-12-10 16:13:39 +0200 commented question Keys of a dictionary if I change the last part of my code to: ... u = w * K * v for u in results: preimages[u] = [w]  (results are defines before as list or tuple of all u) the problem with keys seems to be solved, but I do not get appropriate vectors as preimages, instead I get (1, 1, 1, 1, 1) for all u 2017-12-10 15:19:05 +0200 commented question Keys of a dictionary The code looks like this: import itertools W = itertools.product([0,1], repeat = n) preimages = {} for w in W: w = vector(w) w = w.row() v = w.transpose() u = w * K * v u.set_immutable() if u in preimages: preimages[u].append(w) else: preimages[u] = [w]  The problem is most likely because I have set u immutable, but I need to get preimages for my function u = w * K * v 2017-12-10 14:43:41 +0200 asked a question Keys of a dictionary Hello guys, I have a piece of code to get a dictionary of preimages of a function. These look like this: {[0.0]: [[0 0 0 0 0]], [1.27]: [[0 0 0 0 1]], [2.63]: [[1 1 1 0 0]], ..... , [5.619999999999999]: [[1 0 1 1 1]]}  if I look for preimages.keys(), I get the following: [[0.0], [1.27], [2.63], ....... , [5.619999999999999]]  But when I try to get a particular preimage like preimages[1.27] I get an error KeyError: 1.27000000000000  even though it is in a dictionary. Or if I try preimages [[1.27]]  I get another error TypeError: unhashable type: 'list'  Could someone please help to solve this ploblem ? 2017-12-07 16:22:57 +0200 marked best answer Corresponding value of a parameter Hello, I am quite new to Sage and I have the following question. In my computations I have a quadratic form: $x^T A x = b$ , where the matrix A is defined already as a symmetric matrix, and x is defined as import itertools X = itertools.product([0,1], repeat = n) for x in X: x = vector(x) print x  as all combination of $[0,1]$ repeated n times. I got a set of values for $b$ and extracted few of them from the set. Now, I need to identify which $x$ is corresponding to a particular value of $b$. Is there any command to do so? If it is possible. Thank you for help. 2017-12-07 16:22:47 +0200 marked best answer remember and collect values/output Hello, I am working with a function that involves random values. Thus, every time I run my code I get different values(in my case it is a set of values). When I get my output I want Sage to remember it and collect it every time I run my code. At the end I'd like to get a list of sets that has been accumulated every time I ran my code. Is there a way to do it? I tried to research this topic and haven't found anything except saving a list to .txt file. 2017-12-02 18:33:33 +0200 commented question Corresponding value of a parameter the whole code with defined previously matrix A looks like this: import numpy import itertools X = itertools.product([0,1], repeat = n) results = [] for x in X: x = vector(x) x = x.row() v = x.transpose() b = x * A * v results.append(b[0, 0]) print results