ASKSAGE: Sage Q&A Forum - Latest question feedhttps://ask.sagemath.org/questions/Q&A Forum for SageenCopyright Sage, 2010. Some rights reserved under creative commons license.Sun, 21 Apr 2019 07:20:34 -0500Rounding, using modular arithmetic, etc. in find_fit?https://ask.sagemath.org/question/46299/rounding-using-modular-arithmetic-etc-in-find_fit/I'm learning how to use find_fit and it works great for polynomials, but I'd like to include expressions that require integers. My variables are all integers, and my coefficients are expected to be relatively simple rationals (i.e. small denominators). In particular, I'd like to use a model like:
model(x,y) = (a*x+b*y+c)%(d*x+e*y+f)
However, I get the expected error:
TypeError: unsupported operand parent(s) for %: 'Symbolic Ring' and 'Symbolic Ring'
If I try to convert (dx+ey+f) to an integer within the model using int(), ceiling(), etc. then it won't convert, since of course it's a symbolic expression. Is there a way to round within a symbolic expression? Or any suggestions for other workarounds? Thanks!
edit: I was able to get it to run, but it doesn't provide an acceptable solution. I imagine it's due to how find_fit works with user-defined functions but I don't know enough about the inner-workings to work that out. Here is my code:
data = [(i,(i%2) ) for i in range(30)]
var('a, b, c, d, x')
def f(x, a, b, c, d):
return int(a+b*x)%int(c*x+d)
fit = find_fit(data, f, parameters = [a, b, c, d], variables = [x], solution_dict = True)
print fit
and the output is
{d: 1.0, c: 1.0, b: 1.0, a: 1.0}
I would like it to return, say,
{d: 2.0, c: 0.0, b: 1.0, a: 0.0}
Is there a way to make this work a bit better for my needs?ctennenhSun, 21 Apr 2019 07:20:34 -0500https://ask.sagemath.org/question/46299/Rounding entries of a random vectorhttps://ask.sagemath.org/question/39697/rounding-entries-of-a-random-vector/Hello,
I am trying to generate a random diagonal matrix, defined by a random vector over a field RR. The problem is that I need to round all the values to two decimal places, make entries evenly positive and negative (not necessary of equal amount) and, ideally, avoid zeroes.
I have a code
`[round(4*random()-2,2)for i in[1 .. 8]]`
that produces a list of values that I need of size 8. However, I am struggling to combine it with a command `diagonal_matrix` and insert it there.
Also, I don't really understand why do we need to multiply it by 4 in here
`[round(4*random()-2,2)for i in[1 .. 8]]`
and why it produces negative values only, if I multiply it by 2 instead of 4. Could someone explain it please?
Is there any other simpler and more elegant way to solve this problem? Thank you.XeniaTue, 21 Nov 2017 07:53:11 -0600https://ask.sagemath.org/question/39697/Select list element around a valuehttps://ask.sagemath.org/question/38648/select-list-element-around-a-value/ HI
I would like to show all elements of allRootsAPow2 which are around 2 + 3*I
this code below does not work ;-(
allRootsAPow2 =[2.00000000000000 + 3.00000000000000*I,
2.00000000000000 - 3.00000000000000*I,
-1.05087254532372e-32,
11.2111025509280 - 6.86482042695334e-16*I,
2.00000000000000 - 3.00000000000000*I,
2.00000000000000 + 3.00000000000000*I,
11.2111025509280 - 6.86482042695334e-16*I,
-1.05087254532372e-32,
2.00000000000000 + 3.00000000000000*I,
2.00000000000000 - 3.00000000000000*I,
-1.05087254532372e-32,
11.2111025509280 - 6.86482042695334e-16*I,
2.00000000000000 - 3.00000000000000*I,
2.00000000000000 + 3.00000000000000*I,
11.2111025509280 - 6.86482042695334e-16*I,
-1.05087254532372e-32,
-3.21110255092798 + 1.96623323036392e-16*I,
3.00992653992793e-33,
2.00000000000000 - 3.00000000000000*I,
2.00000000000000 + 3.00000000000000*I,
3.00992653992793e-33,
-3.21110255092798 + 1.96623323036392e-16*I,
2.00000000000000 + 3.00000000000000*I,
2.00000000000000 - 3.00000000000000*I,
-3.21110255092798 + 1.96623323036392e-16*I,
3.00992653992793e-33,
2.00000000000000 - 3.00000000000000*I,
2.00000000000000 + 3.00000000000000*I,
3.00992653992793e-33,
-3.21110255092798 + 1.96623323036392e-16*I,
2.00000000000000 + 3.00000000000000*I,
2.00000000000000 - 3.00000000000000*I]
for el in allRootsAPow2 :
if (math.ceil(el.real_part()*10)/10)== 2 and (math.ceil(el.imag_part()*10)/10)== 3 :
#if (math.ceil(el.real_part()*10)/10)== 2 :
#if (math.ceil(el.imag_part()*10)/10)== 3 :
show(el)
ortolljThu, 24 Aug 2017 12:14:20 -0500https://ask.sagemath.org/question/38648/'object is not callable' error in sagetex (that doesn't occur in a worksheet)https://ask.sagemath.org/question/35766/object-is-not-callable-error-in-sagetex-that-doesnt-occur-in-a-worksheet/I'm generating random numbers to be presented in scientific notation for a quiz I'm typing. In testing this code works fine in a worksheet, but gives an error in a sagesilent block of my .tex document. Here's the code:
sci = {}
for index in range(1, 14): # Picks random coeffs for sci not.
places = ZZ.random_element(0, 4)
sci["co{0}".format(index)] = round(10*random(), places)
powe = {}
for index in range(1, 14): # Picks random powers for use.
powe["r{0}".format(index)] = ZZ.random_element(5, 10)
sci['co1']
sci['co2']
powe['r1']
powe['r2']
In the worksheet, those last four lines generate output like
3.0
5.566
7
8
But when I use that same code in the .tex document (without the last four lines), Sage returns this error before I can even call my numbers:
'module' object is not callable
My best guess is that rounding needs some package that the worksheet imports automatically but sagetex does not, but I'm afraid I don't know enough about what's really going on. Thank you for any help!mathochistMon, 28 Nov 2016 13:00:55 -0600https://ask.sagemath.org/question/35766/Why is sage behaving weird when rounding rational numbers?https://ask.sagemath.org/question/33490/why-is-sage-behaving-weird-when-rounding-rational-numbers/I might be doing something stupid, but Sage's `int()` and `round()` functions seem to be buggy to me.
I'm trying to use these with rational numbers to round towards zero. I'd expect `-7/4` to round to `-1` and `+7/4` to round to `1`. Instead I get different behavior for rationals and their decimal representation:
┌────────────────────────────────────────────────────────────────────┐
│ SageMath Version 7.1, Release Date: 2016-03-20 │
│ Type "notebook()" for the browser-based notebook interface. │
│ Type "help()" for help. │
└────────────────────────────────────────────────────────────────────┘
sage: int(-1.75) # Correctly rounds towards zero
-1
sage: int(-7/4) # Does not round towards zero
-2
sage: (-7/4).round("toward") # Does not round towards zero (despite what the documentation claims!)
-2
sage: -7/4 + 0.0 # The numbers are in fact the same!
-1.75
How do I round rational numbers towards zero in Sage?con-f-useSat, 21 May 2016 07:34:27 -0500https://ask.sagemath.org/question/33490/How to keep my real numbers roundedhttps://ask.sagemath.org/question/30599/how-to-keep-my-real-numbers-rounded/The following code:
var('t')
test(x) = e^(x * t)
test(0.326)
gives:
e^(0.326000000000000*t)
For some reason, Sage converted my "0.326" to "0.326000000000000".
I want to keep the number rounded. i.e:
e^(0.326*t)
I don't even care if Sage rounds it like this:
e^(0.33*t)
How can I do this?
I know it is possible to use fractions, E.g, I can use "326/1000" instead of "0.326", but this doesn't look good. All I want is that the floating point numbers remain rounded.
Erel Segal-HaleviMon, 09 Nov 2015 11:44:41 -0600https://ask.sagemath.org/question/30599/rounding error .n() and N()https://ask.sagemath.org/question/9282/rounding-error-n-and-n/Why does 1.414 - sqrt(2).n(digits=4) not evaluate to zero?
sage: sqrt(2).n(digits=4)
1.414
sage: 1.414 - sqrt(2).n(digits=4)
-0.0002136
kjhudsonMon, 08 Apr 2013 04:53:32 -0500https://ask.sagemath.org/question/9282/4-Digit Rounding Arithmetic:System of Equationshttps://ask.sagemath.org/question/8305/4-digit-rounding-arithmeticsystem-of-equations/I am given this system of equations:
1.130x - 6.990y = 14.20
1.013x - 6.099y = 14.22
In sage I input
sage: y = var("y")
sage: solve ([1.130*x + 6.990*y == 14.20, ... etc ], x,y )
it spits out 4264/63, 1684/189
for x,y I now input
sage: round((4264.0/63.0), 4) --> for x and same thing for y, using 1684.0 / 189.0.
Giving me 8.9101, yet this answer seems to be wrong.
My question is where can I found out how to do 4 digit rounding arithmetic using sage? or Am I doing something wrong with the code. Mathstudent2010Sat, 03 Sep 2011 22:56:41 -0500https://ask.sagemath.org/question/8305/rounding bug?https://ask.sagemath.org/question/7963/rounding-bug/Hi all:
In a notebook window i tell Sage
w = 0.4
while w > 0.1:
w = w - 0.1
print w
print w==0
and it outputs
0.3000000000000000
0.200000000000000
0.100000000000000
2.77555756156289e-17
False
Is this supposed to happen?
Thanks for your attention.
Alex
araichevTue, 01 Mar 2011 15:28:02 -0600https://ask.sagemath.org/question/7963/Changing number of decimal digits displayed in outputhttps://ask.sagemath.org/question/7713/changing-number-of-decimal-digits-displayed-in-output/I am doing some computations with large matrices over the complex numbers. When these matrices are outputted, the entries appear, for example, as
[ 0.03661165235? - 0.08838834765?*I 0.21338834765? + 0.08838834765?*I 0.03661165235? + 0.08838834765?*I etc
I would like to change it so that the output only displays each entry to 3 decimal places, so that they are easier to see. Is there a way to do this? Is there a way to do this that doesn't alter the number of digits stored? For reference, my input is
P8 = diagonal_matrix([1,1,1,0,0,0,0,1,1,0,0,0, 0,1,1,1])
U*P8*U.inverse()
where U is a previously defined matrix with complex entries.
Thanks!slearchWed, 29 Sep 2010 09:37:13 -0500https://ask.sagemath.org/question/7713/