Coding With Fun
Home Docker Django Node.js Articles Python pip guide FAQ Policy

Julia's differences from other languages


May 14, 2021 Julia


Table of contents


The difference with other languages

The difference from MATLAB

Julia's syntax is very much like MATLAB. B ut Julia doesn't simply copy MATLAB, they have a lot of synth and functional differences. Here are some notable differences:

  • Arrays are indexed in square brackets, A[i,j]
  • Arrays are assigned by reference. After A=B the B also modifies A
  • Use references to pass and assign values. If a function modifies an array, the calling function will find that the value has also changed
  • Matlab combines assignment and allocated memory into a single statement. F or example: a(4) = 3.2 creates an array a = [0 0 0 3.2] a(5) = 7 J ulia separates assignment from allocated memory: a is a[5] = 7 error is thrown. J ulia has a dedicated push! to add Vectors Vectors. And it's much more efficient a(end+1) = val val.
  • The imaginary sqrt(-1) is im by im
  • Literally a number that does not have a number of points is defaulted to an integer type rather than a floating-point type. I nteger types of any length are supported. But it also means that some 2^-1 throw an exception because it is not formal.
  • Julia has a one-dimensional array. T he length of the column vector N not Nx1 For example, rand(N) produces a one-dimensional array
  • Use the [x,y,z] to connect a calibration or array, and the connection occurs on the first dimension ("vertical"). F or connections on the second dimension ("horizontal"), you need to use a space, [x y z] T o construct a block matrix, try to use the syntax [a b; c d]
  • a:b and a:b:c used to construct Range Use linspace construct a full vector, or to "connect" a range by using square brackets, [a:b]
  • Function returns must use return keyword instead of listing them in the function definition
  • A file can contain multiple functions, and when a file is loaded, all function definitions are externally visible
  • sum , prod , max are minimalist operations, and if there is only one argument when called, the scope is all elements of the sum(A)
  • sort such as sort, which operates in column direction by default. sort(A) is equivalent to sort(A,1) To sort 1xN matrix, sort(A,2)
  • If A is a 2-dimensional array, fft(A) a 2-dimensional FFT. In particular, it does not work as fft(A,1) which calculates a 1D FFT by column.
  • Even functions without arguments use parentheses, such tic() toc()
  • Do not use a sign at the end of the expression. T he results of the expression do not automatically appear (unless they are under an interactive prompt). println can be used to print values and line them
  • If A B B arrays, A and B A == B return an array of Boolean values. Y ou should A .== B Other Boolean operators can be compared to , != < >
  • The & | $ $ bit operations and ," "or," and "different or." T hey have the same operator precedence as bit operators in python and not the same bit operator precedence in the c language. T hey can be applied on a calibration or between two arrays (each element at the same location is logically performed, returning a new array of results). I t's worth noting their operator precedence, and don't forget the brackets: If you want to determine whether variable A equals 1 or 2, write (A .== 1) | (A .== 2)
  • You ... use ... to pass elements from the collection as arguments to functions, such as xs=[1,2]; f(xs...)
  • The svd in Julia is a vector rather than a complete vernal matrix
  • In Julia ... used to break a line of code into multiple lines. Instead, incomplete expressions automatically continue onto the next line.
  • The ans is the value of the last expression executed in an interactive session;
  • Julia's type type is very close to classes in Matlab. T he structs behavior in Matlab is somewhere between Julia's types and Dicts. If you want to add a domain in strut, using Dict is better than type.

The difference with R

