[julia-users] Re: Replace value in a matriz

2015-10-06 Thread Mauricio Esteban Cuak
Ups, it's nicer to use indmax and indmin, sorry

function change_min_max!{T}(m::Array{T,2})
for row = 1:size(m, 1)
m[row, indmax(m[row, :])] = one(T)
m[row, indmin(m[row, :])] = zero(T)
end
end


El martes, 6 de octubre de 2015, 21:29:02 (UTC-5), Mauricio Esteban Cuak 
escribió:
>
> How about this:
>
> function change_min_max!{T}(m::Array{T,2})
> for row = 1:size(m, 1)
> m[row, findmax(m[row, :])[2]] = one(T)
> m[row, findmin(m[row, :])[2]] = zero(T)
> end
> end
>
>
>
> Two caveats. First, I'm guessing this won't be that fast, because it's 
> traversing through rows instead of columns, but maybe you're not worried 
> about speed.
> Second, this doesn't handle ties (you didn't say how you wanted to handle 
> them)
>
>
> El martes, 6 de octubre de 2015, 15:32:15 (UTC-5), Steven G. Johnson 
> escribió:
>>
>> On Tuesday, October 6, 2015 at 4:27:40 PM UTC-4, Rosangela Oliveira wrote:
>>>
>>> The idea is to read each matrix nxm and  to replace the max value=1 and 
>>>  min value=0 in case when max and min its the same value, the first receive 
>>> 1 and the second recive 0.
>>>
>>  
>> Just write loops.  It will be way less convoluted, and way faster, than 
>> any construction using find etc.
>>
>> With Matlab and Python, every programming problem is an exercise in 
>> mining the standard library for (you hope) just the right functions for 
>> your problem, because any code that you write yourself will be slow.  Julia 
>> doesn't have that problem, and often the most straightforward code is also 
>> the best code.
>>
>

[julia-users] Re: Replace value in a matriz

2015-10-06 Thread Mauricio Esteban Cuak
How about this:

function change_min_max!{T}(m::Array{T,2})
for row = 1:size(m, 1)
m[row, findmax(m[row, :])[2]] = one(T)
m[row, findmin(m[row, :])[2]] = zero(T)
end
end



Two caveats. First, I'm guessing this won't be that fast, because it's 
traversing through rows instead of columns, but maybe you're not worried 
about speed.
Second, this doesn't handle ties (you didn't say how you wanted to handle 
them)


El martes, 6 de octubre de 2015, 15:32:15 (UTC-5), Steven G. Johnson 
escribió:
>
> On Tuesday, October 6, 2015 at 4:27:40 PM UTC-4, Rosangela Oliveira wrote:
>>
>> The idea is to read each matrix nxm and  to replace the max value=1 and 
>>  min value=0 in case when max and min its the same value, the first receive 
>> 1 and the second recive 0.
>>
>  
> Just write loops.  It will be way less convoluted, and way faster, than 
> any construction using find etc.
>
> With Matlab and Python, every programming problem is an exercise in mining 
> the standard library for (you hope) just the right functions for your 
> problem, because any code that you write yourself will be slow.  Julia 
> doesn't have that problem, and often the most straightforward code is also 
> the best code.
>


[julia-users] Re: Replace value in a matriz

2015-10-06 Thread Mauricio Esteban Cuak
Hi,

You didn't tell us what to do with the other numbers, so I'm assuming you 
can also have two columns per row?

for row=1:size(ma,1)
ma[row, 1] = (ma[row, 1] >= ma[row, 2])
ma[row, 2] = (ma[row, 1] <  ma[row, 2])
end


gives

4x2 Array{Float64,2}:
 1.0  0.0
 0.0  1.0
 0.0  1.0
 1.0  0.0


El martes, 6 de octubre de 2015, 12:43:58 (UTC-5), Rosangela Oliveira 
escribió:
>
> Hi,
> How to replace the values in a matriz using maxval and minval?
> for example, I would change the max value in a line for o(zero) and the 
> max value for one.
>
>
> I'm trying something like this...
>
> ma=[0.4 0.3; 0.6 0.9;-0.6 15;0.5 0.5]for i1 = 1:size(ma,1)
> max=findmax(ma[i1,:])
> min=findmin(ma[i1,:])
> end
>
>
> Thanks for helping.
>
>

[julia-users] Re: Spped, assigning arrays

2015-08-24 Thread Mauricio Esteban Cuak
Correction (and sorry for the noise), function get_gauss_points3! 

function get_gauss_points3!(gp_xi::Matrix)
   gp_xi[:] = [-1/sqrt(3)  1/sqrt(3)  1/sqrt(3) -1/sqrt(3);
-1/sqrt(3) -1/sqrt(3)  1/sqrt(3)  1/sqrt(3)]
end

DOES assign in-place. However, it's 50 times slower that assigning element 
by element (which I find surprising for a 2 by 4 matrix). 

