From 57389fb8241c1747c8772d6a553e2e9eaa0926c8 Mon Sep 17 00:00:00 2001 From: Chris Zimmerman Date: Thu, 6 Jun 2024 11:44:10 -0400 Subject: [PATCH] Fmt + fix tensor min --- spec/tensor/reduction_spec.cr | 40 +++++++++++++++++++++++++++++++++++ src/tensor/reduction.cr | 2 +- 2 files changed, 41 insertions(+), 1 deletion(-) create mode 100644 spec/tensor/reduction_spec.cr diff --git a/spec/tensor/reduction_spec.cr b/spec/tensor/reduction_spec.cr new file mode 100644 index 00000000..2c6c5a3f --- /dev/null +++ b/spec/tensor/reduction_spec.cr @@ -0,0 +1,40 @@ +# Copyright (c) 2023 Crystal Data Contributors +# +# MIT License +# +# Permission is hereby granted, free of charge, to any person obtaining +# a copy of this software and associated documentation files (the +# "Software"), to deal in the Software without restriction, including +# without limitation the rights to use, copy, modify, merge, publish, +# distribute, sublicense, and/or sell copies of the Software, and to +# permit persons to whom the Software is furnished to do so, subject to +# the following conditions: +# +# The above copyright notice and this permission notice shall be +# included in all copies or substantial portions of the Software. +# +# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, +# EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF +# MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND +# NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE +# LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION +# OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION +# WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. + +require "../spec_helper" + +describe Tensor do + it "can reduce a tensor's min along an axis" do + input = [[-1, 1], [-1, 1]].to_tensor + expected = [-1, -1].to_tensor + + Num::Testing.tensor_equal(input.min(1), expected).should be_true + end + + it "can reduce a tensor's min to a scalar" do + input = [[-2, 1], [-1, 1]].to_tensor + expected = -2 + + (input.min == expected).should be_true + end +end diff --git a/src/tensor/reduction.cr b/src/tensor/reduction.cr index 0bd7b194..1b944301 100644 --- a/src/tensor/reduction.cr +++ b/src/tensor/reduction.cr @@ -239,7 +239,7 @@ class Tensor(T, S) # Num.min(a) # => 3 # ``` def min : T - Num.min(self, axis, dims) + Num.min(self) end # Reduces a `Tensor` along an axis, finding the min of each