|
1 | 1 | import LinearAlgebra.dot
|
2 |
| -export vecdot, dot |
| 2 | +export dot |
3 | 3 |
|
| 4 | +ismatrix(x::AbstractExpr) = (s = size(x); length(s) == 2 && s[1] > 1 && s[2] > 1) |
| 5 | +ismatrix(::AbstractMatrix) = true |
| 6 | +ismatrix(::Any) = false |
4 | 7 |
|
5 |
| -vecdot(x::AbstractExpr, y::AbstractExpr) = sum(broadcast(*, x, y)) |
6 |
| -vecdot(x::Value, y::AbstractExpr) = sum(broadcast(*, Constant(x), y)) |
7 |
| -vecdot(x::AbstractExpr, y::Value) = sum(broadcast(*, x, Constant(y))) |
| 8 | +# NOTE: Using asvec avoids broadcast-specific behaviors that we want to avoid, such |
| 9 | +# as extending singleton dimensions. We need to ensure that the inputs have the same |
| 10 | +# length, which broadcast will check for us if both inputs are vectors. |
| 11 | +asvec(x) = convert(AbstractExpr, ismatrix(x) ? vec(x) : x) |
| 12 | +_vecdot(x, y) = sum(broadcast(*, asvec(x), asvec(y))) |
8 | 13 |
|
9 |
| -dot(x::AbstractExpr, y::AbstractExpr) = (ismatrix(x) || ismatrix(y)) ? error("dot not implemented for matrices. perhaps you're looking for vecdot?") : vecdot(x, y) |
10 |
| -dot(x::Value, y::AbstractExpr) = (ismatrix(x) || ismatrix(y)) ? error("dot not implemented for matrices. perhaps you're looking for vecdot?") : vecdot(x, y) |
11 |
| -dot(x::AbstractExpr, y::Value) = (ismatrix(x) || ismatrix(y)) ? error("dot not implemented for matrices. perhaps you're looking for vecdot?") : vecdot(x, y) |
| 14 | +dot(x::AbstractExpr, y::AbstractExpr) = _vecdot(x, y) |
| 15 | +dot(x::Value, y::AbstractExpr) = _vecdot(x, y) |
| 16 | +dot(x::AbstractExpr, y::Value) = _vecdot(x, y) |
12 | 17 |
|
13 |
| -# tests if an array is a matrix (2D array) with both dimensions of size > 1 |
14 |
| -function ismatrix(x) |
15 |
| - sz = size(x) |
16 |
| - if length(sz) != 2 |
17 |
| - return false |
18 |
| - else |
19 |
| - for s in sz |
20 |
| - if s == 1 |
21 |
| - return false |
22 |
| - end |
23 |
| - end |
24 |
| - end |
25 |
| - return true |
| 18 | +if isdefined(LinearAlgebra, :vecdot) # defined but deprecated |
| 19 | + import LinearAlgebra: vecdot |
26 | 20 | end
|
| 21 | +Base.@deprecate vecdot(x::AbstractExpr, y::AbstractExpr) dot(x, y) |
| 22 | +Base.@deprecate vecdot(x::Value, y::AbstractExpr) dot(x, y) |
| 23 | +Base.@deprecate vecdot(x::AbstractExpr, y::Value) dot(x, y) |
0 commit comments