Contact
CoCalc Logo Icon
StoreFeaturesDocsShareSupport News AboutSign UpSign In
| Download

Tutorial for using %julia in Sage Worksheets

Views: 1163

This is a tutorial worksheet for SageMathCloud's Julia interface. This is orthogonal to IJulia and is a completetely different approach.

See http://julia.readthedocs.org/en/latest/manual/getting-started/

WARNING: Currently if you put %julia at the start of a cell it will be properly Julia syntax highlighted. If you just switch to Julia mode (with default_mode), then Python syntax highlighting happens instead. This is a temporary "not implemented" issue and won't be hard to fix.

%auto %default_mode julia
1 + 2
3
ans
3
%md(hide=False) Note that % modes *do* work here (we have markdown right here), unlike in IJulia, and you can escape to different other systems, convert variables back and forth, etc.

Note that % modes do work here (we have markdown right here), unlike in IJulia, and you can escape to different other systems, convert variables back and forth, etc.

%python i = sum(range(50)) julia.eval("i=%s"%i)
'1225'
# this is in julia i
1225
i = i + 10
1235
%python julia('i')._sage_()
1235
︠83201484-da77-4daf-b718-4e2467319bfai︠ %md Sage pexepct modes have some interesting depth, for example:

Sage pexepct modes have some interesting depth, for example:

%sage a = julia('12') b = julia('45') a+b
57
%sage (a+b).sin() # call julia's sin
0.43616475524782494
︠d4cdb014-20ff-42f6-87f1-f1aa44035da2i︠ %md ## Back to the tutorial...

Back to the tutorial...

println("Greetings! 你好! 안녕하세요?")
Greetings! 你好! 안녕하세요?
# Assign the value 10 to the variable x x = 10
10
# Doing math with x's value x + 1
11
# Reassign x's value x = 1 + 1
2
# You can assign values of other types, like strings of text x = "Hello World!"
"Hello World!"
x = 1.0
1.0
y = -3
-3
Z = "My string"
"My string"
customary_phrase = "Hello world!"
"Hello world!"
UniversalDeclarationOfHumanRightsStart = "人人生而自由,在尊严和权力上一律平等。"
"人人生而自由,在尊严和权力上一律平等。"
# Unicode names (in UTF-8 encoding) are allowed: δ = 0.00001 안녕하세요 = "Hello"
"Hello"
%julia δ + x
1.00001
pi
π = 3.1415926535897...
pi = 3
Warning: imported binding for pi overwritten in module Main 3
sqrt(100)
10.0
sqrt = 4
Warning: imported binding for sqrt overwritten in module Main 4
x = 3 2x^2 - 3x + 1
10
3(2 - 5im)^-1.0
0.20689655172413796 + 0.5172413793103449im
greet = "Hello" whom = "world" string(greet, ", ", whom, ".\n")
"Hello, world.\n"
"$greet, $whom.\n"
"Hello, world.\n"
function f(x,y) x + y end
f (generic function with 1 method)
f(2,3)
5
(x,y) = x + y
∑ (generic function with 1 method)
(2,3)
5
%sage # we overwrote sqrt above, and I don't know how to undo that, so # just force a restart of the backend julia process julia.quit()
# or we could use "workspace()" ︠88f52cd0-b4a2-40bd-984d-212fa34e02c0︠ i = 5
5
workspace()
# this *should* result in an error: i
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 "/projects/4a5f0542-5873-4eed-a85c-a18c706e8bcd/.sagemathcloud/julia.py", line 114, in eval raise RuntimeError(julia_error) RuntimeError: ERROR: i not defined
%julia # use %julia for better syntax highlighting/code folding, etc. for now. 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)
hypot(3, 4)
5.0