OSDN Git Service

fcbd9f654c7f6a982d69254887952349262eb55d
[mint/mint-lib.git] / lib / mint / generator / partial_fraction_expansion.rb
1 # -*- coding: utf-8 -*-
2
3 module Mint::Generator
4
5   #
6   # 部分分数展開の問題を生成するジェネレータ
7   #
8   # オプション ::
9   #               以下のオプションが使用出来る
10   #               [_order_min_] 次数の最小値 (ex. 2)
11   #               [_order_max_] 次数の最大値 (ex. 2)
12   #               [_x_] 使用する変数 (ex. ['x'] / [['x', 'y']])
13   #               [_factor_minus_] 真なら分母の整数部に負の数を使用する (ex. true)
14   #               [_factor_min_] 分母の整数部の最小値 (ex. 0)
15   #               [_factor_max_] 分母の整数部の最大値 (ex. 100)
16   #               [_coefficient_minus_] 真なら分母の変数部に負の数を使用する (ex. true)
17   #               [_coefficient_min_] 分母の変数部の最小値 (ex. 0)
18   #               [_coefficient_max_] 分母の変数部の最大値 (ex. 100)
19   #               [_numerator_minus_] 真なら分子に負の数を使用するか (ex. true)
20   #               [_numerator_min_] 分子の最小値 (ex. 0)
21   #               [_numerator_max_] 分子の最大値 (ex. 100)
22   #
23   class PartialFractionExpansion < Base
24
25     private
26
27     include Utilities
28
29     def generate_problem
30       defaults = {
31         :order_min         => 2,     :order_max       => 2, :x               => ['x'],
32         :factor_minus      => false, :factor_min      => 1, :factor_max      => 100,
33         :coefficient_minus => false, :coefficient_min => 1, :coefficient_max => 1,
34         :numerator_minus   => false, :numerator_min   => 1, :numerator_max   => 1,
35       }
36       targets = %w[order factor coefficient numerator]
37       options = min_max_order(options(defaults), targets)
38       expression(options)
39     end
40
41     def partial_fraction(options)
42       numerator_part = create_integer(options[:numerator_min], options[:numerator_max], options[:numerator_minus])
43       denominator_part = expansion(options)
44       "#{numerator_part}/#{denominator_part}"
45     end
46
47     def expression(options)
48       result = []
49       result << partial_fraction(options)
50       result.sample
51     end
52
53     def expansion(options)
54       result = []
55       order = create_integer(options[:order_min], options[:order_max], false)
56       x = options[:x].sample
57       order.times{
58         coefficient = factor(options, 'coefficient')
59         factor      = factor(options)
60         result << single(coefficient, factor, x)
61       }
62       result.join
63     end
64   end
65 end
66