I have the following codes:
julia> A = [1 2; 3 4];
julia> function test!(A)
A = permutedims(A, [2,1])
end
test! (generic function with 1 method)
julia> test!(A)
2×2 Array{Int64,2}:
1 3
2 4
julia> A
2×2 Array{Int64,2}:
1 2
3 4
Could someone explain why I reassign A inside test!, but A is not changed? Thanks!
Never mind, I have answer to my own question (I can change the values but cannot change the pointer and permutedims create a new local variable). I have another related question. I have the following codes:
A = [1 2; 3 4]
b = [5.0 7; 6 8]
function solve!(A,b)
sol = A\b
b .= sol
end
function solveTwoTime!(A,b)
solve!(A,b)
b = permutedims(b,[2,1]) # permute rows vs columns
solve!(A,b)
b = permutedims(b,[2,1])
end
The idea is I want to call solve two times, first with the columns, then with the rows. However, I do not want to create new local variable. If permutedims can manipulate b (like reshape) and does not create new local variable, then it will be great! Is there anyway to handle this case? Basically the idea is I do not want to create a local variable to improve the performance. Thanks.
Just do b'
which is a lazy transpose. Also, consider calling factorize(A)
and use the returned object to solve twice.
Also see
julia> using LinearAlgebra
help?> LinearAlgebra.ldiv!
ldiv!(Y, A, B) -> Y
Compute A \ B in-place and store the result in Y, returning the result.
The argument A should not be a matrix. Rather, instead of matrices it should be
a factorization object (e.g. produced by factorize or cholesky). The reason for
this is that factorization itself is both expensive and typically allocates
memory (although it can also be done in-place via, e.g., lu!), and
performance-critical situations requiring ldiv! usually also require
fine-grained control over the factorization of A.
1 Like