Julia also wants to be an efficient language for data analysis and statistical programming. The difference from R:

  • The = operators, such as <- or <<- using the . . . assignment
  • Construct vectors with square brackets. Julia is equivalent to c [1, 2, 3] c(1, 2, 3)
  • Julia's matrix operations are closer to traditional mathematical languages than R. " If A B are matrices, then matrix multiplication A * B Julia and A %*% B I n R, the first statement represents element-by-element Hadamard multiplication. To multiply point-by-element, Julia is A .* B
  • Use ' operator for matrix transplication. A' A' is equivalent to t(A)
  • You for parentheses when writing an if statement or for loop: you should write for i in ( for i in [1, 2, 3] 3 ) instead of for (i in c(1, 2, 3)) if i == 1 if (i == 1)
  • 0 1 not Boolean values. Y ou cannot if (1) the if statement if accepts boolean values as arguments. Should be written if true
  • Nrow nrow are ncol Size size(M, 1) should be used instead of nrow(M) and size(M, 2) used ncol(M)
  • Julia's SVD defaults to non-thinned, unlike R. To get the same result as R, you should call svd(X, true) X
  • Julia distinguishes between primer, vector, and matrix. I n R, 1 c(1) the same. I n Julia, they're completely different. F or x y are vectors, x' * y is a single-element vector, not a standard. To get the calibration, you should dot(x, y)
  • diag() diagm() Julia are different from R
  • Julia cannot call a function on the left side of the assignment statement: diag(M) = ones(n) . . . ones (n).
  • Julia is not in favor of stuffing the main namespace with functions. Most statistical functions can be found in extension packages, such as DataFrames and Distributions packages:

  • Julia provides multiple groups and hash tables, but does not provide a list of Rs. When multiple numbers are returned, multiple groups should be used: list(a = 1, b = 2) and (1, 2)
  • Custom types are encouraged. J ulia's type is simpler than the S3 or S4 object in R. J ulia's overload system table(x::TypeA) and table(x::TypeB) equivalent to table in table.TypeA(x) and table.TypeB(x)
  • In Julia, passing values and assignments is by reference. I f a function modifies an array, the calling function will find that the value has also changed. This is very different from R, which makes it very efficient to perform new function operations on a large data structure
  • Use hcat and vcat connect vectors and matrices c rbind cbind
  • Julia's range a:b are different from the symbols of the defined vector in R. I t is a special object for iterations with low memory overhead. To convert a range object to a vector, you should enclose the range object in [a:b]
  • max and min are equivalent to pmax and pmax R pmin B ut all parameters should have the same dimension. And maximum minimum replace max and min in the max min is the biggest difference.
  • Functions sum the prod minimum and R languages are not the same. maximum T hey receive one or two parameters. T he first argument is a collection, such as an array, which, if there is a second argument, can indicate the dimensions of the data, in addition to similar operations. F or example, it would be a matrix to compare A=[[1 2],[3,4]] and R-in-Julia's B=rbind(c(1,2),c(3,4)) sum(A) sum(B) will then have the same result, but sum(A,1) is a row vector that contains a column and a sum(A,2) is a column vector that contains rows and sum. If the second argument is a vector, such as sum(A,[1,2])=10 you need to make sure that the second argument is not a problem.
  • Julia has many functions that can modify their arguments. F or example, sort(v) sort!(v) function, a v with an exclamation point can v
  • colMeans() and rowMeans() size(m, 1) and size(m, 2)
  • In R, you need to quantify the code to improve performance. In Julia, the opposite is true: cycles that use non-directionally quantification are usually the most efficient
  • Unlike R, there is no delayed value in Julia
  • NULL NULL
  • There are no statements in Julia that are get assign or get

The difference with Python

  • Indexes for arrays, strings, and so on. The underse cursor for the Julia index starts at 1, not 0
  • Julia uses end when indexing the last element of the list and end and Python uses -1
  • Comprehensions in Julia (also) does not have a conditional if statement
  • End is required at the end of a block such as for, if, end indentation typography is not mandatory
  • Julia doesn't have the syntax of a code branch: if the input is already a complete expression at the end of a line, execute it directly; The way to force Julia's expression branch is to enclose it in parentheses
  • Julia is always column-oriented (similar to Fortran), while numpy array defaults to behavior-oriented (similar to C). If you want to optimize the performance of the traversal array, you should change the order of traversals from numpy to Julia.