crystal-data / num.cr

Scientific computing in pure Crystal
MIT License
151 stars 12 forks source link

`Tensor#min` Does Not Compile #91

Closed dhwa-kyle closed 4 months ago

dhwa-kyle commented 4 months ago

Problem description

Calling #min on a tensor results in the following compiler error:

In lib/num/src/tensor/reduction.cr:242:19

 242 | Num.min(self, axis, dims)
                     ^---
Error: undefined local variable or method 'axis' for Tensor(Float32, CPU(Float32))

The cause is here, where you can see that the implementation of Tensor#min appears to be erroneously copied from Tensor#min(axis, dims). The latter has two parameters that the former does not, which thus causes the undefined variable error.

Example

require "num"

tensor = Tensor(Float32, CPU(Float32)).zeros([2, 2])
tensor[1, 1] = -1
puts tensor.min

Here we would expected -1.0 to be output, but the compiler error instead results.

Workaround

One can just use Num.min tensor instead of tensor.min, but the latter should probably work also.

Proposed Fix

Change the implementation of Tensor#min to Num.min self instead of Num.min(self, axis, dims).

christopherzimmerman commented 4 months ago

Thanks for raising this! I will fix this later today, or if you'd like to raise a PR I can merge that in as well.

christopherzimmerman commented 4 months ago

Closed in #92