Contact
CoCalc Logo Icon
StoreFeaturesDocsShareSupport News AboutSign UpSign In
| Download

Szimbolikus programcsomagok, sage, 1. óra

Views: 11
2+2
4
2^3000
1230231922161117176931558813276752514640713895736833715766118029160058800614672948775360067838593459582429649254051804908512884180898236823585082482065348331234959350355845017413023320111360666922624728239756880416434478315693675013413090757208690376793296658810662941824493488451726505303712916005346747908623702673480919353936813105736620402352744776903840477883651100322409301983488363802930540482487909763484098253940728685132044408863734754271212592471778643949486688511721051561970432780747454823776808464180697103083861812184348565522740195796682622205511845512080552010310050255801589349645928001133745474220715013683413907542779063759833876101354235184245096670042160720629411581502371248008430447184842098610320580417992206662247328722122088513643683907670360209162653670641130936997002170500675501374723998766005827579300723253474890612250135171889174899079911291512399773872178519018229989376
10!
Error in lines 1-1 Traceback (most recent call last): File "/cocalc/lib/python3.9/site-packages/smc_sagews/sage_server.py", line 1234, in execute flags=compile_flags), namespace, locals) File "<string>", line 1 Integer(10)! ^ SyntaxError: invalid syntax
factorial(1000)
402387260077093773543702433923003985719374864210714632543799910429938512398629020592044208486969404800479988610197196058631666872994808558901323829669944590997424504087073759918823627727188732519779505950995276120874975462497043601418278094646496291056393887437886487337119181045825783647849977012476632889835955735432513185323958463075557409114262417474349347553428646576611667797396668820291207379143853719588249808126867838374559731746136085379534524221586593201928090878297308431392844403281231558611036976801357304216168747609675871348312025478589320767169132448426236131412508780208000261683151027341827977704784635868170164365024153691398281264810213092761244896359928705114964975419909342221566832572080821333186116811553615836546984046708975602900950537616475847728421889679646244945160765353408198901385442487984959953319101723355556602139450399736280750137837615307127761926849034352625200015888535147331611702103968175921510907788019393178114194545257223865541461062892187960223838971476088506276862967146674697562911234082439208160153780889893964518263243671616762179168909779911903754031274622289988005195444414282012187361745992642956581746628302955570299024324153181617210465832036786906117260158783520751516284225540265170483304226143974286933061690897968482590125458327168226458066526769958652682272807075781391858178889652208164348344825993266043367660176999612831860788386150279465955131156552036093988180612138558600301435694527224206344631797460594682573103790084024432438465657245014402821885252470935190620929023136493273497565513958720559654228749774011413346962715422845862377387538230483865688976461927383814900140767310446640259899490222221765904339901886018566526485061799702356193897017860040811889729918311021171229845901641921068884387121855646124960798722908519296819372388642614839657382291123125024186649353143970137428531926649875337218940694281434118520158014123344828015051399694290153483077644569099073152433278288269864602789864321139083506217095002597389863554277196742822248757586765752344220207573630569498825087968928162753848863396909959826280956121450994871701244516461260379029309120889086942028510640182154399457156805941872748998094254742173582401063677404595741785160829230135358081840096996372524230560855903700624271243416909004153690105933983835777939410970027753472000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
cos(pi)
-1
pi
pi
n(pi, digits=30)
3.14159265358979323846264338328
numerical_approx(pi, digits=1000)
3.141592653589793238462643383279502884197169399375105820974944592307816406286208998628034825342117067982148086513282306647093844609550582231725359408128481117450284102701938521105559644622948954930381964428810975665933446128475648233786783165271201909145648566923460348610454326648213393607260249141273724587006606315588174881520920962829254091715364367892590360011330530548820466521384146951941511609433057270365759591953092186117381932611793105118548074462379962749567351885752724891227938183011949129833673362440656643086021394946395224737190702179860943702770539217176293176752384674818467669405132000568127145263560827785771342757789609173637178721468440901224953430146549585371050792279689258923542019956112129021960864034418159813629774771309960518707211349999998372978049951059731732816096318595024459455346908302642522308253344685035261931188171010003137838752886587533208381420617177669147303598253490428755468731159562863882353787593751957781857780532171226806613001927876611195909216420199
factor(5557891)
47 * 118253
factor(628768638765872365872367682687642983759867598)
2 * 7^2 * 29 * 221241604069624337041649430924575293370819
is_prime(118253)
True
n(pi, 100)
3.1415926535897932384626433833
x = pi n(x, 40)
3.1415926536
y = 7 z = 4
y + z
11
x == 4
pi == 4
y == 7
True
sum(n, n, 1, 10)
Error in lines 1-1 Traceback (most recent call last): File "/cocalc/lib/python3.9/site-packages/smc_sagews/sage_server.py", line 1234, in execute flags=compile_flags), namespace, locals) File "", line 1, in <module> File "/ext/sage/sage-8.9_1804/local/lib/python2.7/site-packages/sage/misc/functional.py", line 580, in symbolic_sum return SR(expression).sum(*args, **kwds) File "sage/structure/parent.pyx", line 900, in sage.structure.parent.Parent.__call__ (build/cythonized/sage/structure/parent.c:9197) return mor._call_(x) File "sage/structure/coerce_maps.pyx", line 162, in sage.structure.coerce_maps.DefaultConvertMap_unique._call_ (build/cythonized/sage/structure/coerce_maps.c:4556) raise File "sage/structure/coerce_maps.pyx", line 157, in sage.structure.coerce_maps.DefaultConvertMap_unique._call_ (build/cythonized/sage/structure/coerce_maps.c:4448) return C._element_constructor(x) File "sage/symbolic/ring.pyx", line 389, in sage.symbolic.ring.SymbolicRing._element_constructor_ (build/cythonized/sage/symbolic/ring.cpp:6969) raise TypeError(f"unable to convert {x!r} to a symbolic expression") TypeError: unable to convert <function numerical_approx at 0x7fe9de4d8668> to a symbolic expression
var('n', 'm') sum(n^3, n, 1, m)
(n, m) 1/4*m^4 + 1/2*m^3 + 1/4*m^2
factor(1/4*m^4 + 1/2*m^3 + 1/4*m^2)
1/4*(m + 1)^2*m^2
L = range(1, 11)
L
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
L[0]
1
[1..10]
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
L2 = [n^2 for n in [1..10]]
L2
[1, 4, 9, 16, 25, 36, 49, 64, 81, 100]
sum(L2)
385
L = [1, 3, 5, 7]
L[3]
7
sum(L)
16
prod(L)
105
sum(range(1, 11))
55
range(1,11)
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
range(1, 11, 2)
[1, 3, 5, 7, 9]
[i^2 for i in range(1,11)]
[1, 4, 9, 16, 25, 36, 49, 64, 81, 100]
L = [i^2 for i in range(1,11) if is_prime(i)]
L
[4, 9, 25, 49]
sum(L)
87
var('n')
n
sum(n, n, 1, 10)
55
sum(1/n^2, n, 1, oo)
16π2\displaystyle \frac{1}{6} \, \pi^{2}
%typeset_mode True
next_prime(2^100)
1267650600228229401496703205653\displaystyle 1267650600228229401496703205653
numerical_approx(pi)
3.14159265358979\displaystyle 3.14159265358979
reset('n')
n(pi)
3.14159265358979\displaystyle 3.14159265358979
factorial()
integral?
File: /ext/sage/sage-8.9_1804/local/lib/python2.7/site-packages/sage/misc/functional.py Signature : integral(x, *args, **kwds) Docstring : Return an indefinite or definite integral of an object "x". First call "x.integral()" and if that fails make an object and integrate it using Maxima, maple, etc, as specified by algorithm. For symbolic expression calls "sage.calculus.calculus.integral()" - see this function for available options. EXAMPLES: sage: f = cyclotomic_polynomial(10) sage: integral(f) 1/5*x^5 - 1/4*x^4 + 1/3*x^3 - 1/2*x^2 + x sage: integral(sin(x),x) -cos(x) sage: y = var('y') sage: integral(sin(x),y) y*sin(x) sage: integral(sin(x), x, 0, pi/2) 1 sage: sin(x).integral(x, 0,pi/2) 1 sage: integral(exp(-x), (x, 1, oo)) e^(-1) Numerical approximation: sage: h = integral(tan(x)/x, (x, 1, pi/3)); h integrate(tan(x)/x, x, 1, 1/3*pi) sage: h.n() 0.07571599101... Specific algorithm can be used for integration: sage: integral(sin(x)^2, x, algorithm='maxima') 1/2*x - 1/4*sin(2*x) sage: integral(sin(x)^2, x, algorithm='sympy') -1/2*cos(x)*sin(x) + 1/2*x
var('x')
x
integral(x^2, x)
1/3*x^3
integral(x^2, x, 0, 1)
1/3
integrate(sqrt(1-x^2)^2 *pi, x, -1, 1)
4/3*pi
integrate(sqrt(1-x^2)^3 *4*pi/3, x, -1, 1)
1/2*pi^2
numerical_approx(integral(exp(-x^2*exp(x+1)), x, 2, 3), digits = 1000)
7.892691765733254e-38
derivative?

