Julia. Introduction
Ode to Julie
It is very difficult to convey all the enthusiasm that accompanied the launch of the first programs and the correction of the first errors using this language. It is simple and beautiful as
Python
, a bit like
Fortran
, convenient work with arrays and graphs, as well as the ability to perform fierce optimization and parallelization, even for teapots such as
I
my classmates. You can work at different levels of abstraction: from high-level programming with dynamic typing you can go down to assembler commands, that is, here you have Python accessibility and speed of execution of Fortran counters. I can not shake the feeling that
Mathcad
,
Scilab
and even, God forgive me,
C ++
start in my heart to go to the background.
Habr's publication , and, as it should be an impressionable student, began to look for guidance, but preferably in Russian. Since the language has been constantly evolving for six years, the scarce sources of information have become obsolete, and the excitement has begun to decline. But with the beginning of a new laboratory course on modeling physical phenomena with a free choice of programming language, there was a motivation to start acquaintance with Julia. In addition, in August the language is "dopekli 1.0" .
The following material was planned as an introduction to the language on which all the laboratory books were later written and the methodology was collected.
Julia
- high-level, high-performance programming language with dynamic typing for mathematical calculations. The syntax is similar to the Matlab family, the language is written in
Cu
,
C ++
and
Scheme
, there is a possibility to call the Syshny libraries
Installation
Under the spoiler [/b]
At the official website you can find news, video tutorials and download the distribution. After installation, you can start working, but everything will happen in the mode of the interpreter.
On the site https://juliacomputing.com products are available, the basis for which this language served:
JuliaDB
- to work with database analytics and time series analytics, based on the rich Julia ecosystem, as well as having built-in parallelism and scalability.
JuliaBOX
- Run Julia without installing from your browser in Jupyter laptops. It is popular in universities and among beginners. For full-fledged work, you need to make a paid subscription. In the free mode, some options will be limited, and to access the computational cores you need to wait in the queue
JuliaRun
- Run Julia applications in a public or private cloud. Scalable deployment in production for real-time analysis and for large-scale parallel simulations.
JuliaFin
- For work in finance. Includes all the tools needed for backtesting and trading: Excel, Bloomberg, contract modeling and JuliaDB.
JuliaPro
- Free version for scientists and data researchers. Install in Windows, Mac or Linux. An extended commercial license is available.
Choose the last option. At the time of writing, version ???.1 is available. After registration, a free download will be available. Members
Windows 7 /Windows Server 2012
also have to set:
TLS easy_fix To find out the details, see Discourse thread .
Windows Management Framework 3.0 or later .
After downloading the fixer, it is better to provide a restriction on access to the Internet, and then update, not everyone will know what your invalid copy of Windows is. This update is necessary to prevent problems with the version control system
git
, otherwise it will not be possible to download additional packages, and without them it will be hard.
Well, everything was finally established, now at our disposal:
JuliaPRO Command Prompt
- All Yulins skills directly from the interpreter.
Juno
- beautiful IDE with a window for graphics and workspace, where you can watch the contents of all objects
Jupyter
- runs the computational kernel in the console, and you can execute the code directly in the browser. (In addition to Julia, there is also Python)
Let's see what this calculator can do Unicode support - you can use Cyrillic, hieroglyphs and call pi a Greek letter. And you can not explicitly specify multiplication between a number and a variable (in this order and without a space):
x = 5 + 8
2x - 3x + 2x ^ 2
Out: 325
All the necessary signs are also in place: + =, * =, = etc. (Sign "" (bitwise shift to the right) Comparison marks:>,> =, <, <=, ==, !=. Неравенства можно объединять в цепочки:
. y = 5-
y + = 2
4 <= y < 8
Out: true
Complex numbers available:
(2-1im) * (4 + 3im)
Out: 11 + 2im
And the functions for working with them:
-
- real (z) - the real part,
- imag (z) - Imaginary part,
- conj (z) Complex conjugate number,
- abs (z) - module,
- abs2 (z) - the square of the module,
- angle (z) Is the argument of a complex number.
You can use rational numbers using "//" and the corresponding functions:
-
- num (x) - numerator,
- den (x) Denominator,
- float (x) - converts to decimal fraction
x = 4 //6 + 5 //7
Out: 29 //21
float (x)
Out: ???r3r3784.
In Julia, you can manage the internal representation of the dаta:
-
- typeof (obj) - the object type is
- typemax (obj) - the maximum number of this type is
- typemin (obj) - the mimic
- eps () - machine zero
- BigInt - large integer
- BigFloat - large with floating point
q = 3-
typemax (q)
Out: 9223372036854775807
typeof (q)
Out: Int64
BigFloat (2.3 ^ 45/6)
Out: ???e + 15
Functions
A set of special functions
-
- abs (x) - the modulus of the number,
- abs2 (x) - the square of the module,
- sqrt (x) - the square root,
- cbrt (x) - cubic root,
- exp (x) - the exponent of the number,
- log (x) - natural logarithm,
- log10 (x) - The decimal logarithm,
- log (b, x) - the logarithm of x on the base b.
And also trigonometric, hyperbolic, Airy, Bessel and many other things.
Custom Functions:
function name (arguments)
# the body of the function
end
The function returns the result of the last expression (user
? Mathcad ).
function cube (x)
x ^ 3
end
cube (4)
Out: 64
Well, or explicitly:
function myabs (x)
if x> = 0
return x
else
return -x
end
end
myabs (-12)
Out: 12
Return values can be collected in the tuple:
function cubeandsquare (x)
x ^ ? x ^ 2
end
a, b = cubeandsquare (3)
print ("a = $ a, b = $ b")
Out: a = 2? b = 9
Functions can accept tuples, default values, keywords. If there are no parentheses after the function name, it is treated as a variable and can be assigned to another variable or passed to the function as a parameter. Julia also supports the functional style of writing programs (hello Lisp)
function mysin (t; A = ?? = ?? = 0) # Unicode support - you can use the Greek characters
A * sin (? * T +?)
end
x1 = mysin (pi) # sinus Pi = 0
x2 = mysin (pi * 0.? A = 2) # increase the amplitude twice
x3 = mysin (pi * 0.5 ,? = 0.5) # reduce the frequency of
twice. print ("x1 = $ x? x2 = $ x? x3 = $ x3")
Out: x1 = ???e-1? x2 = 2.? x3 = ???r3r3784.
Arrays
I'm a user of Scilab did not even notice the spoofing: you can set the array using the function:
-
- Array {T} (undef, dims ) - An array of type T and dimension dims
- zeros (T, dims ) - An array of zeros
- ones (T, dims ) - or
units.
Indexing begins with the unit , instead of$
-end
, and also all necessary operations for matrices are defined (in order to implement, say, element-by-element addition or multiplication, the operator must be put in front of the operator).
(Now it's clear how to embed pictures, but okay )
Basic functions:
-
- det (A) - calculate the determinant
- A ' - transpose the matrix
- inv (A) - Invert the matrix
- length (A) - number of elements
- ndims (A) Is the number of dimensions of
- size (A) A tuple of dimensions
- size (A, n) - dimension in the given direction
- copy (A) - create a copy of the array
- linspace (start, end, step) or
linspace (start: step: end) - Creation of a one-dimensional array
A =[1 2 3; 6 5 4; 7 8 9]
Out: 3? 3 Array {Int6?2}:
???
???
???
A[2,1]
Out: 6
A[end]
Out: 9
size (A)
Out: (? 3)
You can select parts of an array by specifying the range of indices along the dimension with the ":" sign.
m1 = rand (3.2)
m2 = reshape (1: 2: 1? ?2)
Out: 3? 2 Base.ReshapedArray {Int6?? StepRange {Int6? Int64}, Tuple {}}:
???r3r3832. ???r3r3832. ???r3r3832. m3 =[m1 m2]# join rows (along the first dimension)
Out: 3? 4 Array {Float6?2}:
???.??? ???r3r3832. ???.??? ???r3r3832. ???.??? ???r3r3832. m5 =[m1; m2]# join columns (along the second dimension)
Out: 6? 2 Array {Float6?2}:
???.701038
???.620215
???.805307
???.0
???.0
???.0
m3[:, 2:4]
Out: 3? 3 Array {Float6?2}:
???.???
???.???
???.???
Here we used rand (), which returns an array of random numbers of a given dimension, and reshape (), which changes the dimension of the array to the specified one.
for a in A
# Actions on a from array A
end
or
for i in eachindex (A)
# actions taking into account the index i
end
or
for i = 1: size (A, n)
# n - along which dimension the index runs (hmm you can simplify the 3D VU)
# actions taking into account the index i
end
Charts
To use the graphics, you need to download the package from the repository:
-
- Pkg.add ("Plots")
- Pkg.add ("PyPlot")
- Pkg.add ("Gadfly")
- Pkg.add ("Winston")
Of these, the most popular is Piton's PyPlot . Modules are connected with the command using , for example:
using PyPlot
However, let's try Gaston using Gnuplot (it shakes separately).
It is loaded by Gaston.jl with the command
Pkg.add ("Gaston")
And immediately to the case:
using Gaston
t = 0: ???: 1
plot (t, sin. (2? * 5 * t))
plot (t, sin. (2? * 5 * t), title = "A sine wave", xlabel = "Time (s)", ylabel = "Amplitude", grid = "on", linewidth = ? color = "blue",
yrange = "[-1.1:1.1]", marker = "ecircle", plotstyle = "linespoints", linestyle = "-.-")
plot! (t, cos. (2? * 5 * t), color = "red", linewidth = 2) # add to the frame one more graph
x = y = -15: ???: 15
(x. y + x. y), title = "Sombrero" ", plotstyle =" pm3d ")
x = y = -15: ???: 15
surf (x, y, (x, y) -> sin. (sqrt (x. * x + y. * y)) ./sqrt. (x. * x + y. * y),
title = " Edge view of a sombrero ", plotstyle =" pm3d ", gpcom =" set view 8?20 ")
R =[x+y for x=0:5:120, y=0:5:120]
G =[x+y for x=0:5:120, y=120:-5:0]
B =[x+y for x=120:-5:0, y=0:5:120]
Z = zeros (???)
Z[:,:,1]= R
Z[:,:,2]= G
Z[:,:,3]= B
imagesc (Z, title = "RGB Image", clim =[10 200])
histogram (rand (1000), bins = 1? norm = ? title = "Histogram", yrange = "[0:1.6]")
y = 1:40
err = Gaston.ErrorCoords (rand (40))
plot (y, err = err, title = "Example of error bars", plotstyle = "errorbars")
It is possible to create several graphic windows (does not work in
Jupyter
) Using the command
h = figure ()
(Just insert between plot'ami). To save the graph as an image file, use the commands
set_filename ("name.png")
# if you do not specify,
will be displayed.
printfigure ("png")
# save to file, available PNG, PDF, SVG and GIF
More information on the package Gaston
Conclusion
There are many more graphic packages for every taste and covering almost all needs. So-and-so are developing auxiliary packages. Here you and quantum computing, and bioinformatics, and machine learning, and many more pressing problems like 3f3r3821. and
derivatives of .
In general, Julia is beautiful, intelligent and very promising, and it is extremely impermissible to leave her without due attention.
It may be interesting
We take being #1 in Commercial Odor Remover Products & Systems very seriously. Here are some of the reasons you can trust Cupridyne Clean to bring you a solution to your Commercial Odor Control problem.