A = [4. -1.; 1. 2.] b = [1., 2.] x = A\b # solve A*x = b A*x i = 103491; typeof(i) a = 1. + 3im b = complex(2., 1.) a/b n = 17 c = 's' s = "tip "*string(n)*": string"*string(c)*" are concatenated with *" log(-1.) log(complex(-1)) using LinearAlgebra λ, Ψ = eigen(A) # eigenvalue decomposition, with two separate return values A⁻¹= inv(A) c = rand(); if c <= 0.49999 println("heads"); elseif c >= 0.50001 println("tails"); else println("side"); end (rand() <= 0.5 ? "heads" : "tails") for i = 1:10 print(i, " "); end p = 0; while p < 3 && p > -5 p += rand([-1,1]); println(p, " ", abs(p), " ", sign(p)); end y = zeros(size(A,2)); for i = 1:size(A,1) for j = 1:size(A,2) y[i] += A[i,j] * x[j]; end end y x = range(0., stop=1., length=20); x.^4 .* exp.(-x.^2/2) A = [1. 2. ; 3. 4.] A[1,:] A[:,1] = [5, 6]; A arr1 = Array{Array{Float64,1},2}(undef, 2, 2) arr1[1,1] = Array{Float64,1}(); arr1[1,2] = Float64[]; arr1[2,1] = Array{Float64,1}([1., 2.]); arr1[2,2] = Float64[2., 3.]; arr1 arr2 = [1, 1., "1", [1.]] a = [1., 1.]; b = a; # b references the same array as a c = copy(a); # c is a copy of a b[1] = 2.; println("a = ", a, "\nb = ", b, "\nc = ", c) t = (1, 2) t = 1, 2 x, y = 1, 2; # same as (x,y) = (1,2) println("x = ", x, ", y = ", y); y, x = x, y; # swap x and y println("x = ", x, ", y = ", y); MixedSet = Set(); push!(MixedSet,"abc"); push!(MixedSet,π); push!(MixedSet,9); MixedSet π in MixedSet SparseIntSet = Set{Int}([-2411, 1022981,9]); push!(SparseIntSet, 3); for i in SparseIntSet if i ∈ MixedSet println(i); end end DenseIntSet = BitSet([1,3,5,7,11]) # implemented by bit vectors, for non-sparse sets union(SparseIntSet,DenseIntSet) # analogously: intersect, symdiff, ... D = Dict{Tuple{Int,Int},Float64}((1,2)=>π, (1,0)=>ℯ); D[(9,9)] = 0.; D (1,1) ∈ keys(D) D[(1,0)] A = [1., 2., 3.]; for a ∈ A print(a, " "); end println(""); B = BitSet([13, 5, 2, 9]); for b ∈ B print(b, " "); end typeof(1:10) typeof(0:0.1:1) collect(0:0.1:1) for (i, x) in enumerate(B) println(i, ": ", x); end V = [1, 2, 3]; W = [4, 5, 6]; for (x,y) in zip(V, W) println(x, ", ", y); end [n^2 for n=1:5] [1.0/(i+j) for i = 1:3, j = 1:6] sum(i for i=1:10) Dict(x => sin(π*x) for x in 0:.5:2.) function f(x,y) y*cos(x), y*sin(x) end f(π/3, 2) function newtonstep(f::Array, Df::Array, x::Array) println("using vector definition"); return x - Df\f end function newtonstep(f::Number, Df::Number, x::Number) println("using scalar definition"); return x - f/Df end newtonstep([1,1], [2. -1.; -1. 2.], [0.,0.]) newtonstep(1, 2., 0) f(x) = x^2; g = x->x^3; f(2), g(2) h = x->(cos(x),sin(x)); h.([π, π/3, π/8]) map(t->t[1], [h(i*π/10) for i = 0:10]) SomeSet = Set([1, 2]); empty!(SomeSet) function setone!(v) fill!(v, 1.); end A = [2. 2.; 2. 2.]; setone!(A[:,1]); A setone!(view(A,:,1)); A abstract type RoundShape end mutable struct Ellipse <: RoundShape center::Tuple{Float64,Float64} A::Real B::Real end mutable struct Circle <: RoundShape center::Tuple{Float64,Float64} r::Real end getcenter(S::RoundShape) = S.center; area(S::Ellipse) = π * S.A * S.B; area(S::Circle) = π * S.r^2; Ell = Ellipse((1., 0.), 2, 1); getcenter(Ell), area(Ell) @time s = 0; for i=1:1e6 s += i^3; end; print("result: ", s); @time s = sum(map(x->x^3, 1:1e6)); print("result: ", s); @code_native sin(π/2)^2 using Printf println(@sprintf "iteration %d: error %e, step size %e" 139 2.34232131e-5 1.290137e-6) using Pkg Pkg.add("StaticArrays"); using StaticArrays Pkg.add("Revise"); using Revise Pkg.add("PyPlot"); using PyPlot x = range(1e-2,stop=1,length=10000); plot(x, sin.(1.0./x)); Pkg.add("SymPy"); using SymPy x, y = symbols("x, y", real=true); diff(x^y*exp(y*exp(x*y)) - x*y + 1, y) rand([1, 4, 16]) rand("abc") rand(3,3,3) rand(Int, 2, 2, 2) using Random A = zeros(5) rand!(view(A,1:3)) A rng = MersenneTwister(5312) rand!(rng, A) # similar effect as calling seed!(5312), but this does not change global random number generator bitrand(3, 3, 3) G = randn(1000000); using Plots; gr() histogram(G, nbins=200)