File: /sagenb/sage_install/sage-4.7.2/local/lib/python2.6/site-packages/sage/calculus/functional.py

Type: <type ‘function’>

Definition: derivative(f, *args, **kwds)

Docstring:

The derivative of f.

Repeated differentiation is supported by the syntax given in the examples below.

ALIAS: diff

EXAMPLES: We differentiate a callable symbolic function:

sage: f(x,y) = x*y + sin(x^2) + e^(-x)
sage: f
(x, y) |--> x*y + e^(-x) + sin(x^2)
sage: derivative(f, x)
(x, y) |--> 2*x*cos(x^2) + y - e^(-x)
sage: derivative(f, y)
(x, y) |--> x

We differentiate a polynomial:

sage: t = polygen(QQ, 't')
sage: f = (1-t)^5; f
-t^5 + 5*t^4 - 10*t^3 + 10*t^2 - 5*t + 1
sage: derivative(f)
-5*t^4 + 20*t^3 - 30*t^2 + 20*t - 5
sage: derivative(f, t)
-5*t^4 + 20*t^3 - 30*t^2 + 20*t - 5
sage: derivative(f, t, t)
-20*t^3 + 60*t^2 - 60*t + 20
sage: derivative(f, t, 2)
-20*t^3 + 60*t^2 - 60*t + 20
sage: derivative(f, 2)
-20*t^3 + 60*t^2 - 60*t + 20

