Contact
CoCalc Logo Icon
StoreFeaturesDocsShareSupport News AboutSign UpSign In
| Download

Tutorial for using %julia in Sage Worksheets

Views: 1163
import julia as j0; reload(j0); julia = j0.Julia()
<module 'julia' from './julia.py'>
%julia function hypot(x,y) x = abs(x) y = abs(y) if x > y r = y/x return x*sqrt(1+r*r) end if y == 0 return zero(x) end r = x/y return y*sqrt(1+r*r) end
hypot (generic function with 1 method)
%julia hypot(2,3)
3.6055512754639896
%julia println("Greetings! 你好! 안녕하세요?")
Greetings! 你好! 안녕하세요?
s = 'println("Greetings! 你好! 안녕하세요?")'
type(s)
<type 'str'>
t = u'println("Greetings! 你好! 안녕하세요?")'
type(t)
<type 'unicode'>
julia.eval(t.encode('utf8'))
println("Greetings! 你好! 안녕하세요?") __ans__=ans;println("8115930f-3f2a-4748-93fe-d835c53cbab4");ans=__ans__; 'Greetings! \xe4\xbd\xa0\xe5\xa5\xbd! \xec\x95\x88\xeb\x85\x95\xed\x95\x98\xec\x84\xb8\xec\x9a\x94?'
t.decode('utf8')
Error in lines 1-1 Traceback (most recent call last): File "/projects/4a5f0542-5873-4eed-a85c-a18c706e8bcd/.sagemathcloud/sage_server.py", line 862, in execute exec compile(block+'\n', '', 'single') in namespace, locals File "", line 1, in <module> File "/usr/local/sage/sage-6.3.beta6/local/lib/python/encodings/utf_8.py", line 16, in decode return codecs.utf_8_decode(input, errors, True) UnicodeEncodeError: 'ascii' codec can't encode characters in position 20-21: ordinal not in range(128)
︠36074ac2-b489-43a5-8745-d20f4e8777f0︠ print julia.eval('println("Greetings! 你好! 안녕하세요?")')
println("Greetings! 你好! 안녕하세요?") __ans__=ans;println("9b380974-4103-4580-8bde-ad978ab06c07");ans=__ans__; Greetings! 你好! 안녕하세요?
%julia 4c2100d8-b3a6-436e-a967-09b58147f4e4︠ %julia 2+3
5
%julia i = 1; while i <= 5 println(i) i += 1 end
1 2 3 4 5
%julia while i <= 10 println(i) i += 1 end
6 7 8 9 10
m = julia('i')._sage_(); m
11
type(m)
<type 'sage.rings.integer.Integer'>
%timeit(number=50) julia.eval('2+3')
50 loops, best of 3: 3.08 ms per loop
%julia a = "lksdjf dlfjasdflj" a
"lksdjf\ndlfjasdflj"
%julia a = "lksjdfdf
Error in lines 1-1 Traceback (most recent call last): File "/projects/4a5f0542-5873-4eed-a85c-a18c706e8bcd/.sagemathcloud/sage_server.py", line 862, in execute exec compile(block+'\n', '', 'single') in namespace, locals File "", line 1, in <module> File "/projects/4a5f0542-5873-4eed-a85c-a18c706e8bcd/.sagemathcloud/sage_server.py", line 897, in execute_with_code_decorators print code_decorator.eval(code, locals=self.namespace), File "./julia.py", line 109, in eval raise RuntimeError(julia_error) RuntimeError: ERROR: syntax: incomplete: invalid string syntax
%julia a = 5; nonsense
Error in lines 1-1 Traceback (most recent call last): File "/projects/4a5f0542-5873-4eed-a85c-a18c706e8bcd/.sagemathcloud/sage_server.py", line 862, in execute exec compile(block+'\n', '', 'single') in namespace, locals File "", line 1, in <module> File "/projects/4a5f0542-5873-4eed-a85c-a18c706e8bcd/.sagemathcloud/sage_server.py", line 897, in execute_with_code_decorators print code_decorator.eval(code, locals=self.namespace), File "./julia.py", line 109, in eval raise RuntimeError(julia_error) RuntimeError: ERROR: nonsense not defined
%julia a
5
a = julia('10'); b = julia('3.14')
a+b
13.14
a.sin()
-0.5440211108893698
julia('i')
11
julia('i')._sage_()
11
m = julia('i') m._sage_()
11
str(m)
'11'
m._sage_repr()
'11'
m._sage_()
11
m._sage_??
File: /usr/local/sage/sage-6.3.beta6/local/lib/python2.7/site-packages/sage/interfaces/interface.py Source: def _sage_(self): """ Attempt to return a Sage version of this object. This is a generic routine that just tries to evaluate the repr(self). EXAMPLES:: sage: gp(1/2)._sage_() 1/2 sage: _.parent() Rational Field AUTHORS: - William Stein - Felix Lawrence (2009-08-21) """ string = self._sage_repr() try: return sage.misc.sage_eval.sage_eval(string) except Exception: raise NotImplementedError("Unable to parse output: %s" % string)