get_gauss_points!:
elapsed time: 3.176810571 seconds (272096 bytes allocated, 43.29% gc 
time)


get_gauss_points2!
elapsed time: 0.062963865 seconds (96 bytes allocated)


get_gauss_points3!
elapsed time: 3.449866225 seconds (272096 bytes allocated, 40.87% gc 
time)


I'm using 0.3.11


El lunes, 24 de agosto de 2015, 14:26:42 (UTC-5), Mohamed Moussa escribió:
>
> Err, oops. Good point. 
>
> On Monday, August 24, 2015 at 7:47:35 PM UTC+2, Scott Jones wrote:
>>
>> Have you checked the results from `get_gauss_points!` ?  I'm not even 
>> sure that is doing what you think.
>> Instead of setting the values in the mutable array, it is setting the 
>> local variable (and creating a new matrix) every time.
>>
>>
>> On Monday, August 24, 2015 at 7:31:56 PM UTC+2, Mohamed Moussa wrote:
>>>
>>> Hey. I'm new to Julia. I'm playing around with 0.4 on Windows. I'm 
>>> interested in writing finite element code for research purposes. 
>>>
>>> Consider the following code
>>>
>>> function get_gauss_points!(gp_xi::Matrix)
>>> gp_xi = [-1/sqrt(3)  1/sqrt(3)  1/sqrt(3) -1/sqrt(3);
>>>  -1/sqrt(3) -1/sqrt(3)  1/sqrt(3)  1/sqrt(3)]
>>> end
>>>
>>> function get_gauss_points2!(gp_xi::Matrix)
>>> gp_xi[1,1] = -1/sqrt(3)
>>> gp_xi[2,1] = -1/sqrt(3)
>>> gp_xi[1,2] =  1/sqrt(3)
>>> gp_xi[2,2] = -1/sqrt(3)
>>> gp_xi[1,3] =  1/sqrt(3)
>>> gp_xi[2,3] =  1/sqrt(3)
>>> gp_xi[1,4] = -1/sqrt(3)
>>> gp_xi[2,4] =  1/sqrt(3)
>>> end
>>>
>>> function test()
>>> gp_xi = zeros(2,4)
>>> get_gauss_points!(gp_xi)
>>> get_gauss_points2!(gp_xi)
>>>
>>> println("get_gauss_points!:")
>>> @time for i=1:1e7 get_gauss_points!(gp_xi) end
>>>
>>> println("\nget_gauss_points2!")
>>> @time for i=1:1e7 get_gauss_points2!(gp_xi) end
>>> end
>>>
>>> test()
>>>
>>> The output is
>>> get_gauss_points!:
>>>   1.129231 seconds (100.00 M allocations: 2.682 GB, 14.56% gc time)
>>>
>>> get_gauss_points2!
>>>   0.067619 seconds
>>>
>>> Using @profile shows that get_gauss_points! spends a lot of time in 
>>> abstractarray. Is it possible to make get_gauss_points run as fast as 
>>> get_gauss_points2 ? 
>>>
>>> Cheers. 
>>>
>>

[julia-users] Re: Spped, assigning arrays

2015-08-24 Thread Mauricio Esteban Cuak
I thought this would work to assign in-place (but it does not)

function get_gauss_points3!(gp_xi::Matrix)
gp_xi[:] = [-1/sqrt(3)  1/sqrt(3)  1/sqrt(3) -1/sqrt(3);
 -1/sqrt(3) -1/sqrt(3)  1/sqrt(3)  1/sqrt(3)]
end

Is there any reason why this syntax shouldn't assign in-place?

El lunes, 24 de agosto de 2015, 12:31:56 (UTC-5), Mohamed Moussa escribió:
>
> Hey. I'm new to Julia. I'm playing around with 0.4 on Windows. I'm 
> interested in writing finite element code for research purposes. 
>
> Consider the following code
>
> function get_gauss_points!(gp_xi::Matrix)
> gp_xi = [-1/sqrt(3)  1/sqrt(3)  1/sqrt(3) -1/sqrt(3);
>  -1/sqrt(3) -1/sqrt(3)  1/sqrt(3)  1/sqrt(3)]
> end
>
> function get_gauss_points2!(gp_xi::Matrix)
> gp_xi[1,1] = -1/sqrt(3)
> gp_xi[2,1] = -1/sqrt(3)
> gp_xi[1,2] =  1/sqrt(3)
> gp_xi[2,2] = -1/sqrt(3)
> gp_xi[1,3] =  1/sqrt(3)
> gp_xi[2,3] =  1/sqrt(3)
> gp_xi[1,4] = -1/sqrt(3)
> gp_xi[2,4] =  1/sqrt(3)
> end
>
> function test()
> gp_xi = zeros(2,4)
> get_gauss_points!(gp_xi)
> get_gauss_points2!(gp_xi)
>
> println("get_gauss_points!:")
> @time for i=1:1e7 get_gauss_points!(gp_xi) end
>
> println("\nget_gauss_points2!")
> @time for i=1:1e7 get_gauss_points2!(gp_xi) end
> end
>
> test()
>
> The output is
> get_gauss_points!:
>   1.129231 seconds (100.00 M allocations: 2.682 GB, 14.56% gc time)
>
> get_gauss_points2!
>   0.067619 seconds
>
> Using @profile shows that get_gauss_points! spends a lot of time in 
> abstractarray. Is it possible to make get_gauss_points run as fast as 
> get_gauss_points2 ? 
>
> Cheers. 
>


