Class: VectorNumber

Inherits:
Object
  • Object
show all
Includes:
Comparing, Converting, Enumerating, MathConverting, Mathing, Querying, Stringifying
Defined in:
lib/vector_number.rb,
lib/vector_number/mathing.rb,
lib/vector_number/version.rb,
lib/vector_number/querying.rb,
lib/vector_number/comparing.rb,
lib/vector_number/converting.rb,
lib/vector_number/enumerating.rb,
lib/vector_number/stringifying.rb,
lib/vector_number/math_converting.rb,
lib/vector_number/numeric_refinements.rb

Overview

A class to add together anything.

Defined Under Namespace

Modules: Comparing, Converting, Enumerating, MathConverting, Mathing, NumericRefinements, Querying, Stringifying

Constant Summary collapse

KNOWN_OPTIONS =

Keys for possible options. Unknown options will be rejected when creating a vector.

Returns:

  • (Array<Symbol>)

Since:

  • 0.2.0

%i[mult].freeze
DEFAULT_OPTIONS =

Default values for options.

Returns:

  • (Hash{Symbol => Object})

Since:

  • 0.2.0

{ mult: :dot }.freeze
UNIT =

Get a unit for nth numeric dimension, where 1 is real, 2 is imaginary.

Since:

  • 0.2.0

->(n) { n }.freeze
R =

Constant for real unit.

Since:

  • 0.2.0

UNIT[1]
I =

Constant for imaginary unit.

Since:

  • 0.1.0

UNIT[2]
VERSION =

Returns:

  • (String)
"0.4.3"

Constants included from Stringifying

Stringifying::MULT_STRINGS

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Stringifying

#inspect, #to_s

Methods included from Querying

#finite?, #infinite?, #integer?, #negative?, #nonnumeric?, #nonzero?, #numeric?, #positive?, #real?, #zero?

Methods included from Comparing

#<=>, #==, #eql?, #hash

Methods included from Enumerating

#[], #coefficients, #each, #to_h, #unit?, #units

Methods included from Converting

#imaginary, #real, #to_c, #to_d, #to_f, #to_i, #to_r

Methods included from MathConverting

#abs, #abs2, #ceil, #floor, #round, #truncate

Methods included from Mathing

#%, #*, #+, #-, #-@, #/, #coerce, #div, #divmod, #fdiv, #remainder

Constructor Details

#initialize(values = nil, options = nil) {|coefficient| ... } ⇒ VectorNumber

Create new VectorNumber from values, possibly specifying options, possibly modifying coefficients with a block.

Examples:

VectorNumber.new(1, 2, 3) # ArgumentError
VectorNumber.new([1, 2, 3]) # => (6)
VectorNumber.new(["b", VectorNumber::I], mult: :asterisk) # => (1*'b' + 1i)
VectorNumber.new # => (0)

with a block

VectorNumber.new(["a", "b", "c", 3]) { _1 * 2 } # => (2⋅'a' + 2⋅'b' + 2⋅'c' + 6)
VectorNumber.new(["a", "b", "c", 3], &:-@) # => (-1⋅'a' - 1⋅'b' - 1⋅'c' - 3)
VectorNumber.new(["a", "b", "c", 3], &:digits) # RangeError

Parameters:

  • values (Array, VectorNumber, Hash{Object => Integer, Float, Rational, BigDecimal}, nil) (defaults to: nil)

    values for this number, hashes are treated like plain vector numbers

  • options (Hash{Symbol => Object}, nil) (defaults to: nil)

    options for this number, if values is a VectorNumber or contains it, these will be merged with options from its options

Options Hash (options):

Yield Parameters:

  • coefficient (Integer, Float, Rational, BigDecimal)

Yield Returns:

  • (Integer, Float, Rational, BigDecimal)

    new coefficient

Raises:

  • (RangeError)

    if any pesky non-reals get where they shouldn’t



110
111
112
113
114
115
116
117
118
119
# File 'lib/vector_number.rb', line 110

def initialize(values = nil, options = nil, &transform)
  # @type var options: Hash[Symbol, Object]
  initialize_from(values)
  apply_transform(&transform)
  finalize_contents
  save_options(options, values: values)
  @options.freeze
  @data.freeze
  freeze
end

Instance Attribute Details

#optionsHash{Symbol => Object} (readonly)

Options used for this vector.

Returns:

  • (Hash{Symbol => Object})

See Also:

Since:

  • 0.1.0



85
86
87
# File 'lib/vector_number.rb', line 85

def options
  @options
end

#sizeInteger (readonly)

Number of non-zero dimensions.

Returns:

  • (Integer)

Since:

  • 0.1.0



76
77
78
# File 'lib/vector_number.rb', line 76

def size
  @size
end

Class Method Details

.[](*values, **options) ⇒ VectorNumber

Create new VectorNumber from a list of values, possibly specifying options.

Examples:

VectorNumber[1, 2, 3] # => (6)
VectorNumber[[1, 2, 3]] # => (1⋅[1, 2, 3])
VectorNumber["b", VectorNumber::I, mult: :asterisk] # => (1*'b' + 1i)
VectorNumber[] # => (0)
VectorNumber["b", VectorNumber["b"]] # => (2⋅'b')
VectorNumber["a", "b", "a"] # => (2⋅'a' + 1⋅'b')

Parameters:

  • values (Array<Object>)

    values to put in the number

  • options (Hash{Symbol => Object})

    options for the number

Options Hash (**options):

Returns:

Since:

  • 0.1.0



67
68
69
# File 'lib/vector_number.rb', line 67

def self.[](*values, **options)
  new(values, options)
end

Instance Method Details

#+@VectorNumber Also known as: dup

Return self.

Returns:

Since:

  • 0.2.0



126
127
# File 'lib/vector_number.rb', line 126

def +@ = self
# @since 0.2.4

#clone(freeze: true) ⇒ VectorNumber

Return self.

Returns:

Raises:

  • (ArgumentError)

    if freeze is not true or nil.

Since:

  • 0.2.4



136
137
138
139
140
141
142
143
144
145
# File 'lib/vector_number.rb', line 136

def clone(freeze: true)
  case freeze
  when true, nil
    self
  when false
    raise ArgumentError, "can't unfreeze #{self.class}"
  else
    raise ArgumentError, "unexpected value for freeze: #{freeze.class}"
  end
end