We differentiate a symbolic expression:

sage: var('a x')
(a, x)
sage: f = exp(sin(a - x^2))/x
sage: derivative(f, x)
-2*e^(sin(-x^2 + a))*cos(-x^2 + a) - e^(sin(-x^2 + a))/x^2
sage: derivative(f, a)
e^(sin(-x^2 + a))*cos(-x^2 + a)/x

Syntax for repeated differentiation:

sage: R.<u, v> = PolynomialRing(QQ)
sage: f = u^4*v^5
sage: derivative(f, u)
4*u^3*v^5
sage: f.derivative(u)   # can always use method notation too
4*u^3*v^5
sage: derivative(f, u, u)
12*u^2*v^5
sage: derivative(f, u, u, u)
24*u*v^5
sage: derivative(f, u, 3)
24*u*v^5
sage: derivative(f, u, v)
20*u^3*v^4
sage: derivative(f, u, 2, v)
60*u^2*v^4
sage: derivative(f, u, v, 2)
80*u^3*v^3
sage: derivative(f, [u, v, v])
80*u^3*v^3
len([2, 3, 4])
3\displaystyle 3
next_prime(2^100)
1267650600228229401496703205653\displaystyle 1267650600228229401496703205653
20 % 2 != 1 & 20!=5 != 1
False
[i for i in range(1, 100) if (i%2!= 1) & (i %3!=1)]
[2, 6, 8, 12, 14, 18, 20, 24, 26, 30, 32, 36, 38, 42, 44, 48, 50, 54, 56, 60, 62, 66, 68, 72, 74, 78, 80, 84, 86, 90, 92, 96, 98]
H1 = Set([1, 2, 3])
H1
{1,2,3}\displaystyle \left\{1, 2, 3\right\}
H2 = Set([3, 4, 5])
H2
{3,4,5}\displaystyle \left\{3, 4, 5\right\}
H1.union(H2)
{1,2,3,4,5}\displaystyle \left\{1, 2, 3, 4, 5\right\}
H1.intersection(H2)
{3}\displaystyle \left\{3\right\}
prod?

File: /sagenb/sage_install/sage-4.7.2/devel/sage/sage/misc/misc_c.pyx

Type: <type 'builtin_function_or_method'>

