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.Tue, 29 Mar 2022 03:53:02 +0200function definition doesn't work in startup filehttps://ask.sagemath.org/question/61731/function-definition-doesnt-work-in-startup-file/I have this in my jupyter startup file. The os works, but the function definition doesn't take, although I know it works when I paste it in after startup. (I like range to not cut off the last number, but I don't want to type include_endpoint every time.) Do function definitions not work in startup? What does work?
> import os
> os.chdir("allsagemathprogs")
>
> def endrange(a,b,c=1):
> return srange(a,b,c,include_endpoint=True)cybervigilanteTue, 29 Mar 2022 03:53:02 +0200https://ask.sagemath.org/question/61731/Use of / operator when defining a functionhttps://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 21:07:28 +0200https://ask.sagemath.org/question/43906/piecewise defined function via defhttps://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.newuserSun, 11 Feb 2018 03:43:20 +0100https://ask.sagemath.org/question/41061/piecewise defined function via defhttps://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.newuserSun, 11 Feb 2018 03:39:46 +0100https://ask.sagemath.org/question/41060/plot operation errorhttps://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?matxzeroFri, 13 Oct 2017 02:38:19 +0200https://ask.sagemath.org/question/39148/If , else, case?https://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.'LRMFri, 16 Sep 2016 00:24:08 +0200https://ask.sagemath.org/question/34830/def type of mpmath matrices in cythonhttps://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 19:43:17 +0100https://ask.sagemath.org/question/31210/