Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions lib/numo/linalg/function.rb
Original file line number Diff line number Diff line change
Expand Up @@ -755,7 +755,7 @@ def norm(a, ord=nil, axis:nil, keepdims:false)
axis = [axis]
when Array
if axis.size < 1 || axis.size > 2
raise ArgmentError, "axis option should be 1- or 2-element array"
raise ArgumentError, "axis option should be 1- or 2-element array"
end
else
raise ArgumentError, "invalid option for axis: #{axis}"
Expand All @@ -767,7 +767,7 @@ def norm(a, ord=nil, axis:nil, keepdims:false)
axis.each do |i|
x = idx[i]
if x.nil?
raise ArgmentError, "axis contains same dimension"
raise ArgumentError, "axis contains same dimension"
end
tmp << x
idx[i] = nil
Expand Down Expand Up @@ -816,7 +816,7 @@ def norm(a, ord=nil, axis:nil, keepdims:false)
fixdims = [true] * a.ndim
axis.each do |i|
if i < -a.ndim || i >= a.ndim
raise ArgmentError, "axis (%d) is out of range", i
raise ArgumentError, "axis (%d) is out of range", i
end
fixdims[i] = :new
end
Expand Down
6 changes: 6 additions & 0 deletions spec/linalg/function/norm_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,12 @@
expect { described_class.norm(vec_a, 'fro') }.to raise_error(ArgumentError)
end

it 'raises ArgumentError given an invalid axis option' do
expect { described_class.norm(mat_a, axis: []) }.to raise_error(ArgumentError, /axis option should be 1- or 2-element array/)
expect { described_class.norm(mat_a, axis: [0, 1, 2]) }.to raise_error(ArgumentError, /axis option should be 1- or 2-element array/)
expect { described_class.norm(mat_a, axis: [0, 0]) }.to raise_error(ArgumentError, /axis contains same dimension/)
end

it 'calculates the Froubenius norm of a real matrix' do
norm = Math.sqrt(mat_a.transpose.dot(mat_a).trace)
expect(described_class.norm(mat_a)).to be_within(norm).of(ERR_TOL)
Expand Down