[julia-users] Composition of setindex!

2016-08-20 Thread Po Choi

julia> a = zeros(5)
5-element Array{Float64,1}:
 0.0
 0.0
 0.0
 0.0
 0.0

julia> a[1:3][1:2] = 1.0
1.0

julia> a
5-element Array{Float64,1}:
 0.0
 0.0
 0.0
 0.0
 0.0

I understand that `a[1:3]` creates an extra array and then `a[1:3][1:2] = 
1.0` modified the extra array rather than the origin `a`.

Is it an intended behavior?
I come from R where `a[1:3][1:2] = 1.0` works as I expect...



[julia-users] Any suggested mono font for julia with unicode?

2016-07-12 Thread Po Choi
I find that some mono fonts doesn't display distinguishable greek letters 
in unicode.
Any suggested mono font for julia with unicode?


[julia-users] Abstract type and parametric type in multiple dispatch

2016-06-14 Thread Po Choi
julia> abstract Human

julia> immutable Man <: Human
   x::ASCIIString
   end

julia>

julia> john = Man("John")
Man("John")

julia> function yo(h::Human)
   println("yo ", h.x)
   end
yo (generic function with 1 method)

julia> yo(john)
yo John

julia> function yo{T <: Human}(h::T)
   println("yo yo yo ", h.x)
   end
yo (generic function with 2 methods)

julia> yo(john)
yo yo yo John


The two definitions
yo(h::Human)
yo{T <: Human}(h::T)
which one is prefered?

The second definition has higher priority.
Are those two definitions actually the same?



Re: [julia-users] Re: Does it make sense to loop over types?

2016-06-10 Thread Po Choi
Thanks for pointing out the mistakes.

I want to loop over the data types because I want to loop over functions.
I need to do something like this:
function mywork(x, y, z)
  procedure1(x, y, z)
  procedure2(x, y, z)
  procedure3(x, y, z)
  #...
  procedure8(x, y, z)
  procedure9(x, y, z)
  procedure10(x, y, z)
end

I feel it may be silly to write out all the procedurek(x, y, z)
Then I am thinking about the following:
function mywork(x, y, z)
  for k in 1:10
procedure(::Val{k}, x, y, z)
  end
end

I have not seen anyone doing this before. So I am not sure I am doing 
something right.
That's why I am asking this question.

I remember people like to use macro to define functions for different types.
In my case, should I use macro? Or am I fine to code as above?


On Friday, June 10, 2016 at 7:34:09 PM UTC-7, Erik Schnetter wrote:
>
> Your code won't work. `Val{k}` is a type, which is an object in Julia. You 
> are passing this object to `foo`. Thus in your declaration of `foo`, you 
> need to list the type of `Val`, not just `Val` -- types have types as well:
> ```
> foo(::Type{Val{1}}) = 1
> ```
>
> Of course you know that using `Val` in this way is nonsensical in a real 
> program. I understand that you know this, as you're purposefully 
> experimenting with Julia, but I'd still like to point it out for the casual 
> reader of this example.
>
> Whether you encounter "performance issues" or not depends on what 
> performance you need. If you compare this code to simple arithmetic 
> operations (adding numbers), then it's slower. If you compare it to sending 
> data across the network or accessing the disk, then it's faster.
>
> I assume that calling `foo` in the loop requires a hash table lookup at 
> run time, and likely some memory allocation.
>
> -erik
>
>
> On Fri, Jun 10, 2016 at 9:40 PM, Po Choi <vegeta...@gmail.com 
> > wrote:
>
>> Ops! I accidentally hit the post button! So the post is not completed!
>>
>> It is an example:
>> foo(::Val{1}) = 1
>> foo(::Val{2}) = 2
>> foo(::Val{2}) = 3
>>
>> function bar()
>>   s = 0
>>   for t in Datatype[Val{k} for k in 1:3]
>> s += foo(t)
>>   end
>> end
>>
>> Will there be any performance issue if I loop over types?
>> I am still trying to understand how the multiple-dispatch works. 
>> Sometimes I am confused!
>>
>>
>> On Friday, June 10, 2016 at 6:37:31 PM UTC-7, Po Choi wrote:
>>>
>>>
>>>
>>> foo(::Val{1}) = 1
>>> foo(::Val{2}) = 2
>>> foo(::Val{2}) = 3
>>>
>>> function bar()
>>>   
>>> for t in Datatype[Val{k} for k in 1:3]
>>>
>>>   end
>>> end
>>>
>>>
>>>
>>>
>>>
>>>
>>>
>>>
>>>
>>>
>>>
>
>
> -- 
> Erik Schnetter <schn...@gmail.com > 
> http://www.perimeterinstitute.ca/personal/eschnetter/
>


