Sha256: 59e78fc8089598b8e71483e331e7deefdbf9075773c4bb0e052da5349b6f0285

Contents?: true

Size: 947 Bytes

Versions: 5

Compression:

Stored size: 947 Bytes

Contents

module Unitwise
  module Expression
    class Composer
      attr_reader :terms
      def initialize(terms)
        @terms = terms
      end

      def set
        @set ||= terms.reduce(SignedMultiset.new) do |s, t|
          s.increment({f: t.factor, p: t.prefix, a: t.atom}, t.exponent); s
        end
      end

      def numerator
        @numerator ||= set.select{|k,v| v > 0}.map do |k,v|
          "#{k[:f] if k[:f] != 1}#{k[:p]}#{k[:a]}#{v if v != 1}"
        end.select{|t| !t.empty?}.join('.')
      end

      def denominator
        @denominator ||= set.select{|k,v| v < 0}.map do |k,v|
          "#{k[:f] if k[:f] != 1}#{k[:p]}#{k[:a]}#{-v if v != -1}"
        end.select{|t| !t.empty?}.join('.')
      end

      def expression
        @expression = []
        @expression << (numerator.empty? ? '1' : numerator)
        (@expression << denominator) unless denominator.empty?
        @expression.join('/')
      end
    end
  end
end

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
unitwise-0.6.0 lib/unitwise/expression/composer.rb
unitwise-0.5.1 lib/unitwise/expression/composer.rb
unitwise-0.5.0 lib/unitwise/expression/composer.rb
unitwise-0.4.0 lib/unitwise/expression/composer.rb
unitwise-0.3.2 lib/unitwise/expression/composer.rb