Skip to content

Commit

Permalink
Add rownorm² for multiple rows
Browse files Browse the repository at this point in the history
  • Loading branch information
nHackel committed Feb 20, 2024
1 parent 6ad1a26 commit ace388d
Showing 1 changed file with 13 additions and 5 deletions.
18 changes: 13 additions & 5 deletions src/Utils.jl
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,14 @@ export rownorm², nrmsd
"""
This function computes the 2-norm² of a rows of S for dense matrices.
"""
function rownorm²(B::Transpose{T,S},row::Int) where {T,S<:DenseMatrix}
function rownorm²(B::Transpose{T,S},row::Int64) where {T,S<:DenseMatrix}
A = B.parent
U = real(eltype(A))
res::U = BLAS.nrm2(size(A,1), pointer(A,(LinearIndices(size(A)))[1,row]), 1)^2
return res
end

function rownorm²(A::AbstractMatrix,row::Int)
function rownorm²(A::AbstractMatrix,row::Int64)
T = real(eltype(A))
res = zero(T)
@simd for n=1:size(A,2)
Expand All @@ -19,19 +19,27 @@ function rownorm²(A::AbstractMatrix,row::Int)
return res
end

rownorm²(A::AbstractLinearOperator,row) = rownorm²(Matrix(A[row, :]), 1)
rownorm²(A::ProdOp{T, <:WeightingOp, matT}, row) where {T, matT} = A.A.weights[row]^2*rownorm²(A.B, row)
rownorm²(A::AbstractLinearOperator,row::Int64) = rownorm²(Matrix(A[row, :]), 1)
rownorm²(A::ProdOp{T, <:WeightingOp, matT}, row::Int64) where {T, matT} = A.A.weights[row]^2*rownorm²(A.B, row)

"""
This function computes the 2-norm² of a rows of S for sparse matrices.
"""
function rownorm²(B::Transpose{T,S},row::Int) where {T,S<:SparseMatrixCSC}
function rownorm²(B::Transpose{T,S},row::Int64) where {T,S<:SparseMatrixCSC}
A = B.parent
U = real(eltype(A))
res::U = BLAS.nrm2(A.colptr[row+1]-A.colptr[row], pointer(A.nzval,A.colptr[row]), 1)^2
return res
end

function rownorm²(A, rows)
res = zero(real(eltype(A)))
@simd for row in rows
res += rownorm²(A, row)
end
return res
end


### dot_with_matrix_row ###

Expand Down

0 comments on commit ace388d

Please sign in to comment.