[julia-users] Re: Does it make sense to loop over types?

2016-06-10 Thread Po Choi
Ops! I accidentally hit the post button! So the post is not completed!

It is an example:
foo(::Val{1}) = 1
foo(::Val{2}) = 2
foo(::Val{2}) = 3

function bar()
  s = 0
  for t in Datatype[Val{k} for k in 1:3]
s += foo(t)
  end
end

Will there be any performance issue if I loop over types?
I am still trying to understand how the multiple-dispatch works. Sometimes 
I am confused!

On Friday, June 10, 2016 at 6:37:31 PM UTC-7, Po Choi wrote:
>
>
>
> foo(::Val{1}) = 1
> foo(::Val{2}) = 2
> foo(::Val{2}) = 3
>
> function bar()
>   
> for t in Datatype[Val{k} for k in 1:3]
>
>   end
> end
>
>
>
>
>
>
>
>
>
>
>

[julia-users] Does it make sense to loop over types?

2016-06-10 Thread Po Choi


foo(::Val{1}) = 1
foo(::Val{2}) = 2
foo(::Val{2}) = 3

function bar()
  
for t in Datatype[Val{k} for k in 1:3]
   
  end
end












[julia-users] Fun Fact: Julia is space sensitive (in some sense)

2016-05-24 Thread Po Choi


julia> a = zeros(3)
3-element Array{Float64,1}:
 0.0
 0.0
 0.0

julia> a[2+1]
0.0

julia> a[2 + 1]
0.0

julia> a[2+ 1]
0.0

julia> a[2 +1]
ERROR: MethodError: `typed_hcat` has no method matching 
typed_hcat(::Array{Float64,1}, ::Int64, ::Int64)
Closest candidates are:
  typed_hcat(::Type{T}, ::Number...)
  typed_hcat(::Type{T}, ::Any...)

julia> a[(2 +1)]
0.0





Re: [julia-users] In these two ways to "create functions", which one is a better way?

2016-04-20 Thread Po Choi
Thanks. I need to pass a set of orthogonal functions and coefficients into 
my routines. So I want to know the best practice in Julia to do this.

