Class: CABinOp

Inherits:
Object
  • Object
show all
Defined in:
lib/carray/lazy.rb,
yard-stubs/carray_lazy.rb

Overview

Lazy element-wise view of a binary operation on two operands. Nothing is computed until the chain is forced, so a composed expression evaluates in one pass instead of materialising an intermediate per operator.

Produced by an operator on .lazy operands or inside CArray.fuse, not constructed directly.

Constant Summary collapse

OP_NAMES =

op_id => :name reverse lookup

CArray::LAZY_BINOP_OP_IDS.invert.merge(
  OP_IPOWER => :ipow,
).freeze

Copy and conversion collapse

Instance Method Summary collapse

Instance Method Details

#copy ⇒ CArray

Evaluates the expression and returns the result as a new entity.

If an expression evaluator is registered through CArray.expression_evaluator it is asked first; the ordinary element-wise walk is what happens when it declines, when none is registered, or when the array is small enough that walking is the faster answer.

Returns:

  • (CArray) —

    a newly evaluated entity.



88
# File 'yard-stubs/carray_lazy.rb', line 88

def copy; end

#dump_tree(indent = 0) ⇒ String

Returns an ASCII summary of the lazy expression tree, walking both children. Does not materialise.

Returns:

  • (String)


690
691
692
693
694
695
696
697
698
699
700
701
# File 'lib/carray/lazy.rb', line 690

def dump_tree(indent = 0)
  pad = "  " * indent
  out = +"#{pad}#{op_label}\n"
  [parent, __binop_right__].each do |child|
    out << if child.is_a?(CAMonOp) || child.is_a?(CABinOp)
             child.dump_tree(indent + 1)
           else
             "#{"  " * (indent + 1)}#{child.class}(#{child.data_type_name}, #{child.shape.inspect})\n"
           end
  end
  out
end

#each({ |value| ... }) ⇒ Object

Materialises self and yields each element.

Returns:

  • (Object)


706
707
708
# File 'lib/carray/lazy.rb', line 706

def each(&block)
  to_ca.each(&block)
end

#inspect ⇒ String

Returns a summary of the binop chain without materialising.

Returns:

  • (String)


675
676
677
# File 'lib/carray/lazy.rb', line 675

def inspect
  "#<CABinOp #{op_label}(#{parent.inspect}, #{__binop_right__.inspect})>"
end

#sort(*args, **kwargs) ⇒ CArray

Materialises self and sorts the resulting entity.

Returns:



720
721
722
# File 'lib/carray/lazy.rb', line 720

def sort(*args, **kwargs)
  to_ca.sort(*args, **kwargs)
end

#to_a ⇒ Array

Materialises self and returns the resulting Array.

Returns:



713
714
715
# File 'lib/carray/lazy.rb', line 713

def to_a
  to_ca.to_a
end

#to_ca ⇒ CArray

Evaluates the expression and returns the result as a new entity.

A two-operand element-wise operation holds no data of its own, so there is nothing to hand over unevaluated: unlike CArray#to_ca, which returns self, this materialises -- the Ruby Enumerable#to_a / lazy force convention.

The entity is detached from the operands, so writes to it reach nothing. writable: true is therefore refused rather than answered with a result that would swallow them.

Parameters:

  • writable (Boolean) —

    whether the caller needs writes to land back in the source; only false can be satisfied.

Returns:

  • (CArray) —

    a newly evaluated entity.

Raises:

  • (RuntimeError) —

    when writable: true is requested.



77
# File 'yard-stubs/carray_lazy.rb', line 77

def to_ca(writable: false); end

#to_memory_view ⇒ Object

Not supported for lazy binops.

Raises:

  • (TypeError) —

    always.

Raises:

  • (TypeError)


727
728
729
730
731
732
# File 'lib/carray/lazy.rb', line 727

def to_memory_view
  raise TypeError,
        "lazy view (CABinOp) has no backing buffer; call `.to_ca` " \
        "first to materialise into an entity before exporting via " \
        "MemoryView."
end

#to_s ⇒ String

Alias of #inspect.

Returns:

  • (String)


682
683
684
# File 'lib/carray/lazy.rb', line 682

def to_s
  inspect
end