Variables
A variable, in Julia, is a name associated (or bound) to a value. It's useful when you want to store a value (that you obtained after some math, for example) for later use. For example:
# Assign the value 10 to the variable x
julia> x = 10
10
# Doing math with x's value
julia> x + 1
11
# Reassign x's value
julia> x = 1 + 1
2
# You can assign values of other types, like strings of text
julia> x = "Hello World!"
"Hello World!"
Base.Rounding.setrounding
Method
setrounding(T, mode)
Set the rounding mode of floating point type T
, controlling the rounding of basic arithmetic functions (+
, -
, *
, /
and sqrt
) and type conversion. Other numerical functions may give incorrect or invalid values when using rounding modes other than the default RoundNearest
.
Note that this may affect other types, for instance changing the rounding mode of Float64
will change the rounding mode of Float32
. See RoundingMode
for available modes.
Base.log
Method
log(b,x)
Compute the base b
logarithm of x
. Throws DomainError
for negative Real
arguments.
julia> log(4,8)
1.5
julia> log(4,2)
0.5
Note
If b
is a power of 2 or 10, log2
or log10
should be used, as these will typically be faster and more accurate. For example,
julia> log(100,1000000)
2.9999999999999996
julia> log10(1000000)/2
3.0
Julia provides an extremely flexible system for naming variables. Variable names are case-sensitive, and have no semantic meaning (that is, the language will not treat variables differently based on their names).
which |
type of eigenvalues |
---|---|
:LM |
eigenvalues of largest magnitude (default) |
:SM |
eigenvalues of smallest magnitude |
:LR |
eigenvalues of largest real part |
:SR |
eigenvalues of smallest real part |
:LI |
eigenvalues of largest imaginary part (nonsymmetric or complex A only) |
:SI |
eigenvalues of smallest imaginary part (nonsymmetric or complex A only) |
:BE |
compute half of the eigenvalues from each end of the spectrum, biased in favor of the high end. (real symmetric A only)
|
julia> x = 1.0
1.0
julia> y = -3
-3
julia> Z = "My string"
"My string"
julia> customary_phrase = "Hello world!"
"Hello world!"
julia> UniversalDeclarationOfHumanRightsStart = "人人生而自由,在尊严和权利上一律平等。"
"人人生而自由,在尊严和权利上一律平等。"
Unicode names (in UTF-8 encoding) are allowed:
In the Julia REPL and several other Julia editing environments, you can type many Unicode math symbols by typing the backslashed LaTeX symbol name followed by tab. For example, the variable name δ
can be entered by typing \delta
-tab, or even α̂₂
by \alpha
-tab-\hat
- tab-\_2
-tab. (If you find a symbol somewhere, e.g. in someone else's code, that you don't know how to type, the REPL help will tell you: just type ?
and then paste the symbol.)
Julia will even let you redefine built-in constants and functions if needed:
julia> pi
π = 3.1415926535897...
julia> pi = 3
WARNING: imported binding for pi overwritten in module Main
3
julia> pi
3
julia> sqrt(100)
10.0
julia> sqrt = 4
WARNING: imported binding for sqrt overwritten in module Main
4
However, this is obviously not recommended to avoid potential confusion.
Allowed Variable Names
Variable names must begin with a letter (A-Z or a-z), underscore, or a subset of Unicode code points greater than 00A0; in particular, Unicode character categories Lu/Ll/Lt/Lm/Lo/Nl (letters), Sc/So (currency and other symbols), and a few other letter-like characters (e.g. a subset of the Sm math symbols) are allowed. Subsequent characters may also include ! and digits (0-9 and other characters in categories Nd/No), as well as other Unicode code points: diacritics and other modifying marks (categories Mn/Mc/Me/Sk), some punctuation connectors (category Pc), primes, and a few other characters.
Operators like +
are also valid identifiers, but are parsed specially. In some contexts, operators can be used just like variables; for example (+)
refers to the addition function, and (+) = f
will reassign it. Most of the Unicode infix operators (in category Sm), such as ⊕
, are parsed as infix operators and are available for user-defined methods (e.g. you can use const ⊗ = kron
to define ⊗
as an infix Kronecker product).
Matrix type | + |
- |
* |
\ |
Other functions with optimized methods |
---|---|---|---|---|---|
Hermitian |
MV | inv() , sqrtm() , expm() |
|||
UpperTriangular |
MV | MV | inv() , det() |
||
LowerTriangular |
MV | MV | inv() , det() |
||
SymTridiagonal |
M | M | MS | MV | eigmax() , eigmin() |
Tridiagonal |
M | M | MS | MV | |
Bidiagonal |
M | M | MS | MV | |
Diagonal |
M | M | MV | MV | inv() , det() , logdet() , /() |
UniformScaling |
M | M | MVS | MVS | /() |
The only explicitly disallowed names for variables are the names of built-in statements:
julia> else = false
ERROR: syntax: unexpected "else"
julia> try = "No"
ERROR: syntax: unexpected "="
Some Unicode characters are considered to be equivalent in identifiers. Different ways of entering Unicode combining characters (e.g., accents) are treated as equivalent (specifically, Julia identifiers are NFC-normalized). The Unicode characters ɛ
(U+025B: Latin small letter open e) and µ
(U+00B5: micro sign) are treated as equivalent to the corresponding Greek letters, because the former are easily accessible via some input methods.
Math Examples
Some math x^2 here and \sqrt{2} there. What about a tiny fraction \tfrac{1}{2}, does that work well? Let's make the paragraph a bit longer. What about some block of maths?! x = \frac{-b \pm \sqrt{b^2 - 4ac}}{2a} That should work just fine!
Stylistic Conventions
While Julia imposes few restrictions on valid names, it has become useful to adopt the following conventions:
-
Names of variables are in lower case.
-
Word separation can be indicated by underscores (
'_'
), but use of underscores is discouraged unless the name would be hard to read otherwise. -
Names of
Type
s andModule
s begin with a capital letter and word separation is shown with upper camel case instead of underscores. -
Names of
function
s andmacro
s are in lower case, without underscores. -
Functions that write to their arguments have names that end in
!
. These are sometimes called "mutating" or "in-place" functions because they are intended to produce changes in their arguments after the function is called, not just return a value.
For more information about stylistic conventions, see the Style Guide.