I just install Julia, along with LinearAlgebra package.
When I type
A = [1 2 3 4; 4 5 6 7]
rref(A)
My terminal said
ERROR: UndefVarError: rref not defined
Stacktrace:
[1] top-level scope at REPL[22]:1
I am sort of confused. Do I need to install certain package in order to use reef function?
There s no such function in the LinearAlgebra package. There used to be a long time ago, but is has since moved to https://github.com/blegat/RowEchelon.jl.
Related
I want to turn an array like this
[1,2,3,4,5]
into a lagged version
[missing,1,2,3,4] # lag 1
[missing,missing,1,2,3] # lag 2
or a led version
[2,3,4,5,missing] # lead 1
[3,4,5,missing,missing] # lead 2
As Julia is designed for scientific computing, there must be something like this, right?
Add ShiftedArrays. See: https://discourse.julialang.org/t/ann-shiftedarrays-and-support-for-shiftedarrays-in-groupederrors/9162
Quoting from the above:
lag, lead functions, to shift an array and add missing (or a custom default value in the latest not yet released version) where the data is not available, or circshift for shifting circularly in a lazy (non allocating) way:
julia> v = [1.2, 2.3, 3.4]
3-element Array{Float64,1}:
1.2
2.3
3.4
julia> lag(v)
3-element ShiftedArrays.ShiftedArray{Float64,Missings.Missing,1,Array{Float64,1}}:
missing
1.2
2.3
Note the ShiftedArray version of lag keeps the array size the same. You might add a short function to make it behave the way you asked:
biglag(v, n) = lag(vcat(v, v[1:n]), n)
I saw in someone code that they were using the + operator as if it was a function by doing +(1,2,3). Is it possible to use operators as functions in Julia?
In addition, I also saw things like A ⊗ B, where the behaviour of ⊗ was customizable. How can I do such a thing, and is there a list of symbols I can use in this way?
Yes, you indeed can use operators as functions in Julia.
From the Julia Docs:
In Julia, most operators are just functions with support for special syntax. (The exceptions are operators with special evaluation semantics like && and ||. These operators cannot be functions since Short-Circuit Evaluation requires that their operands are not evaluated before evaluation of the operator.) Accordingly, you can also apply them using parenthesized argument lists, just as you would any other function:
julia> 1 + 3 + 5
9
julia> +(1,3,5)
9
julia> 1 * 3 * 5
15
julia> *(1,3,5)
15
julia> h = *;
julia> h(1,3,5)
15
In addition, Julia allows you to define your own meaning to operators, and makes quite a few symbols available for the purpose. You can find the list of available symbols here:|
https://github.com/JuliaLang/julia/blob/master/src/julia-parser.scm
and define them as such:
⊗(a, b) = a * 3 - b # or some other arbitrary thing
a ⊗ b == a * 3 - b # true
I am currently staring with Julia 1.0.0 under Windows 10. When using a function randperm(n) where n is an integer number, I got an error message:
ERROR: UndefVarError: randperm not defined.
Presumably, I have not loaded a library that contains this function. So, could anyone please tell me which library should I load so that I can use that function? Thank you in advance!
Nha
You need to load standard library Random, e.g.:
julia> using Random
julia> randperm(10)
10-element Array{Int64,1}:
6
3
7
10
2
8
5
9
4
1
In Matlab, there is a 1-D filter function http://www.mathworks.com/help/matlab/ref/filter.html .
In R's signal package, the description of its filter function states: Generic filtering function. The default is to filter with an ARMA filter of given coefficients. The default filtering operation follows Matlab/Octave conventions.
However, the answers don't match if I give the same specification.
In MATLAB (correct answer):
x=[4 3 5 2 7 3]
filter(2/3,[1 -1/3],x,x(1)*1/3)
ans =
4.0000 3.3333 4.4444 2.8148 5.6049 3.8683
In R, if I follow Matlab/Octave's convention (incorrect answer):
library(signal)
x<-c(4,3,5,2,7,3)
filter(2/3,c(1,-1/3),x,x[1]*1/3)
Time Series:
Start = 1
End = 6
Frequency = 1
[1] 3.111111 3.037037 4.345679 2.781893 5.593964 3.864655
I tried a lot of other examples too. R's signal package's filter function doesn't appear to follow the Matlab/Octave conventions even though the document states it so. Perhaps, I'm using the filter function incorrectly in R. Can someone help me?
I believe the answer is in the documentation (shock!!!!)
matlab:
The filter is a "Direct Form II Transposed"
implementation of the standard difference equation:
a(1)*y(n) = b(1)*x(n) + b(2)*x(n-1) + ... + b(nb+1)*x(n-nb)
- a(2)*y(n-1) - ... - a(na+1)*y(n-na)
If a(1) is not equal to 1, filter normalizes the filter coefficients by a(1).
[emphasis mine]
R:
a[1]*y[n] + a[2]*y[n-1] + … + a[n]*y[1] = b[1]*x[n] + b[2]*x[m-1] + … + b[m]*x[1]
Thanks for lifting this issue a couple of years back... I bumped into it as well and think I got an answer. Essentially I think the optimization algos are different for R and Matlab.
If no guess is provided (that is, set the initial values to default which is zero for both R and Matlab), the results are very similar.
R
library(signal)
x<-c(4,3,5,2,7,3)
filter(2/3,cbind(1,-1/3),x, 0.00)
2.666667 2.888889 4.296296 2.765432 5.588477 3.862826
Matlab
x=[4 3 5 2 7 3]
filter(2/3,[1 -1/3],x,0.00)
2.6667 2.8889 4.2963 2.7654 5.5885 3.8628
Now, if we start tweaking the initial guess of the parameters, then the results will diverge.
R
library(signal)
x<-c(4,3,5,2,7,3)
filter(2/3,cbind(1,-1/3),x, 0.05)
2.683333 2.894444 4.298148 2.766049 5.588683 3.862894
Matlab
x=[4 3 5 2 7 3]
filter(2/3,[1 -1/3],x,0.05)
2.7167 2.9056 4.3019 2.7673 5.5891 3.8630
Hope it helps!
I am trying to switch from Mathematica to IJulia for data exploration, and I was wondering if there's a n analogue for the following Mathematica one-liner:
ListPlot[Import["/tmp/output.tsv"], Joined -> True]
output.tsv is a tab delineated list of (X,Y) pairs
Here's a lame attempt:
In [1]: using Gadfly; plot(readdlm("/tmp/output.tsv", '\t', Float64))
no method plot(Array{Float64,2},)
at In[1]:1
Gadfly will accept arrays, but you need to specify the x and y values. Also, you need to pass an aesthetic.
julia> a = [1 2 3; 4 5 6]
2x3 Array{Int64,2}:
1 2 3
4 5 6
julia> plot(a)
ERROR: no method plot(Array{Int64,2})
julia> plot(x=a[1,:], y=a[2,:], Geom.line)
Here is a screenshot from REPL (not IJulia):
You'd probably have to read it into a dataframe (DataFrames.readtable), since that's what Gadfly operates on. Other plotting packages such as Winston operate on raw data, but since you are reading structure data anyway, the DataFrames approach is probably best.