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

Generic vecdot requires dot be implemented for scalars #14781

Closed
IainNZ opened this issue Jan 24, 2016 · 6 comments
Closed

Generic vecdot requires dot be implemented for scalars #14781

IainNZ opened this issue Jan 24, 2016 · 6 comments
Labels
linear algebra Linear algebra

Comments

@IainNZ
Copy link
Member

IainNZ commented Jan 24, 2016

The definition of vecdot (and its documentation) requires that user types define dot for scalars, instead of * like I would have guessed:

s = zero(dot(x[1], y[1]))
if size(x) == size(y)
for I in eachindex(x, y)
@inbounds s += dot(x[I], y[I])
end
else
for (Ix, Iy) in zip(eachindex(x), eachindex(y))
@inbounds s += dot(x[Ix], y[Iy])
end
end

I'm not sure what case this is expected to handle, but it increases the burden on people defining custom types, as in addition to multiplication (e.g. *(MyType,Number) and *(Number,MyType)) users must also define an analogous dot just for this case (and in the case of JuMP, where we have multiple types, all the other interaction terms).

I'm wondering if its too general. If the current functionality is desirable, then perhaps a generic dot(x::Any,y::Any) = x*y could be defined as a fallback?

@IainNZ IainNZ changed the title Generic vecdot requires dot be implemented Generic vecdot requires dot be implemented for scalars Jan 24, 2016
@andreasnoack
Copy link
Member

Is YourType <: Number. It's difficult to think of general behavior for Any so would it work if we had dot(x::Number,y::Numner)=x'y? It would then be similar to this definition

@inline vecnorm(x::Number, p::Real=2) = p == 0 ? real(x==0 ? zero(x) : one(x)) : abs(x)
for norm.

@kshyatt kshyatt added the linear algebra Linear algebra label Jan 24, 2016
@tkelman
Copy link
Contributor

tkelman commented Jan 24, 2016

JuMP types aren't subtypes of number AIUI, they're numberlike containers but inheriting from Number would result in too many behaviors that aren't all wanted. There have been other issues that mentioned having an AbstractScalar above Number in the hierarchy.

@IainNZ
Copy link
Member Author

IainNZ commented Jan 24, 2016

Yes not a number.
I mean, I'm a little confused about what vecdot is supposed to be doing. I thought it was Euclidean dot product, which would be a sum of multiplied scalars, but it seems that its coded for something much more general. Is there an example use-case for this more general "recursive" definition?

@andreasnoack
Copy link
Member

See https://groups.google.com/forum/#!topic/julia-users/cPZnKPK4Hbw, #6057, and #11067 for the origins of this behavior. @stevengj has determined how these functions work so we should hear him about ideas here.

@stevengj
Copy link
Member

dot(x::Any,y::Any) = x*y is incorrect as a fallback for non-real types. vecdot really needs an inner product, which is in general distinct from multiplication.

@IainNZ
Copy link
Member Author

IainNZ commented Jun 2, 2018

Tidying up old issues/PRs.

@IainNZ IainNZ closed this as completed Jun 2, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
linear algebra Linear algebra
Projects
None yet
Development

No branches or pull requests

5 participants