[julia-users] scope of the expression (made with Expr)

2015-05-06 Thread Evan Pu
Hi, just a quick question on the scope/environment of an expression.

I am using the Expr construct inside a function to create an expression, 
and I would then want to use this expression in various different places. 
Here's a short example

First we have a simple function funky that takes in a function ff, and 
applies it to two symbolic arguments :x and :y
julia function funky(ff::Function)
 Expr(:call, ff, :x, :y)
   end
funky (generic function with 1 method)


We define x and y to be 1 and 2 respectively
julia x,y = 1,2
(1,2)


No surprise here, when funky is passed with the function +, it adds x and 
y together
julia eval(funky(+))
3


However, now I want to use this expression with different bindings for x 
and y, I attempt the following
julia function funfun(x, y)
 eval(funky(+))
   end
funfun (generic function with 1 method)


Which doesn't seem to work. Here the result is sitll 3, presumably it is 
using the earlier bindings of x=1, y=2
julia funfun(4,5)
3

I would really liked the last result to be 9.


So far it seems the expression uses static scoping and it's looking up the 
binding for :x and :y in the frame generated by calling funky, and not 
finding it there, it looks up :x and :y in the 
global frame, which is 1 and 2.
Is there a way to bind the symbols in an expression to different things? is 
there like an environment dictionary where we can mess with, or is there 
some clean way of doing it?

Much much much appreciated!!!
--evan


[julia-users] how do I overwrite the toString equivalent in Julia?

2015-02-09 Thread Evan Pu
I have a type with a lot of fields, but when I want to print it I only want 
to output the name field of that type.
So when I print an array of objects of this type, I get a on the prinout an 
array of names instead of a huge clutter of prints.

how might I do that?
much thanks!!


[julia-users] pair-wise operation on an array?

2015-02-07 Thread Evan Pu
say I want to compute a pair-wise diff for all the elements in the array.
input:[1, 2, 4, 7, 8]
output:[1, 2, 3, 1]

is there some kind of beautiful way of doing it? i.e. w/o using a for 
loop nor using explicit indecies


Re: [julia-users] pair-wise operation on an array?

2015-02-07 Thread Evan Pu
i see, maybe my example is too special.
What i meant is I have an array of elements and I have a binary operator 
which I want to apply to every adjacent elements.

On Saturday, February 7, 2015 at 7:26:55 AM UTC-5, Tamas Papp wrote:

 diff([1, 2, 4, 7, 8]) 

 I don't think it gets more beautiful than this :D 

 Best, 

 Tamas 

 On Sat, Feb 07 2015, Evan Pu evanth...@gmail.com javascript: wrote: 

  say I want to compute a pair-wise diff for all the elements in the 
 array. 
  input:[1, 2, 4, 7, 8] 
  output:[1, 2, 3, 1] 
  
  is there some kind of beautiful way of doing it? i.e. w/o using a for 
  loop nor using explicit indecies 



[julia-users] incomplete println? (and debugging memory in general)

2015-02-04 Thread Evan Pu
I'm trying to find a memory bug where all of a sudden my program freezes 
and eats up all the memory.
I tried to do some printing, and tried to understand which line of code is 
responsible for causing the memory leak to happen.
The print statement looks like this:

println(computing cost for..., fact.f_name,  , dom)

where fact is an object with a name, f_name, which is a string, and 
dom is simply a list of tuple of numbers.
Usually, this line of code will output something like this in the console:

computing cost for...f_inte1 [(7.2109375,7.21875)]

Indicating that it is about to start computing the cost for the fact 
object. However, when this memory bug occurs, it only prints this:

computing cost for...^CERROR: interrupt

It does not give me the name of fact before the bug happens, nor does it 
attempt to print out the dom. It doesn't even give me a stack trace!


I am very clueless on what could be happening. So here is my 3 questions:
1) What could cause a print statement to not finish printing? the fact 
objects are all already allocated, and all with a valid f_name field.
2) If there is some memory error where I am forced to ^C before my computer 
freezes, how can I recover a call-stack?
the answer here I think... is I am using the @profile macro so something 
happened that prevented the call-stack from showing.

