At the moment of running the model the "Normal" says that it is not defined. However, the idea is that it is a function that indicates random numbers of normal distribution with specific mean and variance.
The original code was made in Julia V0.5.2 but Julia 1.0.3 mentions that "Normal" does not exist.
n=5000;
t=15000;
lambda=0.8;
sigmae1=0.05;
sigmae2=0.1;
sigmaz= 0.013;
n_lambda= trunc(Int, lambda*n)
eshocks1=rand(Normal(0.0,sigmae1), n_lambda, t);
eshocks2=rand(Normal(0.0,sigmae2), n - n_lambda, t);
zshocks =rand(Normal(0.0, sigmaz),1, t);
UndefVarError: Normal not defined
Stacktrace:
[1] top-level scope at In[5]:21
add Distributions, then put
using Distributions
at the top of the code, for use with newer Julia versions. The later versions of Julia tend to have non-Base functions as optional modules that need to be added to the installation.
Related
I have a program for doing Fourier series and I wanted to switch to CuArrays to make it faster. The code is as follows (extract):
#Arrays I want to use
coord = CuArray{ComplexF64,1}(complex.(a[:,1],a[:,2]))
t=CuArray{Float64,1}(-L:(2L/(N-1)):L)
#Array of indexes in the form [0,1,-1,2,-2,...]
n=[((-1)^i)div(i,2) for i in 1:grado]
#Array of functions I need for calculations
base= [x -> exp(π * im * i * x / L) / L for i in n]
base[i](1.) #This line is OK
base[i](-1:.1:1) #This line is OK
base[i].(t) #This line gives error!
base[i].(CuArray{Float64,1}(t)) #This line gives error!
And the error is:
GPU broadcast resulted in non-concrete element type Any.
This probably means that the function you are broadcasting contains an error or type instability.
If I change it like this
base= [(x::Float64) -> (exp(π * im * i * x / L) / L)::ComplexF64 for i in n]
the same lines still give error, but the error now is:
UndefVarError: parameters not defined
Any idea how I could fix this?
Thank you in advance!
Package information:
(#v1.6) pkg> st CUDA
Status `C:\Users\marce\.julia\environments\v1.6\Project.toml`
[052768ef] CUDA v2.6.2
P.S.: This other function has the same problem:
function integra(inizio, fine, arr)
N=size(arr,1)
h=(fine-inizio)/N
integrale=sum(arr)
integrale -= (first(arr)+last(arr))/2
integrale *= h
end
L=2
integra(-L,L,coord)
The first and easier problem is that you should take care to declare global variables to be constant so that the compiler can assume a constant type: const L = 2. A mere L = 2 allows you to do something like L = SomeOtherType(), and if that type can be Anything, so must the return type of your functions. On the CPU that's only a performance hit, but it's a no-no for the GPU. If you actually want L to vary in value, pass it in as an argument so the compiler can still infer types within a function.
Your ::ComplexF64 assertion did actually force a concrete return type, though the middle of the function is still type unstable (check with #code_warntype). The second problem you ran into after that patch was probably caused by this recently patched conflict between ExprTools.jl and LLVM.jl. Seems like you just need to update the packages or maybe reinstall them.
I know that there are a bunch of different implementations of a specific method in my code and I want to see a list of all of them. How can I see all the methods with a specific name?
A very convenient way of using methods() is to type the method name followed by a ( and then type TAB at the REPL, so for your example:
rand(
and then hit the TAB key. The list for rand( is very long though. If you continue writing your function call with arguments and hit TAB again, the list will be filtered according to all matching methods. In your case:
julia> rand(1,
rand(dims::Integer...) in Random at C:\Julia\Julia-1.4.0\share\julia\stdlib\v1.4\Random\src\Random.jl:277
rand(X) in Random at C:\Julia\Julia-1.4.0\share\julia\stdlib\v1.4\Random\src\Random.jl:258
rand(X, dims::Tuple{Vararg{Int64,N}} where N) in Random at C:\Julia\Julia-1.4.0\share\julia\stdlib\v1.4\Random\src\Random.jl:280
rand(X, d::Integer, dims::Integer...) in Random at C:\Julia\Julia-1.4.0\share\julia\stdlib\v1.4\Random\src\Random.jl:283
EDIT:
Internally, Julia calls
methods(rand, (typeof(1), Any))
which would be the according filtering method in a piece of code (the docs unfortunatately do not include an example yet)
In Julia Base, there is a methods function which works as follows:
julia> methods(rand) # where rand is the the function name in question
# 68 methods for generic function "rand":
[1] rand(rd::Random.RandomDevice, sp::Union{Random.SamplerType{Bool}, Random.SamplerType{Int128}, Random.SamplerType{Int16}, Random.SamplerType{Int32}, Random.SamplerType{Int64}, Random.SamplerType{Int8}, Random.SamplerType{UInt128}, Random.SamplerType{UInt16}, Random.SamplerType{UInt32}, Random.SamplerType{UInt64}, Random.SamplerType{UInt8}}) in Random at /Users/sabae/buildbot/worker/package_macos64/build/usr/share/julia/stdlib/v1.3/Random/src/RNGs.jl:29
[2] rand(::Random._GLOBAL_RNG, x::Union{Random.SamplerType{Int128}, Random.SamplerType{Int64}, Random.SamplerType{UInt128}, Random.SamplerType{UInt64}}) in Random at /Users/sabae/buildbot/worker/package_macos64/build/usr/share/julia/stdlib/v1.3/Random/src/RNGs.jl:337
#...etc
This function allows us to see all of the functions matching the name we passed in.
It is also worth noting that the scope of this function can change depending on what packages you are using in the moment. See in the example below where I load in the POMDPs package and the number of available rand functions jumps significantly.
julia> using POMDPs
julia> methods(rand)
# 170 methods for generic function "rand":
[1] rand(rd::Random.RandomDevice, sp::Union{Random.SamplerType{Bool}, Random.SamplerType{Int128}, Random.SamplerType{Int16}, Random.SamplerType{Int32}, Random.SamplerType{Int64}, Random.SamplerType{Int8}, Random.SamplerType{UInt128}, Random.SamplerType{UInt16}, Random.SamplerType{UInt32}, Random.SamplerType{UInt64}, Random.SamplerType{UInt8}}) in Random at /Users/sabae/buildbot/worker/package_macos64/build/usr/share/julia/stdlib/v1.3/Random/src/RNGs.jl:29
#.. ETC.
Read more about the methods function in the Julia Docs.
Given a function object f, how do I find:
Function's name.
Module(s) of its method(s)?
In Julia 0.4 I was able to find name using f.env.name, but no tips for module. For Julia 0.5 I wasn't able to find any of two.
Name is easy: Symbol(f) or string(f) if you want a string
Module is, as you know going to be per method (i.e per type signature).
methods(f) with return a method table that prints out all the methods and where they are, in terms of files.
You can do [meth.module for meth in methods(f)] to get there modules
So to use an example, the collect function.
julia> using DataStructures #so we have some non-Base definitions
julia> Symbol(collect)
:collect
julia> methods(collect)
# 5 methods for generic function "collect":
collect(r::Range) at range.jl:813
collect{T}(::Type{T}, itr) at array.jl:211
collect(itr::Base.Generator) at array.jl:265
collect{T}(q::DataStructures.Deque{T}) at /home/ubuntu/.julia/v0.5/DataStructures/src/deque.jl:170
collect(itr) at array.jl:236
julia> [meth.module for meth in methods(collect)]
5-element Array{Module,1}:
Base
Base
Base
DataStructures
Base
julia> first(methods(collect, (Deque,))).module
DataStructures
#oxinabox's answer is correct. To add, typeof(f).name.mt.name is the v0.5 replacement for f.env.name. That can be useful to avoid the . that occurs when just applying string to a function introduced in a non-stdlib module. There also exists Base.function_name(f) which is probably less likely to break when the Julia version changes.
To get the module that a function (type) is introduced in, rather than the modules of individual methods, there's typeof(f).name.module, or the probably-better version, Base.function_module(f). The module of the method table is probably the same; that can be obtained through typeof(f).name.mt.module.
Note that f.env in v0.4 is a direct equivalent of typeof(f).name.mt, so on v0.4 the same f.env.name and f.env.module apply.
In Julia 1.x the commands are:
Base.nameof
Base.parentmodule
Since this two methods are exported, also nameof and parentmodule works.
Not exactly the answer but closely related: you can find the file name and line number of f using functionloc(f)
I am using this tcl version
/tools/tcl/8.4.11/linux64/bin/tcl
May i know how can i use the built in power function in my tcl script?
I tried this but it does not work.
namespace import ::tcl::mathfunc::*
puts [pow 10 2]
unknown namespace in import pattern "::tcl::mathfunc::*"
while executing
"namespace import ::tcl::mathfunc::\*"
May i know why?
That feature was introduced in Tcl 8.5. In 8.4 and before, functions were implemented using a special API that nobody really understood (passing around pointers to Tcl_Value structures, which were rather strange).
The pow() function is a floating point function in all versions of Tcl (even if written as a call to ::tcl::mathfunc::pow). Integer exponentiation is only available via the ** operator (requires Tcl 8.5, which was also the first version to support arbitrary size integers):
puts [expr { 10 ** 2 }]
# Note the namespace
puts [tcl::mathop::** 10 2]
namespace import tcl::mathop::*
puts [** 10 2]
The ** operator is exactly equivalent to pow() if either of its arguments is a floating point number (and will indeed call the same function in the standard C math library behind the scenes).
The ::tcl::mathfunc namespace was added in Tcl 8.5 and is not supported in Tcl 8.4. In Tcl 8.5+ you can use
namespace import ::tcl::mathfunc::*
pow 10 2
# -> 100.0
as well as calling pow from within expr, but in Tcl 8.4 and earlier you must use
expr {pow(10, 2)}
# -> 100.0
The mathfunc commands, when present, can be invoked as regular Tcl commands or be called as pseudofunctions in the expression script passed to expr. In the latter case, C-style syntax with parantheses and commas is used. The point of having them is mainly that it makes it easier to extend expr with new math functions. As a sideeffect, invoking expr can be avoided altogether for the simplest calculations.
The pow function returns a floating-point value, just as in C. As Donal Fellows describes in his answer (q.v.), integer exponentiation can be specified by using the ** operator (which is also a separate command: tcl::mathop::**). This operator is Tcl 8.5+ only.
Documentation: expr, mathfunc, mathop
I am new to Julia Lang. I am coming from the background of Matlab.
In Matlab, when pressing whos command I will get all variables in the current scope; and also, I can store them in another variable like x=whos; Is there such commands exists in Julia?
Example code in Matlab:
>> a=3;
>> b=4;
>> whos
Variables in the current scope:
Attr Name Size Bytes Class
==== ==== ==== ===== =====
a 1x1 8 double
b 1x1 8 double
prefix 1x16 16 char
Total is 18 elements using 32 bytes.
An Update:
whos()
... is not working either in iJulia or at the command prompt in Julia-1.0.0.
It is working in Julia-0.6.4, though.
On the other hand,
varinfo()
....prints information about the exported global variables in a module. For Example,
julia-1.0> varinfo()
name size summary
–––––––––––––––– ––––––––––– –––––––––––––––––––––––––––––––
Base Module
Core Module
InteractiveUtils 154.271 KiB Module
Main Module
PyPlot 781.872 KiB Module
ans 50.323 KiB Plots.Plot{Plots.PyPlotBackend}
myrepl 0 bytes typeof(myrepl)
x 88 bytes 1×6 Array{Int64,2}
y 0 bytes typeof(y)
Hope, this is found useful.
You can use Julia's whos functions just like that Matlab command.
julia> whos()
Base Module
Core Module
Main Module
ans Nothing
julia> x = 5
5
julia> whos()
Base Module
Core Module
Main Module
ans Int64
x Int64
Any modules (packages/libraries) you import into your local scope (using using) will also show up in the list (as Modules, like Base, Core, and Main above).
Additionally, you can ask about names exported by Modules. Base is the module containing the standard library.
julia> whos(Base)
! Function
!= Function
!== Function
$ Function
% Function
& Function
* Function
+ Function
.... (lots and lots more)
Considering that that result scrolls way off my screen, you can understand why you'd want to filter the results. For that you can use Regexes. (For more info on Julia's regexes, see this manual section)
julia> whos(r"M")
Main Module
julia> whos(Base, r"Match"i)
DimensionMismatch DataType
RegexMatch DataType
each_match Function
eachmatch Function
ismatch Function
match Function
matchall Function
I wasn't aware of the whos function before you asked, so thanks for helping me learn something new too. :)
Julia issue #3393 on github is about adding memory sizes to the whos output. It also references making whos return a value rather than just printing the information out.
Not sure if there is something better, but
names(Main)[4:end]
seems to work. The [4:end] part is because it includes :Main, :Core and :Base which I think you would not want. I hope they will always be at the beginning.
whos() is not available in newer versions of Julia (1.0 onward). Use varinfo() instead. For example, varinfo(Core,r".*field.*")
As of version 1.1 there is also the #locals macro
The experimental macro Base.#locals returns a dictionary of current local variable names and values
Release notes