Contact
CoCalc Logo Icon
StoreFeaturesDocsShareSupport News AboutSign UpSign In
| Download

All published worksheets from http://sagenb.org

Views: 168729
Image: ubuntu2004

1 算数运算

SAGE使用“=”进行赋值,布尔运算符为"==",">=","<=",">","<"

a=5;a
5
2==2
True
2>3
False

SAGE的基本数学运算有: 指数("**","^"),除法(“/”),地板除(“//”),取余("%")

2**3
8
2^3
8
5/2
5/2
5//2
2
18%5
3

SAGE中以0开头的数字为8进制

012
10
09#输入09会出错,因为8进制无9
Traceback (most recent call last): File "<stdin>", line 1, in <module> File "_sage_input_24.py", line 10, in <module> exec compile(u'open("___code___.py","w").write("# -*- coding: utf-8 -*-\\n" + _support_.preparse_worksheet_cell(base64.b64decode("MDkj6L6T5YWlMDnkvJrlh7rplJnvvIzlm6DkuLo46L+b5Yi25pegOQ=="),globals())+"\\n"); execfile(os.path.abspath("___code___.py"))' + '\n', '', 'single') File "", line 1, in <module> File "/tmp/tmp4oe0Hz/___code___.py", line 2 _sage_const_09 = Integer(09) ^ SyntaxError: invalid token

2 帮助

和linux下众多软件相同,SAGE用于强大的内置文档,只需要输入函数或者常数名字,加上问号即可。

sin?

File: /opt/sage/local/lib/python2.6/site-packages/sage/functions/trig.py

Type: <class ‘sage.functions.trig.Function_sin’>

Definition: sin(*args, coerce=True, hold=False, dont_call_method_on_arg=False)

Docstring:

The sine function.

EXAMPLES:

sage: sin(0)
0
sage: sin(x).subs(x==0)
0
sage: sin(2).n(100)
0.90929742682568169539601986591
sage: loads(dumps(sin))
sin

3 函数

在SAGE中定义一个新的函数,使用命令def,并在变量列表后跟一个冒号,如:

def is_even(n): return n%2==0
is_even(2)
True
is_even(123)
False

4 IF语句

SAGE中if语句的语法如下:

if expression:
    if_suite
else:
    else_suite
elif expression1:
    elif_suite

我们编写如下函数

f(x)={1,x<0;0,,x=0;1,x>0.f(x)=\left\{\begin{array}{lrr}-1 & , & x<0;\\ 0, &, & x=0;\\ 1 &, & x>0.\end{array}\right.

def myfunction(x): if x<0: return -1 elif x==0: return 0 else: return 1
myfunction(-2)
-1
myfunction(0)
0
myfunction(3)
1

5 循环

SAGE中提供了两种循环:while循环和for循环, 其中while循环为当型循环,语法如下:

while expression:
    while_suite

假设银行的一年期整存整取的年利率为6.9%,试计算10000元存款多少年后连本带息达到20000,编写命令如下:

money=10000 years=0 while money<20000: years=years+1 money=money*(1+0.069) print u"需存款"+str(years)+u"年,"+u"连本带息共计"+str(money.n(digits=7))+u"元"
需存款11年,连本带息共计20833.14元

SAGE中的for循环与C语言的for循环略有不同,C语言中的for循环为计数器循环,而SAGE中的for循环功能较计数器循环略强,更像foreach(针对每个个体重复一次),先看传统的计数器循环语法:

for each in range(n):#自0循环到n-1:each++
    for_suite
for each in range(m,n):#自m循环到n-1:each++
    for_suite
for each in range(m,n,step):#自m循环到n-1:each+=step
    for_suite

计算n的阶乘

n=10;ans=1 for each in range(1,n+1): ans*=each print ans
3628800

下面举例说明类似foreach的用法

for eachitem in [u'name:薛鹏翔',u'age:30岁',u'job:教师']: print eachitem
name:薛鹏翔 age:30岁 job:教师

6 类class

SAGE也支持面向对象的程序设计,定义类的语法为:

class ClassName(base_class[es]):
    "optional documentation string"
    static_member_declarations
    method_declarations

定义Fibonacci数列类,基础类为list,命令如下:

class fibonacci(list): "Fibonacci" def __init__(self,n): list.__init__(self,range(1,n+1)) self[1]=1 for each in range(2,n): self[each]=self[each-1]+self[each-2] def __repr__(self): return "Fibonacci numbers" def show(self,n): return int((((1.0+sqrt(5.0))/2.0)^n-((1.0-sqrt(5.0))/2.0)^n)/sqrt(5.0))
9
x=fibonacci(10)
9
len(x)
10
9
x
Fibonacci numbers
list(x)
[1, 1, 2, 3, 5, 8, 13, 21, 34, 55]
x.show(11)
89
fibonacci?

Type: <type 'type'>

Definition: fibonacci( [noargspec] )

Docstring:



Fibonacci