Class: CABinOp

Inherits:
Object
  • Object
show all
Defined in:
lib/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

Instance Method Summary collapse

Instance Method Details

#dump_tree(indent = 0) ⇒ String

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

Returns:

  • (String)


687
688
689
690
691
692
693
694
695
696
697
698
# File 'lib/carray/lazy.rb', line 687

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)


703
704
705
# File 'lib/carray/lazy.rb', line 703

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

#inspectString

Returns a summary of the binop chain without materialising.

Returns:

  • (String)


672
673
674
# File 'lib/carray/lazy.rb', line 672

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

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

Materialises self and sorts the resulting entity.

Returns:



717
718
719
# File 'lib/carray/lazy.rb', line 717

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

#to_aArray

Materialises self and returns the resulting Array.

Returns:



710
711
712
# File 'lib/carray/lazy.rb', line 710

def to_a
  to_ca.to_a
end

#to_memory_viewObject

Not supported for lazy binops.

Raises:

  • (TypeError)

    always.

Raises:

  • (TypeError)


724
725
726
727
728
729
# File 'lib/carray/lazy.rb', line 724

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_sString

Alias of #inspect.

Returns:

  • (String)


679
680
681
# File 'lib/carray/lazy.rb', line 679

def to_s
  inspect
end