Will `ArrayView` overcome the unnecessary copying?
On Saturday, August 22, 2015 at 11:54:39 AM UTC+2, Tony Kelman wrote: > > Currently Julia only drops trailing singleton dimensions in indexing. So > if F is n-by-m-by-k, then F[:, i, :] is n-by-1-by-k. You need to use > reshape or squeeze to convert that to an n-by-k 2-dimensional matrix. > > Note that writing code in this direct-port slicing style is going to > involve a lot of unnecessary copying and not perform very well, especially > not if the arrays are large. Just something to be aware of. > http://www.johnmyleswhite.com/notebook/2013/12/22/the-relationship-between-vectorized-and-devectorized-code/ > > is relevant here. > > > On Friday, August 21, 2015 at 7:27:49 PM UTC-7, [email protected] wrote: >> >> R code below changed to Julia code. It is always error. I do not know how >> to change R code below to Julia code correctly? Thank you. >> #R code >> forward <- function(G.I,Tr,Pr) >> { >> n.samp <- dim(G.I)[1] >> n.mark <- dim(G.I)[2] >> F <- G.I >> F[,1,] <- sweep(G.I[,1,],2,Pr,"*") >> for (i in 2:n.mark) >> { >> F[,i,] <- G.I[,i,]*(F[,i-1,]%*%Tr) >> S <- F[,i,1] + F[,i,2] + F[,i,3] >> F[,i,] <- sweep(F[,i,],1,S,"/") >> } >> return(F) >> } >> #error julia code >> function forward(GI::Array,Tr::Array,Pr::Array) >> nsamp = size(GI,1) >> nmark = size(GI,2) >> F = GI >> F[:,1,:] = broadcast(*,Pr,GI[:,1,:]) #error >> for i=2:nmark >> F[:,i,:] = GI[:,i,:].*(F[:,i-1,:]*Tr) #error >> S = F[:,i,1] + F[:,i,2] + F[:,i,3] #error >> F[:,i,:] = broadcast(/,S',F[:,i,:]) #error >> end >> return F >> end >> >
