Contact
CoCalc Logo Icon
StoreFeaturesDocsShareSupport News AboutSign UpSign In
| Download
Views: 242

Magične naredbe

Osim dekoratora %md, %html i %latex, postoji još cijeli niz dekoratora, koje možemo izlistati s %magics

%magics
%auto %axiom %capture %coffeescript %command %cython %default %default_mode %exercise %file %fork %fortran %fricas %gap %gap3 %giac %go %gp %hide %hideall %html %javascript %julia %kash %lie %lisp %load %macaulay2 %magics %magma %maple %mathematica %matlab %maxima %md %mupad %mwrank %octave %pandoc %perl %prun %python %r %reset %ruby %runfile %sage0 %scilab %script %sh %singular %time %timeit %typeset_mode %var %wiki

Za neke, kao npr. %mathematica, potrebno je da je na računalu instaliran odgovarajući program.

%r omogućava korištenje programskog jezika R

%r x <- c(1,2,3,4,5,6) y <- x^2 print(y) mean(y) var(y)
[1] 1 4 9 16 25 36 [1] 15.16667 [1] 178.9667

%fortran omogućuje kompajliranje Fortran koda te njegovo korištenje.

%fortran SUBROUTINE FIB(A,N) INTEGER N REAL*8 A(N) DO I=1,N IF (I.EQ.1) THEN A(I) = 0.0D0 ELSEIF (I.EQ.2) THEN A(I) = 1.0D0 ELSE A(I) = A(I-1) + A(I-2) ENDIF ENDDO END
import numpy n = numpy.array(range(10),dtype=float) fib(n,int(10)) n
array([ 0., 1., 1., 2., 3., 5., 8., 13., 21., 34.])
%cython cimport cython from libc.math cimport sqrt cdef double cy_funkcija(double x): return sqrt(1-x**2) def cy_integral4pi(int n): cdef double korak, rez cdef int i korak = 1.0/n rez = (cy_funkcija(0)+cy_funkcija(1))/2 for i in range(n): rez += cy_funkcija(i*korak) return 4*rez*korak
%timeit cy_integral4pi(10**7)
5 loops, best of 3: 71.4 ms per loop
%cython def is2pow(unsigned int n): while n != 0 and n%2 == 0: n = n >> 1 return n == 1
%time [n for n in range(10^5) if is2pow(n)]
[1, 2, 4, 8, 16, 32, 64, 128, 256, 512, 1024, 2048, 4096, 8192, 16384, 32768, 65536] CPU time: 0.08 s, Wall time: 0.08 s
%python def slow_is2pow(n): while n != 0 and n%2 == 0: n = n >> 1 return n == 1
%time [n for n in range(10^5) if slow_is2pow(n)]
[1, 2, 4, 8, 16, 32, 64, 128, 256, 512, 1024, 2048, 4096, 8192, 16384, 32768, 65536] CPU time: 0.11 s, Wall time: 0.11 s
%octave x = [1; 3; 2] A = [1, 1, 2; 3, 5, 8; 13, 21, 34] A'*x
x = 1 3 2 A = 1 1 2 3 5 8 13 21 34 ans = 36 58 94

%exercise omogućava kreiranje sučelja za vježbanje.

Varijable:

title: naslov

question: pitanje

answer: odgovor

hints: upute (opcionalno)

attempt: odgovor korisnika

Ovdje možete pogledati kako je %exercise implementiran.

%exercise k = randint(2,5) title = "Add %s numbers"%k v = [randint(1,10) for _ in range(k)] question = "What is the sum $%s$?"%(' + '.join([str(x) for x in v])) answer = sum(v) hints = ['This is basic arithmetic.', 'The sum is near %s.'%(answer+randint(1,5)), "The answer is %s."%answer] def check(attempt): c = Integer(attempt) - answer if c == 0: return True if abs(c) >= 10: return False, "Gees -- not even close!" if c < 0: return False, "too low" if c > 0: return False, "too high"
Interact: please open in CoCalc

Jednostavniji primjer.

%exercise title = r"Transpose a $2 x 2$ Matrix" A = random_matrix(ZZ,2) question = "What is the transpose of $%s?$"%latex(A) answer = A.transpose()
Interact: please open in CoCalc