3) Is there some way of knowing which part of the program is consuming most 
of the memory at the time of the bug? I tried using 
--track-allocation=all, but I do not care about memories that are already 
re-claimed by the garbage collector, but I do care about memory yet to be 
claimed at the time of the error.

Sorry for the long post...
--evan


[julia-users] julia array type?

2015-01-19 Thread Evan Pu
I'm trying to construct an array while making an array of pairs of floats. 
The error below is extremely confusing and I have no idea why.


=
# create the array w/o type declarations works fine
julia x = [(0.0, 1.0), (0.0, 1.0)]
2-element Array{(Float64,Float64),1}:
 (0.0,1.0)
 (0.0,1.0)

# but declaring the type in the constructor breaks it?!
julia y = (Float64, Float64)[(0.0, 1.0), (0.0, 1.0)]
ERROR: `getindex` has no method matching 
getindex(::Type{(Float64,Float64)}, ::(Float64,Float64), 
::(Float64,Float64))

# but somehow manually adding with push! works?!
julia z = (Float64, Float64)[]
0-element Array{(Float64,Float64),1}

julia push!(z, (0.0, 1.0))
1-element Array{(Float64,Float64),1}:
 (0.0,1.0)

julia push!(z, (0.0, 1.0))
2-element Array{(Float64,Float64),1}:
 (0.0,1.0)
 (0.0,1.0)



[julia-users] Re: What's your favourite editor?

2015-01-19 Thread Evan Pu
light table juno plugin.
literally cannot use the language w/o it haha, although it is buggy at times

On Saturday, January 17, 2015 at 11:34:46 AM UTC-5, Terry Seaward wrote:

 Hi,

 Just curious to know what people use to write/edit their Julia code 
 (os/app)?



[julia-users] Re: Simple type constructor question

2015-01-15 Thread Evan Pu
very handy, just saw this reply now.
thanks a ton