Definition: prod(x, z=None, recursion_cutoff=5)

Docstring:



    Return the product of the elements in the list x.  If optional
    argument z is not given, start the product with the first element
    of the list, otherwise use z.  The empty product is the int 1 if z
    is not specified, and is z if given.

    This assumes that your multiplication is associative; we don't promise 
    which end of the list we start at.

    EXAMPLES:
        sage: prod([1,2,34])
        68
        sage: prod([2,3], 5)
        30
        sage: prod((1,2,3), 5)
        30
        sage: F = factor(-2006); F
        -1 * 2 * 17 * 59
        sage: prod(F)
        -2006

    AUTHORS:
        Joel B. Mohler (2007-10-03 -- Reimplemented in Cython and optimized)
        Robert Bradshaw (2007-10-26) -- Balanced product tree, other optimizations, (lazy) generator support
        Robert Bradshaw (2008-03-26) -- Balanced product tree for generators and iterators
random_DAG(
File: /projects/sage/sage-6.9/local/lib/python2.7/site-packages/sage/misc/prandom.py Signature : random() Docstring : Get the next random number in the range [0.0, 1.0). EXAMPLES: sage: [random() for i in [1 .. 4]] [0.111439293741037, 0.5143475134191677, 0.04468968524815642, 0.332490606442413]
[1..4]
[1, 2, 3, 4]
is_prime?

File: /sagenb/sage_install/sage-4.7.2/local/lib/python2.6/site-packages/sage/rings/arith.py

Type: <type ‘function’>

Definition: is_prime(n)

Docstring:

Returns True if n is prime, and False otherwise.

AUTHORS:

  • Kevin Stueve kstueve@uw.edu (2010-01-17): delegated calculation to n.is_prime()

INPUT:

  • n - the object for which to determine primality

OUTPUT:

  • bool - True or False

EXAMPLES:

sage: is_prime(389)
True
sage: is_prime(2000)
False
sage: is_prime(2)
True
sage: is_prime(-1)
False
sage: factor(-6)
-1 * 2 * 3
sage: is_prime(1)
False
sage: is_prime(-2)
False

ALGORITHM:

Calculation is delegated to the n.is_prime() method, or in special cases (e.g., Python int``s) to ``Integer(n).is_prime(). If an n.is_prime() method is not available, it otherwise raises a TypeError.

%html<em>Kkjaj</em> (double click to edit)
Error in lines 1-1 Traceback (most recent call last): File "/projects/sage/sage-6.9/local/lib/python2.7/site-packages/smc_sagews/sage_server.py", line 905, in execute exec compile(block+'\n', '', 'single') in namespace, locals File "", line 1, in <module> File "/projects/sage/sage-6.9/local/lib/python2.7/site-packages/smc_sagews/sage_server.py", line 927, in execute_with_code_decorators code_decorators = [eval(code_decorator, self.namespace) for code_decorator in code_decorators] File "<string>", line 1 html<em>Kkjaj</em> ^ SyntaxError: invalid syntax
primes_first_n(10) #elso tiz prim
[2\displaystyle 2, 3\displaystyle 3, 5\displaystyle 5, 7\displaystyle 7, 11\displaystyle 11, 13\displaystyle 13, 17\displaystyle 17, 19\displaystyle 19, 23\displaystyle 23, 29\displaystyle 29]
prod([2, 3, 4])
24\displaystyle 24
2+2
4\displaystyle 4
var('i')
i
i^2
-1
reset('i')
L = [1, 2, 3]
t = arcsin(2)
simplify(t.imag_part())
log(3+2)\displaystyle -\log\left(\sqrt{3} + 2\right)
sage.symbolic.expression.Expression.simplify(cosh(log(sqrt(3)+2)))
cosh(log(3+2))\displaystyle \cosh\left(\log\left(\sqrt{3} + 2\right)\right)
sage.symbolic.expression.Expression.simplify(cosh(arcsin(5)))
cosh(arcsin(5))\displaystyle \cosh\left(\arcsin\left(5\right)\right)
cosh(ln(5)).simplify()
cosh(log(5))\displaystyle \cosh\left(\log\left(5\right)\right)
len(Set([3, 4, 3, 5, 6]))
4\displaystyle 4
H1.cardinality()
3\displaystyle 3
H1.an_element()
1\displaystyle 1