Module: CAMath

Includes:
Math
Defined in:
lib/carray.rb,
lib/carray/math.rb,
ext/carray_operator.c

Overview

Top-level CAMath module -- math namespace for CArrays. CAMath.sin(ca) works because module functions inherited from Math (via include) accept any object that responds to the corresponding instance method; CArray defines those in C.

Class Method Summary collapse

Class Method Details

.atan2(y, x) ⇒ CArray

Returns the element-wise arc tangent of y / x with quadrant selection.

Parameters:

  • y (CArray, Numeric)

    numerator.

  • x (CArray, Numeric)

    denominator.

Returns:



30
# File 'lib/carray/math.rb', line 30

def atan2(y, x);     CArray.wrap_readonly(y, :float64).atan2(x);     end

.copysign(x, y) ⇒ CArray

Returns |x| with the sign of y, element-wise.

Parameters:

  • x (CArray, Numeric)

    magnitude source.

  • y (CArray, Numeric)

    sign source.

Returns:



44
# File 'lib/carray/math.rb', line 44

def copysign(x, y);  CArray.wrap_readonly(x, :float64).copysign(y);  end

.expm1(x) ⇒ CArray

Returns exp(x) - 1 element-wise as float64.

Parameters:

  • x (CArray, Numeric)

    input value.

Returns:



16
# File 'lib/carray/math.rb', line 16

def expm1(x);  CArray.wrap_readonly(x, :float64).expm1;  end

.fmod(x, y) ⇒ CArray

Returns the C-style fmod(x, y) element-wise (sign follows x).

Parameters:

  • x (CArray, Numeric)

    dividend.

  • y (CArray, Numeric)

    divisor.

Returns:



67
# File 'lib/carray/math.rb', line 67

def fmod(x, y);      CArray.wrap_readonly(x, :float64).fmod(y);      end

.hypot(x, y) ⇒ CArray

Returns the element-wise Euclidean distance sqrt(x^2 + y^2).

Parameters:

  • x (CArray, Numeric)

    first leg.

  • y (CArray, Numeric)

    second leg.

Returns:



37
# File 'lib/carray/math.rb', line 37

def hypot(x, y);     CArray.wrap_readonly(x, :float64).hypot(y);     end

.log1p(x) ⇒ CArray

Returns log(1 + x) element-wise as float64.

Parameters:

  • x (CArray, Numeric)

    input value.

Returns:



22
# File 'lib/carray/math.rb', line 22

def log1p(x);  CArray.wrap_readonly(x, :float64).log1p;  end

.logaddexp(x, y) ⇒ CArray

Returns log(exp(x) + exp(y)) computed to avoid overflow, element-wise.

Parameters:

  • x (CArray, Numeric)

    first log-space value.

  • y (CArray, Numeric)

    second log-space value.

Returns:



52
# File 'lib/carray/math.rb', line 52

def logaddexp(x, y); CArray.wrap_readonly(x, :float64).logaddexp(y); end

.max(*argv) ⇒ CArray

Returns the element-wise maximum of the given CArray and other arguments. At least one argument must be a CArray.

Parameters:

Returns:

  • (CArray)

    fresh CArray holding the running max.

Raises:

  • (RuntimeError)

    when no CArray argument is present.



94
95
96
97
98
99
100
101
102
103
104
105
# File 'lib/carray/math.rb', line 94

def max (*argv)
  if ary = argv.find{|x| x.is_a?(CArray) }
    out = ary.copy
    argv.delete(ary)
    argv.each do |x|
      out.pmax!(x)
    end
  else
    raise "args should contain more than one CArray object"
  end
  return out
end

.min(*argv) ⇒ CArray

Returns the element-wise minimum of the given CArray and other arguments. At least one argument must be a CArray.

Parameters:

Returns:

  • (CArray)

    fresh CArray holding the running min.

Raises:

  • (RuntimeError)

    when no CArray argument is present.



75
76
77
78
79
80
81
82
83
84
85
86
# File 'lib/carray/math.rb', line 75

def min (*argv)
  if ary = argv.find{|x| x.is_a?(CArray) }
    out = ary.copy
    argv.delete(ary)
    argv.each do |x|
      out.pmin!(x)
    end
  else
    raise "args should contain more than one CArray object"
  end
  return out
end

.nextafter(x, y) ⇒ CArray

Returns the next representable float from x toward y, element-wise.

Parameters:

  • x (CArray, Numeric)

    starting value.

  • y (CArray, Numeric)

    direction target.

Returns:



60
# File 'lib/carray/math.rb', line 60

def nextafter(x, y); CArray.wrap_readonly(x, :float64).nextafter(y); end