[julia-users] Re: How do I maximize a function ?
Pkg.add("Optim") then using Optim and follow this section of the docs: http://www.juliaopt.org/Optim.jl/stable/user/minimization/#minimizing-a-univariate-function --John On Friday, November 18, 2016 at 7:29:28 PM UTC-8, Pranav Bhat wrote: > > How do I obtain the maximum value of a function over an interval? > > In R I would do something like: > > f <- function(x) { x ^ 2} > optimize(f, c(0, 100), maximum=TRUE) >
[julia-users] How do I maximize a function ?
How do I obtain the maximum value of a function over an interval? In R I would do something like: f <- function(x) { x ^ 2} optimize(f, c(0, 100), maximum=TRUE)
Re: [julia-users] How to enter a file using gallium?
You @enter the function, not the file. What function would you call once you include test.jl? That is the function to which you should prepend @enter. On Fri, Nov 18, 2016 at 6:49 PM, wrote: > > > Hi there, > > I'm totally new to the new debuggers. What I'm aiming at is to go through > a file (it's a file calling other packages and functions therein.) line by > line, just like in matlab. The first step is to step in the file using > gallium. I know how to traverse a function in terminal using Gallium. But > it seems that I can't enter a file by doing the same thing like: > > julia> @enter test.jl > ERROR: BoundsError: attempt to access 0-element Array{Int64,1} at index [0 > ] > > > How can I enter a file via Gallium? Is it possible at all? Thanks. >
[julia-users] Is the compiler able to give warnings that an iterator's internal state modified?
I guess the following is buggy code dict = Dict(x=>x for x in 1:100); for x in keys(dict) delete!(dict, x) end As we need to `collect(keys(...))` to make a copy. However the code above still runs with correct results. Even when it fails to return correct results, no error would be thrown. Just wondering in the language design point of view if the compiler or runtime can detect this sort of behaviour always?
[julia-users] Project organization and variable scope question
To start, I'm new to Julia and I'm trying things out to test Julia out for some scientific/engineering applications. In particular, I'm working on moderate size projects, where they're big enough that I'll need more than one file with code in it to stay organized (let's say anywhere from 5-50 files, 10s-100s of functions. However, I'm struggling to figure out an appropriate way to organize my code and ensure that the proper variables are in scope where they are needed. To start, I come from a Matlab/C#/C++/Fortran/Basic/Pascal/etc. background and have been coding for long time, so I'm a little baffled by Julia's structure. Here's the basic description of my problem. I've got a collection of (sometimes large) 1D arrays. I'll define some starting values for these arrays and then I simply iterate on them and update the values in the arrays (basically I'm solving unsteady PDE problems). A very simple program structure would look something like this (thinking in terms of a functional programming approach in Matlab/C/Fortran,): module MyProjectModule # Include some files that have functions I need include("SetSomeProblemParameters.jl"); include("DeclareASetOfArrays.jl"); include("AssignInitialValuesToArrays.jl"); include("CheckSomeValuesInSomeArrays.jl"); include("CalculateSomeValuesBasedUponArrayValues.jl"); include("UpdateValuesOfArrays.jl"); include("WriteResultsToDisk.jl"); # Make main() visible export main main(NumberOfIterations) # This is the main entry point of the code that performs a lengthy numerical calculation SetSomeProblemParameters() DeclareASetOfArrays() AssignInitialValuesToArrays() while (NumberOfIterations not reached) CheckSomeValuesInSomeArrays() CalculateSomeValuesBasedUponArrayValues() UpdateValuesOfArrays() NumberOfIterations++ end #while WriteResultsToDisk() end #main end MyProjectModule Simple, right? Question #1: If main entry point to run a calculation "main()" is a function, it gets its own variable workspace, right? Now, if I write a script (not a function) and use include("some_script.jl") with main(), does Julia just inline that code within main()? In terms of scope, should the script file be able to see all of the variables in the scope of main()? In Matlab that would be true. In Fortran/C that wouldn't. I guess, I'm not sure what scope implications there are for Julia script files. Question #2: If I've defined a bunch of functions as shown in the pseudocode above, what is the most performant way to have the large 1D arrays accessible within the scope of each function. As you can tell, I'm trying to avoid writing functions that accept a long list of input parameters. The old Fortran solution is to simply make the arrays global, so that each function can access them as needed. How terrible is that idea within the Julia framework? Also, how can I even do that? I've tried writing a script (not a function) to declare a long list of global variables and then used include("DeclareGlobalVariables,jl) within my main. But, when I return to main(), those variables do not show up in the workspace for main??? What am I missing? Question #3: I come from a VisualStudio IDE background, so I'm having trouble figuring out how to organize a Juila project. I'm trying out Atom for my first Julia tests. For a project that's bigger than just a script or a few functions, should I be defining a defining main entry point function within a module? Why Does Julia force modules to be added as packages so they can be loaded with the "using" command? That seems strange. Or, should I just write everything as a collection of files with functions in them and not worry about modules? Simple REPL and one file Julia examples are everywhere. There are also large coding projects/libraries/utilities on github as examples, but I'm having trouble figuring out the structure of these larger projects. I guess, I'm somewhere in between these two cases, where I'm just want to crunch some numbers, but I'm a little more complicated/sophisticated than the single file examples. What's the best way to proceed with such a project/file structure? Thanks in advance for any help. Nick
Re: [julia-users] Got an exception of type ErrorException outside of a @test: type DataType has no field FactorMargin
Have a look please https://github.com/hpoit/MLN.jl/tree/master/BN On Friday, November 18, 2016 at 11:48:58 AM UTC-2, Yichao Yu wrote: > > On Thu, Nov 17, 2016 at 2:39 PM, Kevin Liu > > wrote: > > Right, I need the instance of Factor > > Then use the instance of Factor. > > > > > On Thursday, November 17, 2016 at 5:33:05 PM UTC-2, Yichao Yu wrote: > >> > >> On Thu, Nov 17, 2016 at 2:27 PM, Kevin Liu wrote: > >> > I replaced Factor[:FactorMargin]() with Factor.FactorMargin() back > >> > again. > >> > > >> > Still, for FactorOperations.jl on Atom, I get {UndefVarError: Factor > not > >> > defined} at the end of each block. > >> > > >> > Factor is defined on Factor.jl, and that file evaluates fine. > >> > >> AFAICT the `Factor`is a type so `Factor.FactorMargin` is definitely > wrong. > >> > >> > > >> > The main file, BN.jl, includes Factor.jl and FactorOperations.jl and > >> > exports > >> > Factor, and also evaluates fine. > >> > > >> > On Wednesday, November 16, 2016 at 11:25:24 PM UTC-2, Yichao Yu > wrote: > >> >> > >> >> On Wed, Nov 16, 2016 at 7:24 PM, Kevin Liu > wrote: > >> >> > Hi Yichao! > >> >> > >> >> In general there's nothing from the code you posted that shows what > >> >> you want to do. > >> >> > >> >> > > >> >> > Here is the function from FactorOperations.jl > >> >> > > >> >> > function FactorDropMargin(A::Factor, Remove_var::Vector{String}) > >> >> > Remove_dims = indexin(Remove_var, A.var) > >> >> > if any(Remove_dims==0) > >> >> > error("Wrong variable!") > >> >> > end > >> >> > > >> >> > Remain_var = symdiff(A.var, Remove_var) > >> >> > Remain_dims = indexin(Remain_var, A.var) > >> >> > > >> >> > Factor[:FactorMargin](A, Remove_var, Remain_var, Remove_dims, > >> >> > Remain_dims) # line 85 > >> >> > >> >> Unless you overloaded getindex on this type (which you should > include) > >> >> you are construction a Vector of `Factor` from a symbol and then > >> >> calling it. It's impossible to tell what you actually want to do. > >> >> > >> >> And as I previously mentioned, unless you are using PyCall, the > issue > >> >> you linked is totally unrelated to this. > >> >> > >> >> > end > >> >> > > >> >> > runtests.jl: > >> >> > > >> >> > @testset "Multiply and marginalize factor" begin > >> >> > > >> >> > A=Factor(["a", "b"],[3, 2],[0.5, 0.1, 0.3, 0.8, 0, 0.9]) > >> >> > B=Factor(["b", "c"],[2, 2],[0.5, 0.1, 0.7, 0.2]) > >> >> > C = FactorProduct(A, B) > >> >> > FactorDropMargin(C, ["c"]) # line 19 > >> >> > FactorKeepMargin(C, ["b", "a"]) > >> >> > FactorPermute(ans, [2, 1]) > >> >> > FactorKeepMargin(C, ["a", "b"]) > >> >> > > >> >> > end > >> >> > > >> >> > what I got on the REPL: > >> >> > > >> >> > julia> Pkg.test("BN") > >> >> > > >> >> > INFO: Testing BN > >> >> > > >> >> > Test Summary: | > >> >> > > >> >> > Define, permute factor, and call (var, card, val) | No tests > >> >> > > >> >> > Multiply and marginalize factor: Error During Test > >> >> > > >> >> > Got an exception of type ErrorException outside of a @test > >> >> > > >> >> > type DataType has no field FactorMargin > >> >> > > >> >> >in FactorDropMargin(::BN.Factor, ::Array{String,1}) at > >> >> > /Users/Corvus/.julia/v0.5/BN/src/FactorOperations.jl:85 > >> >> > > >> >> >in macro expansion; at > >> >> > /Users/Corvus/.julia/v0.5/BN/test/runtests.jl:19 > >> >> > [inlined] > >> >> > > >> >> >in macro expansion; at ./test.jl:672 [inlined] > >> >> > > >> >> >in anonymous at ./:? > >> >> > > >> >> >in include_from_node1(::String) at ./loading.jl:488 > >> >> > > >> >> >in include_from_node1(::String) at > >> >> > > >> >> > > >> >> > > /Applications/Julia-0.5.app/Contents/Resources/julia/lib/julia/sys.dylib:? > >> >> > > >> >> >in process_options(::Base.JLOptions) at ./client.jl:262 > >> >> > > >> >> >in _start() at ./client.jl:318 > >> >> > > >> >> >in _start() at > >> >> > > >> >> > > >> >> > > /Applications/Julia-0.5.app/Contents/Resources/julia/lib/julia/sys.dylib:? > >> >> > > >> >> > Test Summary: | Error Total > >> >> > > >> >> > Multiply and marginalize factor | 1 1 > >> >> > > >> >> > > >> >> > On Wednesday, November 16, 2016 at 10:02:48 PM UTC-2, Yichao Yu > >> >> > wrote: > >> >> >> > >> >> >> On Wed, Nov 16, 2016 at 6:50 PM, Kevin Liu > wrote: > >> >> >> > From this issue https://github.com/JuliaPy/PyPlot.jl/issues/157 > I > >> >> >> > understand > >> >> >> > >> >> >> ^^ This is irrelevant unless you are using PyCall > >> >> >> > >> >> >> > > >> >> >> > `Factor[:FactorMargin](A, Remove_var, Remain_var, Remove_dims, > >> >> >> > Remain_dims)` > >> >> >> > (line 85 of FactorOperations.jl) should pass, as it does on > Atom, > >> >> >> > but > >> >> >> > not on > >> >> >> > the REPL, which throws > >> >> >> > > >> >> >> > Got an exce
[julia-users] How to enter a file using gallium?
Hi there, I'm totally new to the new debuggers. What I'm aiming at is to go through a file (it's a file calling other packages and functions therein.) line by line, just like in matlab. The first step is to step in the file using gallium. I know how to traverse a function in terminal using Gallium. But it seems that I can't enter a file by doing the same thing like: julia> @enter test.jl ERROR: BoundsError: attempt to access 0-element Array{Int64,1} at index [0] How can I enter a file via Gallium? Is it possible at all? Thanks.
Re: [julia-users] Got an exception of type ErrorException outside of a @test: type DataType has no field FactorMargin
On Thu, Nov 17, 2016 at 2:39 PM, Kevin Liu wrote: > Right, I need the instance of Factor Then use the instance of Factor. > > On Thursday, November 17, 2016 at 5:33:05 PM UTC-2, Yichao Yu wrote: >> >> On Thu, Nov 17, 2016 at 2:27 PM, Kevin Liu wrote: >> > I replaced Factor[:FactorMargin]() with Factor.FactorMargin() back >> > again. >> > >> > Still, for FactorOperations.jl on Atom, I get {UndefVarError: Factor not >> > defined} at the end of each block. >> > >> > Factor is defined on Factor.jl, and that file evaluates fine. >> >> AFAICT the `Factor`is a type so `Factor.FactorMargin` is definitely wrong. >> >> > >> > The main file, BN.jl, includes Factor.jl and FactorOperations.jl and >> > exports >> > Factor, and also evaluates fine. >> > >> > On Wednesday, November 16, 2016 at 11:25:24 PM UTC-2, Yichao Yu wrote: >> >> >> >> On Wed, Nov 16, 2016 at 7:24 PM, Kevin Liu wrote: >> >> > Hi Yichao! >> >> >> >> In general there's nothing from the code you posted that shows what >> >> you want to do. >> >> >> >> > >> >> > Here is the function from FactorOperations.jl >> >> > >> >> > function FactorDropMargin(A::Factor, Remove_var::Vector{String}) >> >> > Remove_dims = indexin(Remove_var, A.var) >> >> > if any(Remove_dims==0) >> >> > error("Wrong variable!") >> >> > end >> >> > >> >> > Remain_var = symdiff(A.var, Remove_var) >> >> > Remain_dims = indexin(Remain_var, A.var) >> >> > >> >> > Factor[:FactorMargin](A, Remove_var, Remain_var, Remove_dims, >> >> > Remain_dims) # line 85 >> >> >> >> Unless you overloaded getindex on this type (which you should include) >> >> you are construction a Vector of `Factor` from a symbol and then >> >> calling it. It's impossible to tell what you actually want to do. >> >> >> >> And as I previously mentioned, unless you are using PyCall, the issue >> >> you linked is totally unrelated to this. >> >> >> >> > end >> >> > >> >> > runtests.jl: >> >> > >> >> > @testset "Multiply and marginalize factor" begin >> >> > >> >> > A=Factor(["a", "b"],[3, 2],[0.5, 0.1, 0.3, 0.8, 0, 0.9]) >> >> > B=Factor(["b", "c"],[2, 2],[0.5, 0.1, 0.7, 0.2]) >> >> > C = FactorProduct(A, B) >> >> > FactorDropMargin(C, ["c"]) # line 19 >> >> > FactorKeepMargin(C, ["b", "a"]) >> >> > FactorPermute(ans, [2, 1]) >> >> > FactorKeepMargin(C, ["a", "b"]) >> >> > >> >> > end >> >> > >> >> > what I got on the REPL: >> >> > >> >> > julia> Pkg.test("BN") >> >> > >> >> > INFO: Testing BN >> >> > >> >> > Test Summary: | >> >> > >> >> > Define, permute factor, and call (var, card, val) | No tests >> >> > >> >> > Multiply and marginalize factor: Error During Test >> >> > >> >> > Got an exception of type ErrorException outside of a @test >> >> > >> >> > type DataType has no field FactorMargin >> >> > >> >> >in FactorDropMargin(::BN.Factor, ::Array{String,1}) at >> >> > /Users/Corvus/.julia/v0.5/BN/src/FactorOperations.jl:85 >> >> > >> >> >in macro expansion; at >> >> > /Users/Corvus/.julia/v0.5/BN/test/runtests.jl:19 >> >> > [inlined] >> >> > >> >> >in macro expansion; at ./test.jl:672 [inlined] >> >> > >> >> >in anonymous at ./:? >> >> > >> >> >in include_from_node1(::String) at ./loading.jl:488 >> >> > >> >> >in include_from_node1(::String) at >> >> > >> >> > >> >> > /Applications/Julia-0.5.app/Contents/Resources/julia/lib/julia/sys.dylib:? >> >> > >> >> >in process_options(::Base.JLOptions) at ./client.jl:262 >> >> > >> >> >in _start() at ./client.jl:318 >> >> > >> >> >in _start() at >> >> > >> >> > >> >> > /Applications/Julia-0.5.app/Contents/Resources/julia/lib/julia/sys.dylib:? >> >> > >> >> > Test Summary: | Error Total >> >> > >> >> > Multiply and marginalize factor | 1 1 >> >> > >> >> > >> >> > On Wednesday, November 16, 2016 at 10:02:48 PM UTC-2, Yichao Yu >> >> > wrote: >> >> >> >> >> >> On Wed, Nov 16, 2016 at 6:50 PM, Kevin Liu wrote: >> >> >> > From this issue https://github.com/JuliaPy/PyPlot.jl/issues/157 I >> >> >> > understand >> >> >> >> >> >> ^^ This is irrelevant unless you are using PyCall >> >> >> >> >> >> > >> >> >> > `Factor[:FactorMargin](A, Remove_var, Remain_var, Remove_dims, >> >> >> > Remain_dims)` >> >> >> > (line 85 of FactorOperations.jl) should pass, as it does on Atom, >> >> >> > but >> >> >> > not on >> >> >> > the REPL, which throws >> >> >> > >> >> >> > Got an exception of type ErrorException outside of a @test >> >> >> > >> >> >> > type DataType has no field FactorMargin >> >> >> > >> >> >> >in FactorDropMargin(::BN.Factor, ::Array{String,1}) at >> >> >> > /Users/Corvus/.julia/v0.5/BN/src/FactorOperations.jl:85 >> >> >> >> >> >> Impossible to tell without code. >> >> >> >> >> >> > >> >> >> > >> >> >> > Help, please.