function mysquare(x) x * x end # or alternatively just: mysquare(x) = x * x mysquare(3) # Integer arithmetic mysquare(2im) # Complex arithmetic typeof(["abc"]) typeof([1, 2, 3]) typeof([1.1, 2, 3]) typeof(["abc", 1, 2.3]) arr = [1.1, 2, 3] mysquare.(arr) # Vectorised call sin.(exp.(mysquare.(arr))) # or: @. sin(exp(mysquare(arr))) using Printf function printarray(array::Array{T, 1}) where T println("General array passed, elements:") for element in array println(" \$element") end end function printarray(array::Array{T, 1}) where T <: Number println("Numeric array passed, elements:") for element in array println(" \$element") end end function printarray(array::Array{Float64, 1}) println("Float array passed, elements:") for element in array @printf " %10.5f\n" element end end printarray(["a", "b", 1]) println() printarray([1, 2, 3]) println() printarray([1., 2., 3.]) Type = Float16 a = rand(Type, 3, 4) sum(mysquare.(a)) using StaticArrays # Array operations on static-sized arrays: a = @SArray[1,2,3,4] mysquare.(a) using SparseArrays # random sparse array a = sprandn(6, 6, 0.3) mysquare.(a) using Distributed # Add 4 processes and setup environment at each of them #addprocs(4) @everywhere begin mysquare(x) = x * x using DistributedArrays end a = drandn(100,100,10) println(sum(mysquare.(a))) close(a) struct MyPoint x::Float64 y::Float64 end p = MyPoint(1, 2) p.x = 6 pointlength(p::MyPoint) = sqrt(p.x^2 + p.y^2) pointlength(p) using StaticArrays @show AbstractArray{Float64} <: AbstractArray @show SArray <: AbstractArray @show Array{Float64} <: AbstractArray{Float64} @show Array{Int64} <: AbstractArray{Float64}; # Function, which works on all Abstract Arrays of element type Float64 function strangesum(a::AbstractArray{Float64}) a[1] + sum(a[2:end]) end # ok: strangesum([1., 2., 3.]) strangesum(@SArray[1., 2., 3.]) # not ok: strangesum([1, 2, 3]) using Zygote using SpecialFunctions f(x) = 2x*sin(x) - x^2/2 @time f'(1) @time f'(2) # Call to lapack to compute dot product v = [1.0, 2.0, 3.0, 4.0] w = [2.0, 3.0, 4.0, 5.0] VdotW = ccall((:ddot_, "liblapack"), Float64, (Ref{Int32}, Ptr{Float64}, Ref{Int32}, Ptr{Float64}, Ref{Int32}), length(v), v, 1, w, 1) println("v ⋅ w = \$VdotW") # Plot function in matplotlib using PyPlot # f defined as above x = collect(-4:0.2:7) PyPlot.plot(x, f.(x), label="f(x) = 2x*sin(x) - x^2/2 ") PyPlot.plot(x, f'.(x), label="f'(x)") PyPlot.axhline(0, color="grey", linewidth=0.5) PyPlot.legend() PyPlot.show()