So when x = 0.2, B * A gives an approximation of exp(0.2). But when x is a square matrix such as [1 4; 1 1], B * A and B .* A result in errors, not an approximation of exp([1 4; 1 1]).
julia> x = [1 4; 1 1]
2×2 Matrix{Int64}:
1 4
1 1
julia> exp(x)
2×2 Matrix{Float64}:
10.2267 19.7177
4.92941 10.2267
julia> A = [x^0; x; x^2; x^3; x^4; x^5; x^6]
14×2 Matrix{Int64}:
1 0
0 1
1 4
1 1
5 8
2 5
13 28
7 13
41 80
20 41
121 244
61 121
365 728
182 365
julia> C = B .* A
ERROR: DimensionMismatch("arrays could not be broadcast to a common size; got a dimension with lengths 7 and 2")
Stacktrace:
[1] _bcs1
@ .\broadcast.jl:516 [inlined]
[2] _bcs (repeats 2 times)
@ .\broadcast.jl:510 [inlined]
[3] broadcast_shape
@ .\broadcast.jl:504 [inlined]
[4] combine_axes
@ .\broadcast.jl:499 [inlined]
[5] instantiate
@ .\broadcast.jl:281 [inlined]
[6] materialize(bc::Base.Broadcast.Broadcasted{Base.Broadcast.DefaultArrayStyle{2}, Nothing, typeof(*), Tuple{Matrix{Float64}, Matrix{Int64}}})
@ Base.Broadcast .\broadcast.jl:860
[7] top-level scope
@ REPL[105]:1
Wouldn’t it be more consistent if the elements of A were recognized as square matrices, each to be broadcast multiplied by the corresponding element in B, so that B .* A is again an approximation of exp(x)?
Both [a; b; c] and [a b c] do concatenation, the former vertical, the latter horizontal. You don’t want concatenation here, you want to build a vector of matrices. The correct way to build a vector is [a, b, c] (note: comma, not semicolon or space):
Is there some indexing tip to slice out a particular element of each matrix (e.g., to see how the element is converging)? Extracting one matrix and getting an element works fine:
cs[:] just creates a copy of cs, so the above is no different from writing
cs2 = copy(cs)
cs2[3]
which naturally doesn’t work. I guess you want
getindex.(cs, 3)
Since s is a vector, you don’t need the dims keyword here, just write cs = cumsum(s).
But if you don’t want to waste too much performance, I wouldn’t recommend broadcasting the exponential, since that is extremely wasteful (each iteration takes longer than the previous). Instead use a loop, and produce the next element using a single multiplication with the previous.