Re: [julia-users] Wrong number of arguments?

2015-07-15 Thread Mauricio Esteban Cuak
Just in case it seems a bit more transparent, this also works:

r = rand(5)
map( x -> x[1] + x[2], enumerate(r))



El martes, 14 de julio de 2015, 20:20:14 (UTC-5), Yichao Yu escribió:
>
> On Tue, Jul 14, 2015 at 7:09 PM, Ritchie Lee  > wrote: 
> > Sorry if this is a newbie question.  I am trying to get map() to work 
> with 
> > with enumerate() but I am getting an error. 
> > 
> > r=rand(5) 
> > map((i,v)->i+v, enumerate(r)) 
> > ERROR: wrong number of arguments 
> >  in anonymous at none:1 
> >  in map at abstractarray.jl:1207 
> > 
> > collect(enumerate(r)) generates an array of 2-tuples as expected. 
>
> `map` does not splat each element of the iteration and only pass each 
> element as a single argument to the function 
>
> ```julia 
> julia> r = rand(5) 
>   map(x->((i, v) = x; i + v), enumerate(r)) 
> 5-element Array{Any,1}: 
> 1.6277 
> 2.41751 
> 3.34848 
> 4.70194 
> 5.89139 
>
> ``` 
>
> > 
> > Is the syntax incorrect? 
> > 
> > Thanks, 
> > 
> > Ritchie 
>


Re: [julia-users] Re: eig()/eigfact() performance: Julia vs. MATLAB

2015-07-12 Thread Mauricio Esteban Cuak
I tried in in Matlab R2014a and Julia 0.3.10 on an 2.5 GHZ i5 and the 
difference was much smaller:

22 seconds for Julia, 19 for Matlab

Also, I tried it in local and global scope and the difference wasn't more 
than one or two seconds




El domingo, 12 de julio de 2015, 13:57:40 (UTC-5), Milan Bouchet-Valat 
escribió:
>
> Le dimanche 12 juillet 2015 à 11:38 -0700, John Myles White a écrit : 
> > http://julia.readthedocs.org/en/release-0.3/manual/performance-tips/ 
> I don't think running the code in the global scope is the problem here: 
> most of the computing time is probably in BLAS anyway. I think MATLAB 
> uses Intel MKL while Julia uses OpenBLAS, and maybe on that particular 
> problem and with your particular machine the former is significantly 
> faster. 
>
> If you really need this 2.3 factor you could try building Julia with 
> MKL. See https://github.com/JuliaLang/julia/#intel-compilers-and-math 
> -kernel-library-mkl 
> 
>  
>
>
> Regards 
>
>
> > On Sunday, July 12, 2015 at 8:33:56 PM UTC+2, Evgeni Bezus wrote: 
> > > Hi all, 
> > > 
> > > I am a Julia novice and I am considering it as a potential 
> > > alternative to MATLAB. 
> > > My field is computational nanophotonics and the main numerical 
> > > technique that I use involves multiple solution of the 
> > > eigenvalue/eigenvector problem for dense matrices with size of 
> > > about 1000*1000 (more or less). 
> > > I tried to run the following nearly equivalent code in Julia and in 
> > > MATLAB: 
> > > 
> > > Julia code: 
> > > 
> > > n = 1000 
> > > M = rand(n, n) 
> > > F = eigfact(M) 
> > > tic() 
> > > for i = 1:10 
> > > F = eigfact(M) 
> > > end 
> > > toc() 
> > > 
> > > 
> > > MATLAB code: 
> > > 
> > > n = 1000; 
> > > M = rand(n, n); 
> > > [D, V] = eig(M); 
> > > tic; 
> > > for i = 1:10 
> > > [D, V] = eig(M); 
> > > end 
> > > toc 
> > > 
> > > It turns out that MATLAB's eig() runs nearly 2.3 times faster than 
> > > eig() or eigfact() in Julia. On the machine available to me right 
> > > now (relatively old Core i5 laptop) the average time for MATLAB is 
> > > of about 37 seconds, while the mean Julia time is of about 85 
> > > seconds. I use MATLAB R2010b and Julia 0.3.7 (i tried to run the 
> > > code both in Juno and in a REPL session and obtained nearly 
> > > identical results). 
> > > 
> > > Is there anything that I'm doing wrong? 
> > > 
> > > Best regards, 
> > > Evgeni 
> > > 
>