Hi,

why isn’t it working? g(x) works, g2(x) fails aswell.

Best,

Hannes!

Welcome, @Romero_Azzalini! The link you posted is broken. In general, the best way to get help is to paste complete and stand-alone code directly into the post. Surround the code with triple backticks in order for it to be formatted properly.

```
```
code here
```
```

1 Like

sorry, now you can see it

You’re not doing the exponentiation elementwise (`^`

vs `.^`

) in `f`

and `g2`

.

```
julia> x = linspace(0,10,2)
0.0:10.0:10.0
julia> x^4
ERROR: DimensionMismatch("Cannot multiply two vectors")
Stacktrace:
[1] power_by_squaring(::StepRangeLen{Float64,Base.TwicePrecision{Float64},Base.TwicePrecision{Float64}}, ::Int64) at ./intfuncs.jl:175
[2] literal_pow(::Base.#^, ::StepRangeLen{Float64,Base.TwicePrecision{Float64},Base.TwicePrecision{Float64}}, ::Type{Val{4}}) at ./intfuncs.jl:205
julia> x.^4
2-element Array{Float64,1}:
0.0
10000.0
```

1 Like

thx, it works now! is it a type problem?

Kind-of, but has more to do with linear algebra than types. Multiplication simply isn’t defined between two vectors. Exponentiation by integers is just repeated multiplications. But you don’t want to exponentiate the *whole vector*, you just want to exponentiate each of its elements. That’s the difference between `x^4`

and `x.^4`

.

If you are coming from Python/NumPy, you probably are used to exponentiation (`**`

) and multiplication (`*`

) always being elementwise; to get the linear-algebra meaning of these operations you need to do `numpy.matrix_power`

and `numpy.dot`

.

In Julia, exponentiation (`^`

) and multiplication (`*`

) are the linear-algebra meaning, and you use `.^`

and `.*`

for the elementwise/broadcasting meaning.

However, this is more than just a spelling difference. The dot `.`

has a deeper meaning in Julia: it allows you to turn *any* function or operator in Julia into an elementwise operation. Moreover, because you indicate that it is elementwise at a syntactic level, the language can fuse multiple elementwise operations into a single loop.

5 Likes

We could easily change the error to:

```
ERROR: DimensionMismatch("Cannot multiply two vectors. Maybe you had element-wise (done with a dot) in mind?")
```

Right. Now that we have the new Row and Column vectors, and such multiplied an the exception, then possibly that can help in cases to make more helpful error messages?

```
julia> [1 2]^2 # here helpful "square matrix wanted", could be done for the power operator on Row or Column vectors
ERROR: DimensionMismatch("matrix A has dimensions (1,2), matrix B has dimensions (1,2)")
julia> [1 2]*[1 2]' # while this works
1×1 Array{Int64,2}:
5
```