ASKSAGE: Sage Q&A Forum - RSS feedhttps://ask.sagemath.org/questions/Q&A Forum for SageenCopyright Sage, 2010. Some rights reserved under creative commons license.Mon, 18 Jan 2021 11:29:54 +0100I should store binary values in an arrayhttps://ask.sagemath.org/question/55323/i-should-store-binary-values-in-an-array/Given a Sage integer, I wish to obtain the list of its binary digits.
How can I get that?
So far, I found the `binary` method of Sage integers.
Given an integer, that method returns its binary expansion as a string.
For example:
sage: a = 8
sage: a.binary()
'1000'
-----
Thanks for the response.
Here is a follow-up question.
x = 3
z = IncreasingArrays()
z = x.binary()
for i in range(x):
if z[i] == 1:
print(' ', 2^i) # Not printing here
I want to print if binary value is 1.
And raise it to power 2.
The `for` loop isn't printing print statement also.
Please help me with these for loops and also how to store values in an array.
In C I go:
int a[6];
for(i = 2 ; i <= 5 ; i++) { a[i] = 1 ; }
Here like this in C, array values are set.
How to do the same in Sage please help.RamaMon, 18 Jan 2021 11:29:54 +0100https://ask.sagemath.org/question/55323/Generate >2D Matrixhttps://ask.sagemath.org/question/49762/generate-2d-matrix/I am trying to find a concise way to store data in a single variable for easy computational referencing. This data is basically an n-length vector of 2x2 matrices. I would like to be able to do linear algebra on the 2x2 matrices, but each of the n elements are handled separately. Is there an easy way to do this?
Basically, this would be the equivalent of a nx2x2 array in Numpy. The reason I want to use Sage instead of Numpy, is because there is a lot of linear algebra, including some symbolic, that needs to be performed. Numpy and Sympy are too slow for this. I've been able to do these computations in Maxima with an acceptable speed. For ease of interfacing with my Python script, I would like to just use Sage. This would also make things much easier if I do still need to push some of the computation to Maxima.mattbMon, 03 Feb 2020 14:27:19 +0100https://ask.sagemath.org/question/49762/return lists that do not share all of the same elementshttps://ask.sagemath.org/question/46159/return-lists-that-do-not-share-all-of-the-same-elements/ I am having Sage generate an array of lists for me, but I do not want it to give me the lists that have all of the same elements. For example, if Sage gives me
[1 2 3 4]
[2 3 4 1]
[1 3 4 2]
I only want Sage to give me [1 2 3 4]. I want to create a new array with these non-repeated lists. How do I do that? Thank you!merluzaSun, 14 Apr 2019 18:19:37 +0200https://ask.sagemath.org/question/46159/Checking that all values in an array satisfy an inequalityhttps://ask.sagemath.org/question/45259/checking-that-all-values-in-an-array-satisfy-an-inequality/I would like to know how to check element-wise that every entry in an array is less than or equal to a particular number. It would seem that something like this would require a loop, but I am not sure. Could someone help me? Thank you in advancemerluzaFri, 01 Feb 2019 09:19:09 +0100https://ask.sagemath.org/question/45259/Checking that all values in an array satisfy an inequalityhttps://ask.sagemath.org/question/45258/checking-that-all-values-in-an-array-satisfy-an-inequality/I would like to know how to check element-wise that every entry in an array is less than or equal to a particular number. It would seem that something like this would require a loop, but I am not sure. Could someone help me? Thank you in advancemerluzaFri, 01 Feb 2019 09:18:03 +0100https://ask.sagemath.org/question/45258/polynomial list, arrayhttps://ask.sagemath.org/question/11065/polynomial-list-array/I would like to store polynomials in an array/list in Sage. How do I do this?
-------
I am editing my question due to the confusion expressed below.
I would like to generate a list of polynomials from a loop. HansThu, 19 Jun 2014 22:30:28 +0200https://ask.sagemath.org/question/11065/array numpy mixed division problemhttps://ask.sagemath.org/question/42456/array-numpy-mixed-division-problem/ Hi
why this line is ok:
DiceValueFrequencyT.append([nonZeroValueT[value],(valueT[nonZeroValueT[value]]*(1/int(len(DiceT[die]))))])
Except the fact that I do not understand why it is written 0.8333333 instead of 5/6 as usual
but this one is ko ?:
DiceValueFrequencyT.append([nonZeroValueT[value],(valueT[nonZeroValueT[value]] / int(len(DiceT[die])))])
in notebook Sagemath 8.2
forget()
import numpy as np
DiceT=[[3,3,3,3,3,6],[1,4,4,4,4,4] ,[2,2,2,5,5,5]]
DiceColorT=['Red','Green','White']
DiceValueFrequencyT=[]
#Rf=[[3,5/6],[6,1/6]];Gf=[[1,1/6],[4,5/6]];Wf=[[2,1/2],[5,1/2]];
for die in range(0,len(DiceT)) :
freqT = np.array(DiceT[die])
valueT = np.bincount(freqT)
nonZeroValueT=np.nonzero(valueT)[0]
show(valueT)
show(nonZeroValueT)
for value in range(0,len(nonZeroValueT)) :
print int(len(DiceT[die]))
# these Two lines are ok when uncommented
#DiceValueFrequencyT.append([nonZeroValueT[value],(valueT[nonZeroValueT[value]]*(1/6))])
DiceValueFrequencyT.append([nonZeroValueT[value],(valueT[nonZeroValueT[value]]*(1/int(len(DiceT[die]))))])
# but this one give false results when uncommented!
#DiceValueFrequencyT.append([nonZeroValueT[value],(valueT[nonZeroValueT[value]] / int(len(DiceT[die])))])
show (DiceValueFrequencyT)
ortolljMon, 28 May 2018 18:42:16 +0200https://ask.sagemath.org/question/42456/Is there a command to find the place of an element in an array?https://ask.sagemath.org/question/41832/is-there-a-command-to-find-the-place-of-an-element-in-an-array/ For example, let v=[a, b, c, d]
I wonder if there is already encoded a function f such that f(a)=0, f(b)=1, f(c)=2..logomathFri, 30 Mar 2018 11:39:59 +0200https://ask.sagemath.org/question/41832/Convert all complex numbers in array/matrix to value 0 or remove these numbershttps://ask.sagemath.org/question/33847/convert-all-complex-numbers-in-arraymatrix-to-value-0-or-remove-these-numbers/ Hi all
I'm new to sagemath, approximately 2 week-old. How do you convert all complex numbers in a nested array to value 0 or NaN? Or to remove these numbers from the array/matrix(which is eventually what I want to achieve in the end)?
Thanks for your help!
Rgds
Samsam_kjmSat, 18 Jun 2016 21:39:34 +0200https://ask.sagemath.org/question/33847/passing an array of values or a matrix to a user defined function, also expected the array or the matrix as output...How can we do this?https://ask.sagemath.org/question/32826/passing-an-array-of-values-or-a-matrix-to-a-user-defined-function-also-expected-the-array-or-the-matrix-as-outputhow-can-we-do-this/ passing an array of values or a matrix to a user defined function, also expected the array or the matrix as output...How can we do this?noufalasharafSat, 19 Mar 2016 15:00:56 +0100https://ask.sagemath.org/question/32826/cell arrays octavehttps://ask.sagemath.org/question/31485/cell-arrays-octave/Sorry my question probably naive. I have vector K with numbers.
K =
92
93
109
110
114
115
143
147
161
162
I have also a cell-array
C =
{
[1,1] = 4
[2,1] = 2
[3,1] = 5
[4,1] = 9
[5,1] = 7
[6,1] = 6
[7,1] = 8
[8,1] = 1
[9,1] = 3
[10,1] = 10
}
I need to compose the vector with values from K in C order.
I can do it using K([4,2,5,9,7,6,8,1,3,10]), but how to write it using a variable. Thank you very much.ivan.bFri, 11 Dec 2015 16:38:04 +0100https://ask.sagemath.org/question/31485/Create a periodic vector/arrayhttps://ask.sagemath.org/question/30637/create-a-periodic-vectorarray/What is the quickest way to create an array of length 999 such as the following:
[0, 2, 7, 0, 2, 7, 0, 2, 7, ...]
?Erel Segal-HaleviWed, 11 Nov 2015 18:51:31 +0100https://ask.sagemath.org/question/30637/export data and labels in cvs filehttps://ask.sagemath.org/question/23407/export-data-and-labels-in-cvs-file/Hi experts!
I have the Numpy arrays `AAA=[a1,a2,...]` and `BBB=[b1,b2,...]`. I know that I can export these arrays doing:
np.savetxt('array AAA.csv', AAA, delimiter=',')
np.savetxt('array BBB.csv', BBB, delimiter=',')
I wanna export one csv file that include both arrays + labels, in this way:
AAA | BBB
----------
a1 | b1
a2 | b2
. | .
. | .
. | .
How can I do that?
Waiting for your answers.
Thanks a lot.
Best regardsmresimulatorMon, 14 Jul 2014 01:08:16 +0200https://ask.sagemath.org/question/23407/construct a structurehttps://ask.sagemath.org/question/10778/construct-a-structure/Dear all,
I want to construct a structured array which has 2 fields storing 2 different data type, one is a float number and the other is a polynomial matrix.
[Structured Arrays (and Record Arrays)](http://docs.scipy.org/doc/numpy/user/basics.rec.html) Can I use this NumPy method or any other else? NumPy doesn't know/understand the data type of a polynomial matrix defined in SAGE.
sage: R.<s> = PolynomialRing(QQ); R
Univariate Polynomial Ring in s over Rational Field
sage: H = matrix(1,2, [(s+5)**2, s]); H
[s^2 + 10*s + 25 s]
sage: H.parent()
Full MatrixSpace of 1 by 2 dense matrices over Univariate Polynomial Ring in s over Rational Field
sage: type(H[0,0])
<type 'sage.rings.polynomial.polynomial_rational_flint.Polynomial_rational_flint'>
sage: sys = np.zeros(1, dtype=[('TF','sage.rings.polynomial.polynomial_rational_flint.Polynomial_rational_flint',(1,2)),('Ts','f4')])
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
/home/xxx/Programs/sage-5.10-linux-64bit/local/lib/python2.7/site-packages/sage/all_cmdline.pyc in <module>()
----> 1 sys = np.zeros(Integer(1), dtype=[('TF','sage.rings.polynomial.polynomial_rational_flint.Polynomial_rational_flint',(Integer(1),Integer(2))),('Ts','f4')])
TypeError: data type "sage.rings.polynomial.polynomial_rational_flint.Polynomial_rational_flint" not understood
----------
Thanks in advance!gundamlhTue, 26 Nov 2013 10:09:38 +0100https://ask.sagemath.org/question/10778/Numpy array or list of random signshttps://ask.sagemath.org/question/10468/numpy-array-or-list-of-random-signs/Hi experts!
How can I obtain a numpy array or list of 'N'random signs? for example:
[1, -1, 1, 1 , -1, -1, 1 , 1, 1]
where N = 9.
Waiting for your answers.
Thanks a lot!mresimulatorSat, 24 Aug 2013 18:28:40 +0200https://ask.sagemath.org/question/10468/Add Numpy array into other Numpy arrayhttps://ask.sagemath.org/question/10391/add-numpy-array-into-other-numpy-array/Hi experts!!
My code starts with a empty array (POPOPO=np.array([])).
In the next lines, np.array are created (L1, L2, ...). How can it add this new-created-arrays into POPOPO in order to obtain the array POPOPO= [[L1],[L2],...]?
Its important to note that L1, L2,.... have different lenght.
Waiting for your answers.
Thanks in advance!mresimulatorSat, 27 Jul 2013 11:21:42 +0200https://ask.sagemath.org/question/10391/export data in MS Excel filehttps://ask.sagemath.org/question/10335/export-data-in-ms-excel-file/Hi experts!
Im running a code that generate three differents Numpy arrays (L1, L2 and L3) for every for-cycle (there is N cycles), so I get 3N arrays.
I wanna save these NP arrays in a MS excel file (.xls or other).
My questions are:
1) How can i generate this excel file?
2) How can I read the Excel file generated in a script?
Waiting for your answers.
Thanks a lot!
mresimulatorTue, 09 Jul 2013 12:20:37 +0200https://ask.sagemath.org/question/10335/Replace rows an columns by zeros in a numpy arrayhttps://ask.sagemath.org/question/10395/replace-rows-an-columns-by-zeros-in-a-numpy-array/Hi expers.
¿How can I replace rows an columns by zeros (or other values) in a numpy array?
Waiting for your answers.
Thanks a lot.mresimulatorMon, 29 Jul 2013 12:13:08 +0200https://ask.sagemath.org/question/10395/replace values in Numpy arrayhttps://ask.sagemath.org/question/10394/replace-values-in-numpy-array/Hi experts.
I'm trying to replace some values in a Numpy array. I wanna conserve the unchanged array too.
sage: import numpy as np
sage: N = np.array([[1,2,3],[4,5,6],[8,9,7]])
I call K the new array (with some values repaced):
sage: K = N
sage: K
array([[1, 2, 3],
[4, 5, 6],
[8, 9, 7]])
sage: K[1,2] = 9
sage: K
array([[1, 2, 3],
[4, 5, 9],
[8, 9, 7]])
But here is the problem: the original array is changed too!!
sage: N
array([[1, 2, 3],
[4, 5, 9],
[8, 9, 7]])
How can I change only the array K (and conserve without change the original array N)?
Waiting for your answers.
Thanks a lot!
mresimulatorSun, 28 Jul 2013 23:05:30 +0200https://ask.sagemath.org/question/10394/refresh numpy array in a for-cyclehttps://ask.sagemath.org/question/10291/refresh-numpy-array-in-a-for-cycle/Hi experts!
Im writing a code with a numpy array L, the numpy matrix M and the next script:
for x in L:
for l in srange(N):
z= l in L
if z is False and M[x,l] != 0:
L=np.append(L,l)
here, in the end of the cycle, new elements are incorporated to the array 'L'.
I want these new elements be considered as 'x' index in the cycle.
When I execute the script I see that only the 'originals' elements of L are considered as 'x'.
How can i fix it?
Waiting for your answers.
Thanks a lot!
mresimulatorWed, 26 Jun 2013 19:36:41 +0200https://ask.sagemath.org/question/10291/frequency (count) in Numpy Arrayhttps://ask.sagemath.org/question/10279/frequency-count-in-numpy-array/Hi experts!
For a list `L` the command `L.count(z)` shows the number of times that the element `z` is repeated in list `L`.
For example:
[I] L=[1,1,3,1,4,5,8]
[I] L.count(1)
[O] 3
How can i do that with a Numpy array?
Waiting for your answers.
Thanks a lot!
mresimulatorTue, 25 Jun 2013 18:28:27 +0200https://ask.sagemath.org/question/10279/Using the solution of a linear system, and splitting a matrixhttps://ask.sagemath.org/question/8925/using-the-solution-of-a-linear-system-and-splitting-a-matrix/Hi there,
I'm fairly new to Sage and Python, so I'm getting into basic problems here that I'd be happy if you could help me out.<br>
Here it is, actually, here they are:
I'm generating a set of equalities and solving them with solve. A simple example:
<blockquote> import numpy as n;<br>m=4;<br>
s = list(var('s_%d' % int(i)) for i in range(m));<br>
eqns=[s_0+s_1==1,s_2-s_3==0];<br>
sol=solve(eqns,s,solution_dict=True)[0]
</blockquote>
This gives the solutions:
<blockquote>
{s_1: -r2 + 1, s_0: r2, s_3: r1, s_2: r1}
</blockquote>
My first question is, how do I create a matrix with the solutions? Say, something like:
<blockquote>M=m.zeros((2,2));<br>
for i in range(2):<br>
for j in range(2):<br>
M[int(i),int(j)] = sol[s[i]]+ sol[s[j+2]]
</blockquote>
This is giving me the error: "TypeError: unable to simplify to float approximation"
My second question, would be, given the array M, how do I split it as r1 times a matrix, plus r2 times another matrix, plus a constant matrix? In the above example,
<blockquote> M= [[1,r1+r2],[r1-r2+1,r1-r2+1]]= r1 [[0,1],[1,1]] + r2 [[0,1],[-1,-1]]+ [[1,0],[0,0]]
</blockquote>
I'm interested in the matrices multiplying the still unknown coefficients. Maybe I should add that the number of equations in the problem I'm solving is much bigger than in this simple example, and therefore I cannot find this matrix decomposition by simply looking at it.<br>
Thanks for the help!
fmeloWed, 25 Apr 2012 09:56:14 +0200https://ask.sagemath.org/question/8925/Newton's cubic Fractal help. Plotting https://ask.sagemath.org/question/8555/newtons-cubic-fractal-help-plotting/I'm solving newton's cubic for all the black points that occur within the fractal:
I have the code:
LA(x) = x - (x^3.0-1)/(3.0*x^2.0)
S = solve(LA==0,x)
for s in S:
solve(LA == s.rhs(),x)
and i want to take all those points I'm generating and plot them in a scatter plot. I think I have to use an array? Can anyone tell me exactly how I tell sage to put these points in an array then plot them? Thanks everyone for your help
----------
rower9911Wed, 14 Dec 2011 21:44:19 +0100https://ask.sagemath.org/question/8555/Creating an array of variableshttps://ask.sagemath.org/question/8390/creating-an-array-of-variables/Here is a very very basic question.
I want to create a polynomial, say
a_0*x^0 + a_1*x + a_2*x^2+ \cdots + a_{20} x^{20}.
I could define these a_i one at a time, but it would be much better to have a way to create an array A of length 20 where A[i] is the coefficient a_i. The idea is that I want to do some operations and solve for these coefficients, which will end up being rational numbers.
There must be some very basic command that I don't know, but I can't find it in the documentation.NathanMon, 17 Oct 2011 12:16:43 +0200https://ask.sagemath.org/question/8390/Elegant solution to 'thin out' array and plot linehttps://ask.sagemath.org/question/8231/elegant-solution-to-thin-out-array-and-plot-line/Sometimes I don't know how many points will be in the target array, yet I need to plot a line with markers. If too many points presented, markers on the plot will overlap and an single bold line will be plotted:
line([(x, np.sin(x)) for x in srange(0,np.pi,np.pi/128)], marker='d')
The solution acceptable for me is to 'thin out' input array in order to exclude points, which are too close to each other. My first implementation of such functional is:
def thin_out_array(points, size):
if len(points) <= size: return points
g = int(len(points)/size)
return [p for i,p in enumerate(points) if i % g == 0]
def line(points, thin_out = None, **kwds):
if thin_out: points = thin_out_array(points, thin_out)
return sage.plot.line.line(points, **kwds)
So this code gives line with separated markers:
line([(x, np.sin(x)) for x in srange(0,np.pi,np.pi/128)], marker='d', thin_out = 32)
Perhaps, more clear and common way to do such thing is already designed?
EugeneSat, 16 Jul 2011 15:48:10 +0200https://ask.sagemath.org/question/8231/Can I define a function from a list of values?https://ask.sagemath.org/question/7818/can-i-define-a-function-from-a-list-of-values/Hello,
I am doing numerical integration. If I define the resulting function as a list of values, I have to keep track of both the number of values and the distance between them. This also makes showing the domain on a plot more annoying. Could I instead define a function object from the list of values? In other words, I need an array whose index is not necessarily integer. And I need to be able to plot it over a range.
Thanks!chaeslocThu, 16 Dec 2010 09:04:22 +0100https://ask.sagemath.org/question/7818/