Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

What is the basic syntax comparison of Julia/Python/Matlab?

2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/02 Report--

This article is to share with you about the basic grammar comparison of Julia/Python/Matlab. The editor thinks it is very practical, so I share it with you. I hope you can get something after reading this article. Let's take a look at it.

I believe that many friends at the beginning of the algorithm should be using matlab to verify the theoretical model, and then Python became popular again, and many friends scrambled to learn python, but before long, a more powerful language came into our field of vision-Julia, which claims to have intuitive mathematical expressions like matlab, with the speed of C operation. I believe that many friends are ready to move, and the editor found that it is easy to confuse its syntax with other languages when learning a language or when switching back and forth between multiple languages, so today we have sorted out a comparison of the basic syntax of the three programming languages commonly used by Julia/Python/Matlab algorithm engineers, so friends can collect it and take it out when they forget a grammar.

Basic data type

Python: numbers, strings, lists, tuples, collections, dictionaries

Matlab: numbers, strings, logical values, tables, structures, cellular arrays, function handles

Julia: numbers, strings, custom types (struct/Union/Tuple/Array, etc.)

Note: many tutorials on julia are aimed at version 0.3, and there are many grammatical differences with the latest version 1.0. It is best to read the official julia documentation when learning.

Python

Matlab

Julia

Basic operation

Types

Dynamic language, which does data type checking only at run time, so there is no need to specify the data class, and the data type is written down the first time the assignment is made

Dynamic language, but you can specify types, adding types will significantly improve performance and system stability

Values have types, variables have no types

Integer number

Method of use: Xerox 1

You can use x.bit_length () to check the number of bit of x. Int type in python has no size limit, and theoretically the bit width can be infinite.

Method of use: Xerox 1

Double is the default type in matlab. You can also use int16 (x) to convert x to a 16-bit signed number.

Method of use: Xerox 1

In 64-bit systems, the default is Int64. You can check the type of x by typeof (x), or you can use Int32 (x) to convert x to Int32.

Floating point number

How to use: Xbox 1.0

Float type

How to use: Xbox 1.0

Default flexible type

How to use: Xbox 1.0

On 64-bit systems, the default is Float64, or you can convert it to Float32 with Float32 (x)

Complex number

X = complex (1par 2)

X1 = x.real

X2 = x.imag

X = 1 + 1i*2

X1 = real (x)

X2 = imag (x)

X = 1 + 2im

X1 = real (x)

X2 = imag (x)

Score

X = 0.5

X.as_integer_ratio ()

X = 0.5

Formalt rat

X = 1 Universe 2

Conversion between different binary systems

X1 = int (0b1010)

X2 = int (0x10)

X3 = hex (10)

X4 = bin (20)

X1 = bin2dec ('1010')

X2 = hex2dec ('a')

X3 = dec2hex (10)

X4 = dec2bin (10)

X1 = 10

X2 = UInt8 (x1)

X3 = Int64 (x2)

Only Int and UInt are defined for integers in Julia, in which Int is expressed in decimal and UInt in hexadecimal.

String

X1 ='a'

X2 = 'abc'

X3 = "a"

X3 = "abcd"

There is no difference between single and double quotation marks

X1 ='a'

X2 = 'abc'

Characters and strings are in single quotation marks

X1 ='a'

X2 = "abc"

Like C, characters are in single quotation marks and strings are in double quotation marks

View data type function

A = [1pm 2pm 3]

Type (a)

A = [1, 2, 3, 4, 5, 6]

Class (a)

Double

A = rand (3pr 3)

Typeof (a)

Array (Float64,2)

Matrix operation (Julia is basically similar to MATLAB in matrix operation, and the usage of functions is basically the same)

Matrix.

Merge

X = np.ones ((2pm 3))

Y = np.zeros ((2pm 3))

Np.hstack ((XBI y))

Np.vstack ((XBI y))

X = ones (2pr 3)

Y = zeros (2jue 3)

[XBI y] or [x y]

[XTX y]

X = ones (2pr 3)

Y = zeros (2jue 3)

[x y]

[XTX y]

Note: [XBI y] is the direct stitching of the matrix, not the stitching of the elements in the matrix.

Matrix index

The index of x starts at 0

The index of x starts at 1

The index of x starts at 1

Matrix dimension

X.shape

Size (x)

Size (x)

Matrix summation

X.sum ()

Sum (x)

Sum (x)

Function operation

Function

Define

Def F1 (x):

Return xrem xjin1

Function [y1ther y2] = F1 (x)

Y1 = x

Y2 = x + 1

End

Function F1 (x)

XMagnum xpen1

End

Simple form:

F (xBI y) = x + y

Special usage of julia:

+ (1, 2, 3) the result is 6

Anonymous function

Often used in conjunction with map/filter, etc.

F = lambda x _ 1 _ y _

F (2pr 3)

Filter (lambda x:x+3, [1, 2, 3, 4])

Fendy @ (xmeny) xfanty

F (2pr 3)

Map (x-> Xero1, [1Jing 2pm 3])

Variable length parameter

(default parameters and key value parameters are used the same)

* indicates that the variable length parameter is list

* * indicates that the variable length parameter is dict

Def F1 (* arg1,**arg2)

Print (arg1)

Print (arg2)

F1 (1pence2) a = [1magent2pyr3], baked 9)

The varargin in the function parameter indicates the number of variables

Function F1 (varargin)

For i=1:numel (varargin)

Disp (varargin {I})

End

F1 (1 ~ (1))

Function f (x...)

R1 = length (x)

R2 = x [R1]

Return r1,r2

End

Print (f (1, 2, 3, 4)

Parameter type

There is no need to specify, the parameter type is only obtained when the function is called

There is no need to specify, the parameter type is only obtained when the function is called

Can not be specified, or you can specify

Function F1 (x::Int64)

End

Control flow

Conditional statement

If flag

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report