On Wednesday, April 20, 2016 at 12:51:47 AM UTC-7, David P. Sanders wrote:
>
> If this is your actual use case, then I suggest checking out the 
> Polynomials.jl package. 
>
> In particular, there is a more efficient algorithm (Horner's algorithm) 
> that you can use to actually evaluate such a function. 
>


Re: [julia-users] In these two ways to "create functions", which one is a better way?

2016-04-19 Thread Po Choi
I see. So the multiple dispatch is better than anonymous function in 0.4

In 0.5, if I do this
create_function{T}(coeff::Vector{T}) = x -> sum(coeff .* [x^k for k in 1:
length(coeff)]) 
would it have better performance?

On Tuesday, April 19, 2016 at 8:44:26 PM UTC-7, Yichao Yu wrote:
>
> On Tue, Apr 19, 2016 at 11:31 PM, Po Choi <vegeta...@gmail.com 
> > wrote: 
> > Given coefficients, say, coeff = [1,2,3] 
> > I want to get a function for the polynomial: f(x) = coeff[1] * x + 
> coeff[1] 
> > * x^2 + coeff[1] * x^3 
> > 
> > First way 
> > create_function(coeff) = x -> sum(coeff .* [x^k for k in 
> 1:length(coeff)]) 
> > 
> > Second way 
> > immutable Myfunction 
> > coeff::Vector 
>
> ^^ Vector is an abstract type. You probably want to parametrize this. 
>
> > end 
> > call(f::Myfunction, x) = sum(f.coeff .* [x^k for k in 
> 1:length(f.coeff)]) 
> > 
> > Then 
> > coeff = [1,2,3] 
> > foo = create_function(coeff) 
> > bar = Myfunction(coeff) 
>
> If you fix the type instability/uncertainty mentioned above. The 
> second one is better on 0.4. They are the same on 0.5. 
>
> > 
> > foo(1.2) 
> > bar(1.2) 
> > 
> > Question: Which way is better in Julia? 
>


[julia-users] In these two ways to "create functions", which one is a better way?

2016-04-19 Thread Po Choi
Given coefficients, say, coeff = [1,2,3]
I want to get a function for the polynomial: f(x) = coeff[1] * x + coeff[1] 
* x^2 + coeff[1] * x^3

First way
create_function(coeff) = x -> sum(coeff .* [x^k for k in 1:length(coeff)]) 

Second way
immutable Myfunction
coeff::Vector
end
call(f::Myfunction, x) = sum(f.coeff .* [x^k for k in 1:length(f.coeff)])

Then
coeff = [1,2,3]
foo = create_function(coeff)
bar = Myfunction(coeff)

foo(1.2)
bar(1.2)

Question: Which way is better in Julia?


Re: [julia-users] What is the correct way to use the type alias Vector and Matrix in multiple dispatch?

2016-04-08 Thread Po Choi
Thanks.

One point I don't understand is the type alias `Matrix{T}` with parametric 
type `T`.

julia> foo{T}(A::Vector{Matrix{T}}) = 1

foo (generic function with 1 method)


julia> methods(foo)

# 1 method for generic function "foo":

foo{T}(A::Array{Array{T,2},1}) at none:1


julia> bar(A::Vector{Matrix}) = 1

bar (generic function with 1 method)


julia> methods(bar)

# 1 method for generic function "bar":

bar(A::Array{Array{T,2},1}) at none:1


julia> baz(A::Vector{Matrix{T}}) = 1

ERROR: UndefVarError: T not defined


I can define `foo{T}(A::Array{Array{T,2},1})`.
I can define `bar(A::Array{Array{T,2},1})`  by using type alias `Matrix` 
without `{T}`
But, I cannot define `baz` as above without `{T}`.
Is it fair?


On Wednesday, April 6, 2016 at 6:24:34 PM UTC-7, Andy Ferris wrote:
>
> T is meant to be a parametric type, defined in this case in the definition 
> of Matrix (as a type alias) and also Array (as a type parameter with the 
> same name). In typeof(AAA) it's pulling T out of that definition of the 
> typealias. You could have written AAA = Matrix{Float64}[randn(3,3) for k in 
> 1:4] to define T.
>
> Further, types in Julia are not covariant which means even if A <: B, we 
> do NOT have Type{A} <: Type{B}. In your case that reads Matrix{Float64} <: 
> Matrix, but not Vector{Matrix{Float64}} <: Vector{Matrix}.
>
> To be generic, your function definitions could take a type parameter, like:
>
> hello{T}(A::Vector{Matrix{T}}) = 2
>
> Here we have introduced a new "parameteric type" variable "T". It could 
> have been named anything. It works since there is some T (==Float64) where 
> it will find a match. 
>
> Or, to be specific about the input type, just define:
>
> hello(A::Vector{Matrix{Float64}}) = 2
>
> You don't have to be afraid of using Matrix and Vector, but you do have to 
> think about how that might interact with the non-covariant type system. In 
> cases like these AFAIK the only way to make a generic function is to 
> introduce type parameters (using either Array{T,2} or Matrix{T} should be 
> fully equivalent).
>
> Does that help?
> Andy
>
>
> On Thursday, April 7, 2016 at 9:58:50 AM UTC+10, Po Choi wrote:
>>
>>
>> Does it make sense to declare a variable with the type `Matrix`?
>>
>> julia> methods(hello)
>> # 2 methods for generic function "hello":
>> hello(A::Array{T,2}) at none:1
>> hello(A::Array{Array{T,2},1}) at none:1
>>
>> julia> AA = [randn(3,3) for k in 1:4];
>>
>> julia> AAA = Matrix[randn(3,3) for k in 1:4];
>>
>> julia> hello(AA)
>> ERROR: MethodError: `hello` has no method matching 
>> hello(::Array{Array{Float64,2},1})
>>
>> julia> hello(AAA)
>> 2
>>
>>
>> julia> typeof(AA)
>> Array{Array{Float64,2},1}
>>
>> julia> typeof(AAA)
>> Array{Array{T,2},1}
>>
>> julia> Array{T,2}
>> ERROR: UndefVarError: T not defined
>>
>>
>> I am a little bit confused about the `T`. Why can `T` appear inside `AAA` 
>> without being declared?
>>
>>
>> On Wednesday, April 6, 2016 at 1:44:38 PM UTC-7, Yichao Yu wrote:
>>>
>>> On Wed, Apr 6, 2016 at 4:23 PM, Po Choi <vegeta...@gmail.com> wrote: 
>>> > 
>>> > hello(A::Matrix) = 1 
>>> > hello(A::Vector{Matrix}) = 2 
>>>
>>>
>>> http://julia.readthedocs.org/en/latest/manual/types/#parametric-composite-types
>>>  
>>>
>>> Vector{Matrix{Float64}} is not a subtype of Vector{Matrix} 
>>>
>>> > A = randn(3,3); 
>>> > AA = [randn(3,3) for k in 1:4]; 
>>> > hello(A) 
>>> > hello(AA) 
>>> > 
>>> > The output has method error. 
>>> > julia> hello(A) 
>>> > 1 
>>> > 
>>> > julia> hello(AA) 
>>> > ERROR: MethodError: `hello` has no method matching 
>>> > hello(::Array{Array{Float64,2},1}) 
>>> > 
>>> > 
>>> > If I write down the types explicitly, 
>>> > hi(A::Array{Float64,2}) = 1 
>>> > hi(A::Array{Array{Float64,2},1}) = 2 
>>> > A = randn(3,3); 
>>> > AA = [randn(3,3) for k in 1:4]; 
>>> > hi(A) 
>>> > hi(AA) 
>>> > The output is what I expect. 
>>> > julia> hi(A) 
>>> > 1 
>>> > 
>>> > julia> hi(AA) 
>>> > 2 
>>> > 
>>> > Am I using Vector and Matrix in a wrong way? 
>>>
>>

Re: [julia-users] What is the correct way to use the type alias Vector and Matrix in multiple dispatch?

2016-04-06 Thread Po Choi

Does it make sense to declare a variable with the type `Matrix`?

julia> methods(hello)
# 2 methods for generic function "hello":
hello(A::Array{T,2}) at none:1
hello(A::Array{Array{T,2},1}) at none:1

julia> AA = [randn(3,3) for k in 1:4];

julia> AAA = Matrix[randn(3,3) for k in 1:4];

julia> hello(AA)
ERROR: MethodError: `hello` has no method matching 
hello(::Array{Array{Float64,2},1})

julia> hello(AAA)
2


julia> typeof(AA)
Array{Array{Float64,2},1}

julia> typeof(AAA)
Array{Array{T,2},1}

julia> Array{T,2}
ERROR: UndefVarError: T not defined


I am a little bit confused about the `T`. Why can `T` appear inside `AAA` 
without being declared?


On Wednesday, April 6, 2016 at 1:44:38 PM UTC-7, Yichao Yu wrote:
>
> On Wed, Apr 6, 2016 at 4:23 PM, Po Choi <vegeta...@gmail.com > 
> wrote: 
> > 
> > hello(A::Matrix) = 1 
> > hello(A::Vector{Matrix}) = 2 
>
>
> http://julia.readthedocs.org/en/latest/manual/types/#parametric-composite-types
>  
>
> Vector{Matrix{Float64}} is not a subtype of Vector{Matrix} 
>
> > A = randn(3,3); 
> > AA = [randn(3,3) for k in 1:4]; 
> > hello(A) 
> > hello(AA) 
> > 
> > The output has method error. 
> > julia> hello(A) 
> > 1 
> > 
> > julia> hello(AA) 
> > ERROR: MethodError: `hello` has no method matching 
> > hello(::Array{Array{Float64,2},1}) 
> > 
> > 
> > If I write down the types explicitly, 
> > hi(A::Array{Float64,2}) = 1 
> > hi(A::Array{Array{Float64,2},1}) = 2 
> > A = randn(3,3); 
> > AA = [randn(3,3) for k in 1:4]; 
> > hi(A) 
> > hi(AA) 
> > The output is what I expect. 
> > julia> hi(A) 
> > 1 
> > 
> > julia> hi(AA) 
> > 2 
> > 
> > Am I using Vector and Matrix in a wrong way? 
>


[julia-users] What is the correct way to use the type alias Vector and Matrix in multiple dispatch?

2016-04-06 Thread Po Choi

hello(A::Matrix) = 1
hello(A::Vector{Matrix}) = 2
A = randn(3,3);
AA = [randn(3,3) for k in 1:4];
hello(A)
hello(AA)

The output has method error.
julia> hello(A)
1

julia> hello(AA)
ERROR: MethodError: `hello` has no method matching 
hello(::Array{Array{Float64,2},1})


If I write down the types explicitly,
hi(A::Array{Float64,2}) = 1
hi(A::Array{Array{Float64,2},1}) = 2
A = randn(3,3);
AA = [randn(3,3) for k in 1:4];
hi(A)
hi(AA)
The output is what I expect.
julia> hi(A)
1

julia> hi(AA)
2

Am I using Vector and Matrix in a wrong way?


[julia-users] Re: ANN: A potential new Discourse-based Julia forum

2016-02-23 Thread Po Choi
Does it support MathJax?

On Saturday, September 19, 2015 at 5:16:36 PM UTC-7, Jonathan Malmaud wrote:
>
> Hi all,
> There's been some chatter about maybe switching to a new, more modern 
> forum platform for Julia that could potentially subsume julia-users, 
> julia-dev, julia-stats, julia-gpu, and julia-jobs.   I created 
> http://julia.malmaud.com for us to try one out and see if we like it. 
> Please check it out and leave feedback. All the old posts from julia-users 
> have already been imported to it.
>
> It is using Discourse , the same forum 
> software used for the forums of Rust , 
> BoingBoing, and some other big sites. Benefits over Google Groups include 
> better support for topic tagging, community moderation features,  Markdown 
> (and hence syntax highlighting) in messages, inline previews of linked-to 
> Github issues, better mobile support, and more options for controlling when 
> and what you get emailed. The Discourse website 
>  does a better job of summarizing the 
> advantages than I could.
>
> To get things started, MIke Innes suggested having a topic on what we 
> plan on working on this coming wee 
> k.
>  
> I think that's a great idea.
>
> Just to be clear, this isn't "official" in any sense - it's just to 
> kickstart the discussion. 
>
> -Jon
>
>
>

Re: [julia-users] Re: Googling the functions I need in Julia is hard

2016-02-12 Thread Po Choi
Good point. I tried to use this search box in Juila manual, but get nothing.
In R and Matlab, the search would return the function sd/std.

I think, not only the "words" in Julia documentation is needed to improved, 
but also the search engine.

On Friday, February 12, 2016 at 12:52:19 AM UTC-8, Michele Zaffalon wrote:
>
> But the original point is still valid: using the search box in the 
> official documentation page http://docs.julialang.org/en/release-0.4, 
> searching for "standard deviation" does not bring up any useful hit, 
> despite the fact that Base.std is fairly well documented and contains the 
> words standard deviation.
> Is there a reason why it should work at the REPL but not in the webpage?
>
>
> On Fri, Feb 12, 2016 at 9:25 AM, Mauro  
> wrote:
>
>> Also at the Julia REPL:
>>
>> julia> apropos("standard deviation")
>> randn!
>> stdm
>> std
>> randn
>>
>> help?> std
>> search: std stdm STDIN STDOUT STDERR setdiff setdiff! hist2d hist2d! 
>> stride strides StridedArray StridedVector StridedMatrix StridedVecOrMat 
>> redirect_stdin
>>
>>   std(v[, region])
>>
>>   Compute the sample standard deviation of a vector or array v, 
>> optionally along dimensions in region. The algorithm returns an estimator 
>> of the generative
>>   distribution's standard deviation under the assumption that each 
>> entry of v is an IID drawn from that generative distribution. This 
>> computation is equivalent to
>>   calculating sqrt(sum((v - mean(v)).^2) / (length(v) - 1)). Note: 
>> Julia does not ignore NaN values in the computation. For applications 
>> requiring the handling of
>>   missing data, the DataArray package is recommended.
>>
>> Having said this, documentation always needs improvements and is
>> certainly not on Matlab's level of completeness.  Please contribute
>> where you find it lacking.  See
>>
>> https://github.com/JuliaLang/julia/blob/master/CONTRIBUTING.md#improving-documentation
>>
>>
>> On Fri, 2016-02-12 at 09:18, NotSoRecentConvert > > wrote:
>> > You can even download the entire thing as a PDF, HTML, or EPUB if you 
>> want
>> > to highlight, annotate, or bookmark your most searched functions. Look 
>> in
>> > the lower right of the page for "v: latest" and click it for more 
>> options.
>> >
>> > On Friday, February 12, 2016 at 8:03:27 AM UTC+1, Lutfullah Tomak wrote:
>> >>
>> >> There is this one
>> >>
>> >> http://docs.julialang.org/en/release-0.4/stdlib/math/#Base.std
>> >>
>> >> Instead of google, I use this manual for search.
>> >>
>> >>
>>
>
>

[julia-users] How to do multiple dispatch on different dimension of matrices?

2016-02-12 Thread Po Choi
Can I do something like this?
testMatrix(x::Array{1}) = println("It is 1-dimensional.")
testMatrix(x::Array{2}) = println("It is 2-dimensional.")

The above does not work because Array{1} == Array{1, N}

One solution is
testMatrixAgain{T}(x::Array{T, 1}) = println("It is 1-dimensional.")
testMatrixAgain{T}(x::Array{T, 2}) = println("It is 2-dimensional.")

However, the parametric type is actually not used. Is there any way that I 
don't need the parametric type?


[julia-users] Googling the functions I need in Julia is hard

2016-02-11 Thread Po Choi
One thing I hate Julia is that it is very hard to google for the functions 
I need. I feel like Julia has no documentation.

If I google "R standard deviation", the first link is
https://stat.ethz.ch/R-manual/R-devel/library/stats/html/sd.html
which tells all the things about the standard deviation function in R

If I google "Matlab standard deviation", the first link is
http://www.mathworks.com/help/matlab/ref/std.html
which tells all the things about the standard deviation function in Matlab.

If I google "julia standard deviation", there are just some random pages 
which are very confusing.

Any tips for how to google for the functions I need in Julia?
Could Julia have a nice user manual like R or Matlab?