Ask Your Question

Xenia's profile - activity

2019-01-13 15:45:14 -0500 received badge  Notable Question (source)
2019-01-13 15:45:14 -0500 received badge  Popular Question (source)
2018-12-16 04:23:45 -0500 received badge  Notable Question (source)
2018-10-26 12:14:03 -0500 received badge  Notable Question (source)
2018-09-30 00:48:52 -0500 received badge  Popular Question (source)
2018-06-15 14:47:41 -0500 received badge  Popular Question (source)
2018-02-11 11:56:11 -0500 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 08:08:16 -0500 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 12:36:46 -0500 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 06:31:41 -0500 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 11:54:14 -0500 received badge  Commentator
2017-12-10 11:54:14 -0500 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 10:42:34 -0500 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 10:06:26 -0500 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 09:13:39 -0500 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 08:19:05 -0500 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 07:43:41 -0500 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-02 11:32:40 -0500 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
2017-12-02 11:25:06 -0500 commented question Corresponding value of a parameter

My apologies for ambiguity. I have edited the post. Hope it is now clearer.

2017-12-02 08:49:25 -0500 received badge  Organizer (source)
2017-12-02 07:38:04 -0500 asked a question 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-11-23 14:07:44 -0500 asked a question 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-11-21 12:50:30 -0500 marked best answer Plotting a frequency chart from a set of values

Hello,

I have got a list of results from my function already. And now, I need to visualise this information and plot a chart of how many times each particular value is repeated in a list. I know how to do a histogram, but it shows intervals of values, but not specific values. I need to access all the values afterwards as well and be able to work with them.


I have found also that I can count values using a command "count" list.count() if it is necessary to do it like this in my problem. I'd like to ask what is the code for such frequency chart (or plot of points)? And how do I need to define my variables? Would be much appreciated if someone could help in any way. Thank you very much.

Xenia

2017-11-21 12:50:30 -0500 received badge  Scholar (source)