Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

unexpected failure to copy #3037

Closed
bsxfan opened this issue May 6, 2013 · 6 comments
Closed

unexpected failure to copy #3037

bsxfan opened this issue May 6, 2013 · 6 comments

Comments

@bsxfan
Copy link

bsxfan commented May 6, 2013

The unary - operator for numeric arrays creates as new array, but the unary + does not:

julia> X=randn(2,2); 
julia> is(X,+X)
true
julia> is(X,-X)
false

This may not be so bad, since the unary + is probably quite rare.

But the following unexpected failure to copy WILL cause a hard-to-find bug in someone's code at some stage:

julia>  [is(X,X^k) for k=0:2]
[false,true,false]

Counterexample:

julia>  [is(X,X.^k) for k=0:2]
[false,false,false]
@mlubin
Copy link
Member

mlubin commented May 6, 2013

Doesn't this restrict future enhancements to eliminate temporary objects? I don't think we should force all operators to make copies.

@bsxfan
Copy link
Author

bsxfan commented May 7, 2013

I think it was Stefan Karpinsky who commented in the discussion of some other issue (I can't find it now because the search on Github issues has disappeared today), that the type of return arguments shoud not depend on the value of inout arguments. Here it is not the type that changes but whether ^(X,k) is an inplace operation or not depends on the value of k. Elsewhere in Julia great care is taken to flag all inplace functions with !. Here it happens unexpectedly.

If performance is the issue, then I would say anyone that cares about efficiency when raising big matrices to integer powers should be paying careful attention to what they are doing anyway and could easily just avoid calling ^(X,1) unnecessarily.

On the other hand, if size and speed are not an issue then ^ should just work without surprizes.

@JeffBezanson
Copy link
Member

! marks mutating operations. f(x) = x is not mutating.

@StefanKarpinski
Copy link
Member

I agree with @bsxfan that this is just asking for trouble.

@JeffBezanson
Copy link
Member

I'm fine with adding a copy in those cases.

@lindahua
Copy link
Contributor

lindahua commented May 8, 2013

The current behavior is

julia> is(a, a*1)
false

julia> is(a, a/1)
false

julia> is(a, a^1)
true

For consistency, a^1 should not be so special, and it should be implemented in a way that is(a, a^1) returns false.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants