Contact
CoCalc Logo Icon
StoreFeaturesDocsShareSupport News AboutSign UpSign In
| Download

All published worksheets from http://sagenb.org

Views: 168744
Image: ubuntu2004

1. INTRODUCTION

ARITHMETIC OPERATORS

4+2*5
14
3*2!=2*10
True
5*2<=1+7
False
2+1==3*1
True
True and False or True
True


NUMERICAL TYPES

INTEGERS AND RATIONAL NUMBERS

a = 10 print(a) print(type(a)) print(a / 3) print(type(a / 3)) print(sqrt(a)) print(type(sqrt(a)))
10 <type 'sage.rings.integer.Integer'> 10/3 <type 'sage.rings.rational.Rational'> sqrt(10) <type 'sage.symbolic.expression.Expression'>

 

REAL NUMBERS

b = 10.0 print(b) print(type(b)) print(b / 3) print(type(b / 3)) print(sqrt(b)) print(type(sqrt(b)))
10.0000000000000 <type 'sage.rings.real_mpfr.RealLiteral'> 3.33333333333333 <type 'sage.rings.real_mpfr.RealNumber'> 3.16227766016838 <type 'sage.rings.real_mpfr.RealNumber'>

 

COMPLEX NUMBERS

c1 = sqrt(-1.0) print(c1) print(type(c1)) c2 = sqrt(-1) print(c2) print(type(c2)) c3 = 1.0 + i*sqrt(2.0) print(c3) print(type(c3))
1.00000000000000*I <type 'sage.rings.complex_number.ComplexNumber'> I <type 'sage.symbolic.expression.Expression'> 1.00000000000000 + 1.41421356237310*I <type 'sage.symbolic.expression.Expression'>
I
I
i
I
c3 = 1.0 + sqrt(-1.0)*sqrt(2.0)
f(x)=x+1 show(f(x))
\newcommand{\Bold}[1]{\mathbf{#1}}x + 1
e=5 print(e)
5
print(c3)
1.00000000000000 + 1.41421356237310*I

 

SYMBOLIC EXPRESSIONS

var('x, y, z') print(x) print(type(x)) z = x + y print(z)
x <type 'sage.symbolic.expression.Expression'> x + y
type(3/2)
<type 'sage.rings.rational.Rational'>
type(2/3.0)
<type 'sage.rings.real_mpfr.RealNumber'>
type(sin(pi/3))
<type 'sage.symbolic.expression.Expression'>
type(sqrt(-1))
<type 'sage.symbolic.expression.Expression'>
type(sqrt(-1.0))
<type 'sage.rings.complex_number.ComplexNumber'>
type(CC(7 + 3 * i))
<type 'sage.rings.complex_number.ComplexNumber'>

 

STRINGS

string_1 = 'Single quoted string' string_2 = "Sometimes it's good to use double quotes" multiline_string = """ This string contains single quotes ' and double quotes " and spans multiple lines""" print(string_1) print(string_2) print(multiline_string) numerical_value = 1.616233 print('The value is ' + str(numerical_value))
Single quoted string Sometimes it's good to use double quotes This string contains single quotes ' and double quotes " and spans multiple lines The value is 1.61623300000000

 

DEFINITIONS OF THE FUNCTIONS

(Sage assumes that x is symbolic by default.)

x
x
y
Traceback (most recent call last): File "<stdin>", line 1, in <module> File "_sage_input_6.py", line 10, in <module> exec compile(u'open("___code___.py","w").write("# -*- coding: utf-8 -*-\\n" + _support_.preparse_worksheet_cell(base64.b64decode("eQ=="),globals())+"\\n"); execfile(os.path.abspath("___code___.py"))' + '\n', '', 'single') File "", line 1, in <module> File "/tmp/tmpwSBbLd/___code___.py", line 2, in <module> exec compile(u'y' + '\n', '', 'single') File "", line 1, in <module> NameError: name 'y' is not defined
var('a, x') f(x) = a * x^3 print(f) show(f) f(2, a=5)
x |--> a*x^3
x  ax3\renewcommand{\Bold}[1]{\mathbf{#1}}x \ {\mapsto}\ a x^{3}
40


DERIVATIVE

(derivative is a Sage function for computing symbolic derivatives)

g(x) = derivative(f, x) show(g) g(x=2, a=3)
\newcommand{\Bold}[1]{\mathbf{#1}}x \ {\mapsto}\ 3 \, a x^{2}
\newcommand{\Bold}[1]{\mathbf{#1}}36
g(2,a=3)
36
g(2,3)
12*a


TYPESET - NOT CHECKED AND CHECKED - Near the top of every worksheet is a check box with the label Typeset.

(has no effect on the print or show functions).

f(x)=a*x^3 f
x |--> a*x^3
f(x)=a*x^3 f
\newcommand{\Bold}[1]{\mathbf{#1}}x \ {\mapsto}\ a x^{3}


PLOT  FUNCTION

var('x') sinc(x) = sin(x) / x plot(sinc, (x, -10, 10))

 

CUSTOMIZATION OF PLOT FUNCTION

 

a function is called using the syntax:

result = function_name(argument_1, argument_2, … , argument_n, keyword=value)


POSITIONAL ARGUMENTS

Positional arguments are required, and they must occur in the correct order.

first argument: symmbolic expresion

second argument: A TUPLE contains the independent variable, the minimum value and the maximum value of the plotting domain.


KEYWORD ARGUMENTS 

A keyword argument is optional and it must come after all the positional arguments. 

plot(sinc, x, xmin=-15, xmax=15, thickness=2, color='red', legend_label='sinc')


BUILT-IN FUNCTIONS

sin(x)
\newcommand{\Bold}[1]{\mathbf{#1}}\sin\left(x\right)
cos(x)
\newcommand{\Bold}[1]{\mathbf{#1}}\cos\left(x\right)
tan(x)
\newcommand{\Bold}[1]{\mathbf{#1}}\tan\left(x\right)
arcsin(x)
\newcommand{\Bold}[1]{\mathbf{#1}}\arcsin\left(x\right)
arccos(x)
\newcommand{\Bold}[1]{\mathbf{#1}}\arccos\left(x\right)
arctan(x)
\newcommand{\Bold}[1]{\mathbf{#1}}\arctan\left(x\right)
sqrt(x)
\newcommand{\Bold}[1]{\mathbf{#1}}\sqrt{x}
exp(x)
\newcommand{\Bold}[1]{\mathbf{#1}}e^{x}
log(x)
\newcommand{\Bold}[1]{\mathbf{#1}}\log\left(x\right)
abs(x)
\newcommand{\Bold}[1]{\mathbf{#1}}{\left| x \right|}
conjugate(x)
\newcommand{\Bold}[1]{\mathbf{#1}}\overline{x}

 

THE RESTORE AND RESET FUNCTIONS

Restore function can be used to restore predefined global variables (such as i and e) to their default values. If we call restore without any arguments, it will restore all the predefined variables to their default values.

Reset deletes all the variables we have defined, restores all global variables to their default values, and resets the interfaces to other computer algebra systems.

print(e + i * 5)
e + 5*I
i = 10 e = 5 print(e + i * 5)
55
restore('e i') print(e + i * 5)
e + 5*I

 

HOW TO DEFINE MY OWN FUNCTION?

def function_name(argument_1, argument_2, … , argument_n):
              """
              Documentation string here
              """
              statement one
              statement two
              ...
              return some_value

Make sure that you consistently indent each line inside the function (indentation is used to delimit blocks of code)!!!

def RC_voltage(v0, R, C, t): """ Calculate the voltage at time t for an R-C circuit with initial voltage v0. """ tau = R * C return v0 * exp(-t / tau) R = 250e3 # Ohms C = 4e-6 # Farads v0 = 100.0 # Volts t = 1.0 # seconds v = RC_voltage(v0, R, C, t) print('Voltage at t=' + str(n(t, digits=4)) + 's is ' + str(n(v, digits=4)) + 'V')
Voltage at t=1.000s is 36.79V

 

Our function even has documentation like a built-in function!

RC_voltage?

File: /tmp/tmp2WYzMF/___code___.py

Type: <type 'function'>

Definition: RC_voltage(v0, R, C, t)

Docstring:



    Calculate the voltage at time t for an R-C circuit
    with initial voltage v0.


FUNCTIONS WITH KEYWORDS ARGUMENTS

If there are keyword arguments, they must be defined after the positional arguments.


def function_name(argument_1, argument_2, … , argument_n, keyword_arg_1=default_value ,… , keyword_arg_n=default_value ):
          """
          Documentation string here
          """
          statement one
          statement two
          ...
          return some_value

def RC_voltage(t, v0=100, R=1000, C=1e-9): """ Calculate the voltage at time t for an R-C circuit with initial voltage v0. """ tau = R * C return v0 * exp(-t / tau) res = 250e3 # Ohms cap = 4e-6 # Farads v0 = 100.0 # Volts t = 1.0 # seconds v = RC_voltage(t, v0=v0, R=res, C=cap) print('Voltage at t=' + str(n(t, digits=4)) + 's is ' + str(n(v, digits=4)) + 'V')
Voltage at t=1.000s is 36.79V
v = RC_voltage(t=1.0) v
\newcommand{\Bold}[1]{\mathbf{#1}}3.29683147847236 \times 10^{-434293}