On Thursday, November 6, 2014 at 5:16:31 PM UTC-5, Valentin Churavy wrote:

 You could using a abstract type instead of a Union

 abstract Element
 type Tree 
   body :: Element
 end

 type Branch : Element
   a :: Tree
   b :: Tree
 end

 type Leaf : Element
   a
 end

 so  this would create a tree
 julia Tree(Branch(
Tree(Leaf(:a)), 
Tree(Branch(
   Tree(Leaf(:b)),
   Tree(Leaf(:c))
))
   ))
 Tree(Branch(Tree(Leaf(:a)),Tree(Branch(Tree(Leaf(:b)),Tree(Leaf(:c))

 adding the following methods makes it a bit more readable

 Tree(a :: Any) = Tree(Leaf(a))
 Tree(a :: Tree,b::Tree) = Tree(Branch(a, b))

 julia Tree(
 Tree(:a), 
 Tree(
  Tree(:b),
  Tree(:c)
   )
 )
 Tree(Branch(Tree(Leaf(:a)),Tree(Branch(Tree(Leaf(:b)),Tree(Leaf(:c))


 So this stills looks a bit clunky and you should also be aware that this 
 allows for Tree(Tree(:a), Tree(1.0)) so some type constraints would be in 
 order.


 On Thursday, 6 November 2014 21:52:05 UTC+1, Evan Pu wrote:

 Quick question:

 In haskell one can do something like the following to define a type:

  data Tree a = Branch (Tree a) (Tree a) | Leaf a


 Is there something analogous in the Julia world?
 I'm sure I'm doing something wrong here...

 julia type Tree
body :: Union(Branch, Leaf)
end
 ERROR: Branch not defined

 julia type Branch
a :: Tree
b :: Tree
end
 ERROR: Tree not defined

 julia type Leaf
a
end

 thanks!



[julia-users] what do you do with strings in julia

2015-01-13 Thread Evan Pu
what is the convention? I kept getting
`convert` has no method matching convert(::Type{SubString(UTF8String)}}, 
::ASCIIString)
all the time, every time

I I don't really know _why_ this error comes up, or how or what. I just 
kept typing :: ASCIIString trying to force the types onto things and 
hopefully it goes away, so of course I'm doing something wrong.
thanks~


[julia-users] Re: what do you do with strings in julia

2015-01-13 Thread Evan Pu
Steven,
The error is actually an issue with LightTable's Juno plugin and actually 
has nothing to do with Julia.
I think what happened is in reporting the errors the plugin makes some 
mistake and ended up reporting another error(or itself had some misuse of 
strings) instead. 
The actual error was discovered while running it from the command shell is 
in fact:
ERROR: assertion failed
 in ∫ at SimplePoly.jl:286
 in ∫ at SimplePoly.jl:518
 in ∫ at SimplePoly.jl:786
 in ∫ at Factor.jl:153
which seems more reasonable, as the assertion is the one I wrote.

sorry for the troubles!



On Tuesday, January 13, 2015 at 3:16:44 PM UTC-5, Steven G. Johnson wrote:

 Though we could define such a conversion method with:

 convert{T:AbstractString}(::Type{SubString{T}}, s::AbstractString) = 
 let s′ = T(s); SubString(s′, 1, endof(s′)); end

 Evan, what is your application here?  What are you trying to do?



[julia-users] Re: what do you do with strings in julia

2015-01-13 Thread Evan Pu
I'll try. the call chain is rather large and I'll see if I can get it down 
to a few constructs.

On Tuesday, January 13, 2015 at 4:34:39 PM UTC-5, Steven G. Johnson wrote:



 On Tuesday, January 13, 2015 at 4:10:54 PM UTC-5, Evan Pu wrote:

 Steven,
 The error is actually an issue with LightTable's Juno plugin and actually 
 has nothing to do with Julia.


 Can you file an issue at:

 https://github.com/one-more-minute/Jewel.jl

 explaining how the error arose? 



[julia-users] initialize array of arrays?

2015-01-08 Thread Evan Pu
Imagine I want to initialize an array of array.
x = [[1,2],[3,4]]

but this gets flattened into [1,2,3,4]

Is there an easy way of constructing a nested array? I'm currently having 
to do
x = Array{Int64}[]
push!(x, [1,2])
push!(x, [3,4])
which doesn't seem very clean


[julia-users] Re: initialize array of arrays?

2015-01-08 Thread Evan Pu
works for me! thanks!!

On Thursday, January 8, 2015 1:07:35 PM UTC-5, Steven G. Johnson wrote:

 You can do Array{Int}[[1,2],[3,4]], or Vector{Int}[[1,2],[3,4]] if you 
 want to restrict the entries to be 1d arrays of Int.



Re: [julia-users] Re: package proposal

2014-12-26 Thread Evan Pu
is this thread still alive? a phc package would be good...

On Monday, June 23, 2014 5:26:34 AM UTC-7, Andrei Berceanu wrote:

 By the way I recently stumbled upon NLSolve.jl, and asked them if they 
 would be interested in PHCpack
 https://github.com/EconForge/NLsolve.jl/issues/12

 Still no reply on their part yet though.

 On Monday, June 23, 2014 1:06:03 PM UTC+2, Andrei Berceanu wrote:

 Free binary versions for Mac and Windows of the gnu-ada compiler are 
 available at http://libre.adacore.com/ and are very easy to install.
 As for HOMPACK, the main difference is that PHCpack is specifically 
 targeted for polynomial systems. HOMPACK provides continuation methods for 
 general nonlinear systems and has extra drivers for polynomial systems. 
 Another main difference is that PHCpack offers polyhedral homotopies, which 
 are absent from HOMPACK. So I guess it depends on what we want really, 
 personally I am interested in polynomial systems. Please let me know if you 
 succeed in installing the ada compiler for Mac from the above link.


 On Friday, June 20, 2014 11:36:01 PM UTC+2, Tony Kelman wrote:

 That sounds like the best plan for the PHCpack code. Have you looked at 
 or are you familiar with whether HOMPACK as Hans mentioned would be able to 
 provide similar functionality? I say that just because more of us are used 
 to building Fortran code than Ada. Ada should be reasonable to work with on 
 Linux, and maybe even in MinGW or Cygwin, but it doesn't look like it's set 
 up in Homebrew for Mac users. If you know of a standard way to install GNAT 
 and can get the library building on at least your platform of choice, go 
 for it.


 On Friday, June 20, 2014 2:14:18 PM UTC-7, Andrei Berceanu wrote:

 I have contacted the author (
 https://github.com/janverschelde/PHCpack/issues/3) and it seems 
 redistribution under a different license is not really an option.
 However, if what Milan says is correct, then we should be able to 
 include it 'as-is'.
 Now for the interface, I was thinking that, since there already exists 
 a comprehensive C API to the Ada code, we could call that from Julia, what 
 do you guys reckon?

 On Friday, June 6, 2014 2:18:33 PM UTC+2, Milan Bouchet-Valat wrote:

  Le vendredi 06 juin 2014 à 03:43 -0700, Hans W Borchers a écrit : 

 Please notice that PHCpack is distributed under GPL license, so your 
 first 

  step should be to contact the author and ask for his approval to 
 distribute  

  it under a lesser license such as MIT. 


 Though since it's a package rather than code to be included in Julia 
 Base, GPL is fine too.


 Regards 



[julia-users] how to test NaN in an array?

2014-12-15 Thread Evan Pu
1 in [1,2,3] # returns true

NaN in [NaN, 1.0, 2.0] # returns false

how do I test if a float64 NaN is present in an array? I'm doing some 
numerical computation and it can have some NaN error, I want to drop the 
arrays that has NaN.


Re: [julia-users] how to test NaN in an array?

2014-12-15 Thread Evan Pu
come to think of it that is the right semantics.
well designed language this is!

On Monday, December 15, 2014 3:35:22 PM UTC-5, John Myles White wrote:

 You need to check isnan() per element. NaN == NaN is false, so in() fails 
 on NaN right now. 

  -- John 

 On Dec 15, 2014, at 3:33 PM, Evan Pu evanth...@gmail.com javascript: 
 wrote: 

  1 in [1,2,3] # returns true 
  
  NaN in [NaN, 1.0, 2.0] # returns false 
  
  how do I test if a float64 NaN is present in an array? I'm doing some 
 numerical computation and it can have some NaN error, I want to drop the 
 arrays that has NaN. 



[julia-users] customized hash function (or equality function)?

2014-12-10 Thread Evan Pu
Hi, this should be a simple affair.

I have a polynomial object that keeps track of its coefficients, it is 
defined as follows:

immutable Poly1
  coef :: Array{Float64}
end

I would like to make a dictionary with keys that are Poly1 type, like 
follows:

# creation
r_dict1 = [Poly1([1.0, 2.0]) = 1]
# access
r_dict1[Poly1([1.0, 2.0])]

However currently it gives the arrow key not found
By looking at the hash we can see why, the has is not identical even for 
equal polynomials:
hash(Poly1([1.0, 2.0])) # gives an arbitrary hex string
hash(Poly1([1.0, 2.0])) # gives a different hex string

How would I implement my own hash so the same polynomials (by same I mean 
the coefficients are same, i.e. the coef::Array{Float64} part of the two 
polynomials are the same?

Much thanks!!
--evan


[julia-users] Re: customized hash function (or equality function)?

2014-12-10 Thread Evan Pu
wonderful! thanks!!

On Wednesday, December 10, 2014 8:24:17 PM UTC-5, Steven G. Johnson wrote:

 Base.hash(p::Poly1, h::Uint) = hash(p.coef, h)
 Base.(==)(p1::Poly1, p2::Poly2) = p1.coef == p2.coef

 (If you override hash you should always override == to be consistent, and 
 vice versa.)



[julia-users] Simple type constructor question

2014-11-06 Thread Evan Pu
Quick question:

In haskell one can do something like the following to define a type:

 data Tree a = Branch (Tree a) (Tree a) | Leaf a


Is there something analogous in the Julia world?
I'm sure I'm doing something wrong here...

julia type Tree
   body :: Union(Branch, Leaf)
   end
ERROR: Branch not defined

julia type Branch
   a :: Tree
   b :: Tree
   end
ERROR: Tree not defined

julia type Leaf
   a
   end

thanks!


Re: [julia-users] type confusions in list comprehensions (and how to work around it?)

2014-11-04 Thread Evan Pu
It does indeed happens inside the function, if you pass a function as an 
argument to it (rather than refering to f implicitly in the function body, 
you explicitly pass in f as an extra argument)
see below:

julia f(x) = x + 1
f (generic function with 1 method)

julia g(f, xs) = [f(x) for x in xs]
g (generic function with 1 method)

julia xs = [1,2,3]
3-element Array{Int64,1}:
 1
 2
 3

julia g(f,xs)
3-element Array{Any,1}:
 2
 3
 4


On Tuesday, November 4, 2014 2:22:24 AM UTC-5, Jutho wrote:

 This only happens in global scope, not inside a function? If you define
 f(list) = return [g(x) for x in list]

 then f(xs) will return an Array{Float64,1}. 

 Op dinsdag 4 november 2014 03:23:36 UTC+1 schreef K leo:

 I found that I often have to force this conversion, which is not too 
 difficult.  The question why comprehension has to build with type Any? 


 On 2014年11月04日 07:06, Miguel Bazdresch wrote: 
   How could I force the type of gxs1 to be of an array of Float64? 
  
  The simplest way is: 
  
  gxs1 = Float64[g(x) for x in xs] 
  
  -- mb 
  
  On Mon, Nov 3, 2014 at 6:01 PM, Evan Pu evanth...@gmail.com 
  mailto:evanth...@gmail.com wrote: 
  
  Consider the following interaction: 
  
  julia g(x) = 1 / (1 + x) 
  g (generic function with 1 method) 
  
  julia typeof(g(1.0)) 
  Float64 
  
  julia xs = [1.0, 2.0, 3.0, 4.0] 
  4-element Array{Float64,1}: 
   1.0 
   2.0 
   3.0 
   4.0 
  
  julia gxs1 = [g(x) for x in xs] 
  4-element Array{Any,1}: 
   0.5 
   0.33 
   0.25 
   0.2 
  
  Why isn't gxs1 type of Array{Float64,1}? 
  How could I force the type of gxs1 to be of an array of Float64? 
  
  julia gxs2 = [convert(Float64,g(x)) for x in xs] 
  4-element Array{Any,1}: 
   0.5 
   0.33 
   0.25 
   0.2 
  
  somehow this doesn't seem to work... 
  
  
  
  



[julia-users] Is there an implicit apply method for a type?

2014-11-04 Thread Evan Pu
Hello,
I want to create a polynomial type, parametrized by its coefficients, able 
to perform polynomial additions and such.
but I would also like to use it like a function call, since a polynomial 
should be just like a function
Something of the following would be nice:

p = Poly([1,2,3]) # creating a polynomial p(x) = 1 + 2x + 3x^2
q = Poly([1,2]) # creating a polynomial 1 + 2x
r = p + q # using dynamic dispatch, creating the polynomial 2 + 4x + 3x^2

r(1) # this should give 9, by evaluating polynomial r at x = 1

Is there something in Julia that would allow me to create what I have in 
mind?
thanks!!


[julia-users] base case for the reduce operator?

2014-11-04 Thread Evan Pu
Hi I'm writing a simple polynomial module which requires addition of 
polynomials.

I have defined the addition by overloading the function + with an 
additional method:
+(p1::Poly, p2::Poly) = ...# code for the addition

I would like to use + now in a reduce call, imagine I have a list of 
polynomials [p1, p2, p3],
calling reduce(+, [p1, p2, p3]) behaves as expected, giving me a polynomial 
that's the sum of the 3

however, I would also like to cover the edge cases where there's only a 
single polynomial or no polynomial.
I would like the following behaviours, :

# reducing with a single polynomial list gives just the polynomial back
reduce(+, [p1]) 
 p1

# reducing with an empty list gives back the 0 polynomial
reduce(+, [])
 ZeroPoly

How might I add such functionality?
For the empty list case, how might I annotate the type so Julia is aware 
that [] means an empty list of polynomials rather than an empty list of Any?


Re: [julia-users] Fully Typed Function As Argument Type

2014-11-03 Thread Evan Pu
Nooo TT I was looking for it too today.
Hope it gets added soon, fingers crossed!

On Thursday, August 14, 2014 10:31:17 AM UTC-4, John Myles White wrote:

 Not possible in the current versions of Julia. Maybe one day. There are 
 bunch of us who’d like to have this functionality, but it’s a non-trivial 
 addition of complexity to the compiler.

  — John

 On Aug 14, 2014, at 4:59 AM, Chris Kellendonk chriske...@gmail.com 
 javascript: wrote:

 I'm new to the language so I may have missed something. But is there a way 
 to make sure a function passed as an argument is of a specific type 
 including it's arguments? It doesn't look like from the design of the 
 language this could be supported but I'm curious.

 For example I would like to be able to do:

 function callme(handler::Function(Int32, Float64))
 handler(12, 0.2)
 end

 And the compiler would guarantee the correct type of function is called.

 Thanks,




[julia-users] type confusions in list comprehensions (and how to work around it?)

2014-11-03 Thread Evan Pu
Consider the following interaction:

julia g(x) = 1 / (1 + x)
g (generic function with 1 method)

julia typeof(g(1.0))
Float64

julia xs = [1.0, 2.0, 3.0, 4.0]
4-element Array{Float64,1}:
 1.0
 2.0
 3.0
 4.0

julia gxs1 = [g(x) for x in xs]
4-element Array{Any,1}:
 0.5 
 0.33
 0.25
 0.2 

Why isn't gxs1 type of Array{Float64,1}?
How could I force the type of gxs1 to be of an array of Float64?

julia gxs2 = [convert(Float64,g(x)) for x in xs]
4-element Array{Any,1}:
 0.5 
 0.33
 0.25
 0.2   

somehow this doesn't seem to work...





[julia-users] how to find out how many argument a function has?

2014-10-16 Thread Evan Pu
I'm assuming the function only has a single method, although a more general 
answer would be nice too.
Imagine I have:
f(x,y,z) = x + y + z

I would like to have something like
num_args(f)
which should give back 3

is there something that could let me do this?
thanks a lot!!


[julia-users] how to i get number of arguments of a function?

2014-10-16 Thread Evan Pu
How do I get the number of arguments of a function?

for instance, f(x, y) = x + y

I want something like num_args(f), which will give me back 2. If the 
function has multiple methods then something more general would be nice, 
but so far I only care about functions with just a single method.

Is there something like this?
thanks alot!!


Re: [julia-users] how to i get number of arguments of a function?

2014-10-16 Thread Evan Pu
thanks!!

On Thursday, October 16, 2014 12:41:22 PM UTC-4, Tim Holy wrote:

 You might want to look into the various introspection functions, like 
 `methods`, `code_lowered`, etc. Leah Hanson had a nice blog post: 
 http://blog.leahhanson.us/julia-introspects.html 

 For your specific problem, once you have a specific method, then `m.sig` 
 returns 
 its signature, and you can just compute the length. A MethodTable 
 (returned by 
 `methods(sum)`, for example) is iterable, so you can iterate over the 
 table 
 until you find what you want. 

 Best, 
 --Tim 

 On Thursday, October 16, 2014 08:55:01 AM Evan Pu wrote: 
  How do I get the number of arguments of a function? 
  
  for instance, f(x, y) = x + y 
  
  I want something like num_args(f), which will give me back 2. If the 
  function has multiple methods then something more general would be nice, 
  but so far I only care about functions with just a single method. 
  
  Is there something like this? 
  thanks alot!! 



[julia-users] named function within a loop is overwritten?

2014-10-16 Thread Evan Pu
Consider this code:

function give_funs()
  funs = []
  for i in 1:5
function newfun()
  i
end
funs = [funs, newfun]
  end
  funs
end

The intention is to create 5 functions and store them in a list called 
funs.
All the functions take no argument, and when the ith function is called, it 
returns i.

However, when run...
funs1 = give_funs()
funs1[1]() # this should give 1, but instead it gives 5
funs1[2]() # this should give 2, but instead it gives 5 as well

This is problem goes away if I stop naming the function as newfun but 
instead use ananymous functions like so:
funs = [funs, () - i]
however in real code I would like to give it a name so to be more clear 
what the function is suppose to compute

It seems that these functions are bound by their function names, and since 
they're all named newfun, the compiler over-write the old ones defined 
earlier.

How should this be resolved?


Re: [julia-users] how to i get number of arguments of a function?

2014-10-16 Thread Evan Pu
yeah, the function I'm intending to inspect will be defined by me and they 
shouldn't be varargs

On Thursday, October 16, 2014 2:10:34 PM UTC-4, Toivo Henningsson wrote:

 Though you should probably look out for varargs methods too, the length of 
 e.g. (Int...) is one, but a method with that signature can take any number 
 of arguments.