translation

This shows you the differences between two versions of the page.

translation [2018/08/25 08:24] |
translation [2018/08/25 08:24] (current) |
||
---|---|---|---|

Line 1: | Line 1: | ||

+ | |||

+ | ~~CLOSETOC~~ | ||

+ | |||

+ | ~~TOC 1-3 wide~~ | ||

+ | |||

+ | |||

+ | Chapter 38 of the [Julia Language Documentation](https://media.readthedocs.org/pdf/julia/latest/julia.pdf) | ||

+ | |||

+ | https://en.wikibooks.org/wiki/Julia_for_MATLAB_Users | ||

+ | |||

+ | |||

+ | |||

+ | ## Translating from R | ||

+ | |||

+ | ### Block Termination | ||

+ | |||

+ | R uses the `{}` to denote a block of statements. Julia uses the `end` keyword to terminate blocks. | ||

+ | |||

+ | ### Assignment | ||

+ | |||

+ | Julia doesn't allow the `<-`, `<<-` and `->` operators that are used commonly in R code. | ||

+ | |||

+ | ### Assignment is only Alias | ||

+ | |||

+ | Most julia objects are assigned by reference, i.e., an assignment creates an alias not a copy. | ||

+ | |||

+ | |||

+ | ### Array Creation | ||

+ | |||

+ | - R : `c(1,2,3)` | ||

+ | - Julia : `[1,2,3]` | ||

+ | |||

+ | ### Vectors and Types | ||

+ | |||

+ | Vectors are always typed and flattened in R, but not in julia. | ||

+ | |||

+ | ### Matrix Multiplication | ||

+ | |||

+ | - R : `A %*% B` | ||

+ | - Julia : `A * B` | ||

+ | |||

+ | |||

+ | |||

+ | |||

+ | ## Translating from Matlab | ||

+ | |||

+ | Julia and Matlab have very similar syntax, with a few notable differences. This file should help you convert Matlab code into Julia. | ||

+ | |||

+ | ### Package imports | ||

+ | |||

+ | The list of registered Julia packages can be found at the Pkg list [website](http://pkg.julialang.org/), or at [Julia.jl](https://github.com/svaksha/Julia.jl). | ||

+ | |||

+ | Some common matlab libraries and their equivalents include: | ||

+ | |||

+ | - Bioinformatics Toolbox -> [Bio.jl](https://github.com/BioJulia/Bio.jl) | ||

+ | - Statistics and Machine Learning Toolbox -> [StatsBase.jl](https://github.com/JuliaStats/StatsBase.jl), [MLBase.jl](https://github.com/JuliaStats/MLBase.jl) | ||

+ | |||

+ | Also, Julia uses the `import`, `using` and `include` constructs to import code from other files or packages. | ||

+ | |||

+ | ### Array Indexing | ||

+ | |||

+ | Matlab and Julia use very similar notations for vector indexing, and differ only in the operator used: Matlab uses the `A(1,2)` notation, while Julia uses the `A[1,2]` notation. | ||

+ | |||

+ | ### Assignment is only Alias | ||

+ | |||

+ | Most julia objects are assigned by reference, i.e., an assignment creates an alias not a copy. | ||

+ | |||

+ | |||

+ | ### Imaginary Numbers | ||

+ | |||

+ | The square root of an `-1` is denoted by the symbol `im` in Julia. Complex numbers in general are represented by the `Complex{T}` type. The complex form of a number can be obtained using the `complex` function. | ||

+ | |||

+ | |||

+ | ### Files and functions | ||

+ | |||

+ | In Julia file names aren't associated with the functions they contain. A file may contain multiple modules and functions and can be named arbitrarily. | ||

+ | |||

+ | ### Function Declarations | ||

+ | |||

+ | Matlab uses named output arguments. Julia uses explicit return statements. The syntax for declaring anonymous functions also differs in Julia. | ||

+ | |||

+ | ```juliarepl | ||

+ | julia> function f(x,y); return(x + y); end#function | ||

+ | julia> f= (x,y)->(x + y) | ||

+ | julia> f(x,y)= (x + y) | ||

+ | ``` | ||

+ | |||

+ | |||

+ | |||

+ | |||

+ | ## Translating from Perl | ||

+ | |||

+ | In Julia, array indices begin with 1. In Perl, array indexes begin with 0. | ||

+ | |||

+ | |||

+ | ## Translating from Python [[translation:python|Python to Julia]] | ||

+ | |||

+ | |||

+ | ### Indentation and Block statements | ||

+ | |||

+ | In Python indentations are used to identify statement blocks. In Julia, the keyword `end` is often used to terminate a block of statements. | ||

+ | |||

+ | ```julia | ||

+ | function f(x,y) | ||

+ | x,y | ||

+ | end | ||

+ | ``` | ||

+ | |||

+ | ```python | ||

+ | def f(x,y): | ||

+ | return(x,y) | ||

+ | ``` | ||

+ | |||

+ | ### Array indexing | ||

+ | |||

+ | In Julia, array indices begin with 1. In Python, array indexes begin with 0. | ||

+ | |||

+ | ```juliarepl | ||

+ | julia> A = [1,2,3] | ||

+ | |||

+ | julia> A[0] | ||

+ | ERROR: BoundsError: attempt to access 3-element Array{Int64,1} at index [0] | ||

+ | ``` | ||

+ | |||

+ | |||

+ | |||

+ | # Julia and Other Computer Languages | ||

+ | |||

+ | * Common Gotchas. | ||

+ | |||

+ | * Opinionated relative language advantages and disadvantages | ||

+ | |||

+ | |||

+ | |||

+ | ### Multiple Dispatch | ||

+ | |||

+ | In Julia, methods do not belong to objects, but are implementations of generic functions. If you're used to Object oriented languages such as Python, this may seem strange. In python you'd use the `.` operator to access an objects methods. In Julia, you would pass the object as the first argument to the function. For example, to define a Class `Person` with an age accessor method in Python, you would do: | ||

+ | |||

+ | ```python | ||

+ | class Person: | ||

+ | def __init__(self, age): | ||

+ | self.age = age | ||

+ | |||

+ | def get_age(self): | ||

+ | return(self.age) | ||

+ | |||

+ | p = Person(10) | ||

+ | |||

+ | python> p.get_age() | ||

+ | ``` | ||

+ | |||

+ | ```juliarepl | ||

+ | julia> type Person | ||

+ | age::Int | ||

+ | end##type## | ||

+ | |||

+ | julia> getAge(x::Person) = x.age | ||

+ | |||

+ | julia> p = Person(10) | ||

+ | |||

+ | julia> getAge(p) | ||

+ | 10 | ||

+ | ``` | ||

translation.txt ยท Last modified: 2018/08/25 08:24 (external edit)