ASKSAGE: Sage Q&A Forum - Latest question feedhttp://ask.sagemath.org/questions/Q&A Forum for SageenCopyright Sage, 2010. Some rights reserved under creative commons license.Wed, 10 Oct 2018 14:07:28 -0500Use of / operator when defining a functionhttp://ask.sagemath.org/question/43906/use-of-operator-when-defining-a-function/ Hello. I need a clarification on the behaviour of fractions. More precisely, I don't understand the behaviour of the / operator when used inside a def().
I noticed that / finds the integer part of the fraction when used inside a def:
def sumA():
a=randint(2,10)
b=randint(2,10)
c=randint(3,10)
d=randint(4,10)
return [a,b,c,d,a/b + c/d]
Then sumA() the following result:
[8, 3, 9, 8, 3]
That is: 8/3 + 9/8 = 3. How can I make a/b + c/d work like a sum of fractions inside this function?
Oddly enough, the behaviour is different when generating random numbers a and b and adding 1/a + 1/b.
def sumB():
a=randint(2,10)
b=randint(2,10)
return [a,b,1/a + 1/b]
This function adds the fractions in the correct way. The result of sumB() is
[5, 9, 14/45]
Finally, I noticed that writing a*1/b + c*1/d instead of a/b + c/d makes everything work like fractions:
def sumC():
a=randint(2,10)
b=randint(2,10)
c=randint(3,10)
d=randint(4,10)
return [a,b,c,d,a*1/b + c*1/d]
The result of sumC() is
[6, 7, 5, 6, 71/42]
jcarrilloWed, 10 Oct 2018 14:07:28 -0500http://ask.sagemath.org/question/43906/piecewise defined function via defhttp://ask.sagemath.org/question/41061/piecewise-defined-function-via-def/I've made the following experiment with Sage:
def f(x):
if (0<= x<= 1/2):
return 1
else:
return 0
assume(0<= x<= 1/2)
show(f(x))
show(f(1/3))
However I get outputs 0 and 1 respectively. Can someone clarify please? Thanks.newuserSat, 10 Feb 2018 20:43:20 -0600http://ask.sagemath.org/question/41061/piecewise defined function via defhttp://ask.sagemath.org/question/41060/piecewise-defined-function-via-def/I've made the following experiment with Sage:
def f(x):
if (0<= x<= 1/2):
return 1
else:
return 0
assume(0<= x<= 1/2)
show(f(x))
show(f(1/3))
However I get outputs 0 and 1 respectively. Can someone clarify please? Thanks.newuserSat, 10 Feb 2018 20:39:46 -0600http://ask.sagemath.org/question/41060/plot operation errorhttp://ask.sagemath.org/question/39148/plot-operation-error/I'm trying to plot the following.
def myfn2(x):
if x<0:
return 1
else:
return -1
plot(myfn2(x),x,-3,3,figsize=3,color="red")
The graph is only displayed as -1. Why?matxzeroThu, 12 Oct 2017 19:38:19 -0500http://ask.sagemath.org/question/39148/If , else, case?http://ask.sagemath.org/question/34830/if-else-case/Hallo!
Is there a function to help me out to test a condition without a 'cascade' of 'else(s)'? Like a "**case**"? I'm trying hard to find out a solution using the build-in help/manual and could not find a "case" statement. *Thank you in advance.*
r = randint(1,3)
if r == 1:
print 'One.'
if r == 2:
print 'Two.'
if r == 3:
print 'Three.'LRMThu, 15 Sep 2016 17:24:08 -0500http://ask.sagemath.org/question/34830/def type of mpmath matrices in cythonhttp://ask.sagemath.org/question/31210/def-type-of-mpmath-matrices-in-cython/ I'm trying to speed up the exact *iterative* calculation of the 1E9 th Fibonacci number, which has 208987640 digits.
Working with Python and mpmath I've reached an acceptable time, but I'm looking to further reduce it using Cython.
In this case the overload is due to the huge numbers, stored in Knuth's 2 x 2 matrices, that have been defined using Numpy and mpmath.
For Cython to be helpful in this subject, I should define those matrices as static, but I don't know the type I should use to define it. Is there a special type for this case?
Cython curiousMon, 30 Nov 2015 12:43:17 -0600http://ask.sagemath.org/question/31210/