julia
How do you format a string when interpolated in Julia?
You can use @sprintf macro from the standard library package Printf. This returns a string rather than just printing it as @printf. using Printf x = 1.77715 print(“I’m long: $x, but I’m alright: $(@sprintf(“%.2f”, x))”) Output: I’m long: 1.77715, but I’m alright: 1.78
How to select elements from array in Julia matching predicate?
You can use a very Matlab-like syntax if you use a dot . for elementwise comparison: julia> a = 2:7 2:7 julia> a .> 4 6-element BitArray{1}: false false false true true true julia> a[a .> 4] 3-element Array{Int32,1}: 5 6 7 Alternatively, you can call filter if you want a more functional predicate approach: … Read more
How do I select a random item from a weighted array in Julia?
Use the StatsBase.jl package, i.e. Pkg.add(“StatsBase”) # Only do this once, obviously using StatsBase items = [“a”, 2, 5, “h”, “hello”, 3] weights = [0.1, 0.1, 0.2, 0.2, 0.1, 0.3] sample(items, Weights(weights)) Or if you want to sample many: # With replacement my_samps = sample(items, Weights(weights), 10) # Without replacement my_samps = sample(items, Weights(weights), 2, … Read more
set the random seed in julia generator of random numbers
Updated answer, for Julia 0.7 onwards. import Random Random.seed!(1234) dim = 5 A = randn(dim,dim) H = (A + A’)/sqrt(2) Previous answer, for Julia 0.6 and earlier. You are looking for the srand function, e.g. srand(1234) dim = 5 A = randn(dim,dim) H = (A + A’)/sqrt(2) Will always produce the same results.
Julia: Convert numeric string to float or int
You can parse(Float64,”1″) from a string. Or in the case of a vector map(x->parse(Float64,x),stringvec) will parse the whole vector. BTW consider using tryparse(Float64,x) instead of parse. It returns a Nullable{Float64} which is null in the case string doesn’t parse well. For example: isnull(tryparse(Float64,”33.2.1″)) == true And usually one would want a default value in case … Read more
Julia: How to copy data to another processor in Julia
I didn’t know how to do this at first, so I spent some time figuring it out. Here are some functions I wrote to pass objects: sendto Send an arbitrary number of variables to specified processes. New variables are created in the Main module on specified processes. The name will be the key of the … Read more
Why is operating on Float64 faster than Float16?
As you can see, the effect you are expecting is present for Float32: julia> rnd64 = rand(Float64, 1000); julia> rnd32 = rand(Float32, 1000); julia> rnd16 = rand(Float16, 1000); julia> @btime $rnd64.^2; 616.495 ns (1 allocation: 7.94 KiB) julia> @btime $rnd32.^2; 330.769 ns (1 allocation: 4.06 KiB) # faster!! julia> @btime $rnd16.^2; 2.067 μs (1 allocation: … Read more
Julia DataFrame: remove column by name
You can use select!: julia> df = DataFrame(A = 1:4, B = [“M”, “F”, “F”, “M”], C = 2:5) 4×3 DataFrame |——-|—|—–|—| | Row # | A | B | C | | 1 | 1 | “M” | 2 | | 2 | 2 | “F” | 3 | | 3 | 3 | … Read more
What on earth is an inner constructor?
By the way of example, suppose you want to define a type to represent even numbers: julia> struct Even e::Int end julia> Even(2) Even(2) So far so good, but you also want the constructor to reject odd numbers and so far Even(x) does not: julia> Even(3) Even(3) So you attempt to write your own constructor … Read more