for i=1:numel(X)X[i] += Y[i]end
1. If indexing is involved, would we do this transformation:
y[I] .= -x
to
-!(sub(y,I), x)
?
2. I assume .= only works with certain operators on the right, so "y
.= x" can't be used to copy?
3. It's too bad these are redundant with a[:,:] = b+c except with
respect to performance. That almost makes me think it's not a good
thing to be doing. Maybe this can be a feature of indexed assignment
somehow?
This is pretty good. We probably need something pretty close to this.
1. If indexing is involved, would we do this transformation:
y[I] .= -x
to
-!(sub(y,I), x)
?
2. I assume .= only works with certain operators on the right, so "y
.= x" can't be used to copy?
3. It's too bad these are redundant with a[:,:] = b+c except with
respect to performance. That almost makes me think it's not a good
thing to be doing. Maybe this can be a feature of indexed assignment
somehow?
X .+= 1Y .+= XY .= X1 * X2
macro vectorize_unary(f)f! = symbol("$(f)!")quotefunction ($f!)(Y::Array, X::Array)for i = 1:numel(X)X[i] = ($f)(Y[i])endreturn Yend($f!)(X::Array) = ($f!)(X,X)($f)(X::Array) = ($f!)(similar(X,map_type(f,X)),X)endend
a = exp(a)
into in-place assignment as opposed to making an intermediate array?
a .= exp(a)
The thing I don't like about the . syntax is that we now have two ways of doing in-place operations - namely by suffixing ! after functions or by prefixing . in front of operators. I would have liked it if there were one way.
Also, for things like a = exp(a), it would be nice if the compiler can just do it automatically, since I am already telling the compiler that by using the same name in the input and output.
Perhaps even fancier usage could be where the compiler can do analysis on all the temporary creation and decide in which cases it needs to allocate new stuff and which stuff it can overwrite the input. Thus, in some cases, the compiler would be able to call foo! automatically instead of foo. It may also mean that the compiler needs to differentiate between mutable and immutable types.
-viral
The thing I don't like about the . syntax is that we now have two ways of doing in-place operations - namely by suffixing ! after functions or by prefixing . in front of operators. I would have liked it if there were one way.
Also, for things like a = exp(a), it would be nice if the compiler can just do it automatically, since I am already telling the compiler that by using the same name in the input and output.
Perhaps even fancier usage could be where the compiler can do analysis on all the temporary creation and decide in which cases it needs to allocate new stuff and which stuff it can overwrite the input. Thus, in some cases, the compiler would be able to call foo! automatically instead of foo. It may also mean that the compiler needs to differentiate between mutable and immutable types.
I love the idea of in-place operations. My objection is just stylistic, and about the choice of the ".=" operator. I agree that =! looks like a weird not-equals symbol. I don't yet have a better solution, but I feel that the whole syntax could be a little more unified - the current approach with ! and .= feels like a bit of a hack.
Do we need to think through something at a more fundamental level?
Also, what does "a .*= b" mean? Is it "a = a .* b" or in-place "*!(a, a, b)"? Should we then have "a ..*= b" for in-place ".*".
-viral
I love the idea of in-place operations. My objection is just stylistic, and about the choice of the ".=" operator. I agree that =! looks like a weird not-equals symbol. I don't yet have a better solution, but I feel that the whole syntax could be a little more unified - the current approach with ! and .= feels like a bit of a hack.
Do we need to think through something at a more fundamental level?
Also, what does "a .*= b" mean? Is it "a = a .* b" or in-place "*!(a, a, b)"? Should we then have "a ..*= b" for in-place ".*".
Y .= X1 + X2 - X3
+!(Y, X2, X2-X3)
for i = 1:numel(Y)Y[i] = X1[i] + X2[i] - X3[i]end
w .= f(v)
Y = 0.5*X[I] + 2*X + 0.5*X[J]
You are essentially using the .= syntax here as a compiler hint for devectorization. I think the right thing is for the compiler to detect these cases and devectorize. I believe Fortran has had this for the longest time. The cases where I and J are ranges can be optimized quite aggressively.
One usually has to decide which cases should be devectorized, since sometimes the vectorized versions can be faster, especially if you can use the vector registers. We don't yet have vectorization, and we I'm not sure we can use all the SSE and AVX stuff yet anyways.
-viral
Maybe we could somehow consider .= to be syntax for a loop, basically asking to have a for loop around the statement. This could be helpful in cases where vectorization fails to generalize, like a[x]=b where a is a hash table and x is a vector. a[x].=b would insert many keys, while the first one would use the whole vector as one key.
Maybe we could somehow consider .= to be syntax for a loop, basically asking to have a for loop around the statement. This could be helpful in cases where vectorization fails to generalize, like a[x]=b where a is a hash table and x is a vector. a[x].=b would insert many keys, while the first one would use the whole vector as one key.