# 高校までの数学の計算問題
#(1) 多倍長整数と分数計算
p "(1)",x=Rational(12345678901234567890 ,(1 - 12345678901234567891i)) -
Rational(12345678901234567891,(1 + 12345678901234567890i))
#(2) 多倍長整数と複素数の分数計算
p "(2-1)",x=Rational(12345678901234567890 ,(1 - 12345678901234567891i)) -
Rational(12345678901234567891,(1 + 12345678901234567890i))
p "(2-2)",x+0.0
#(3) 数桁の数字を虚数係数とする分数を要素とする行列の四則演算
require 'matrix'
a = Matrix[[Rational(1, 2 + 3i), Rational(2, 3 + 4i)], [Rational(3, 4 + 5i), Rational(4, 5 + 6i)]]
b = Matrix[[Rational(5, 6 + 7i), Rational(6, 7 + 8i)], [Rational(7, 8 + 9i), Rational(8, 9 + 10i)]]
# 行列の加算
c = a + b
puts "行列の加算: #{c}"
# 行列の減算
d = a - b
puts "行列の減算: #{d}"
# 行列の乗算
e = a * b
puts "行列の乗算: #{e}"
#(4) 整数係数の連立方程式の解
require 'matrix'
m = Matrix[ [1234567890123456789890, 1234567890123456789890*2],
[1234567890123456789890, 1234567890123456789890*3] ]
b = Vector[1234567890123456789890,0]
p "(4-1)",m.lup.solve(b)
p "(4-2)", Matrix[[1234567890123456789890,0]] / m
#(5) 多倍長整数と複素数の分数を係数とする連立方程式の解
m = Matrix[ [1234567890123456789890, 1234567890123456789890*(1+1i)],
[1234567890123456789890, Rational(1234567890123456789890*(2 - 3i),10 + 10i)] ]
b = Vector[1234567890123456789890 * Rational(1 + 1i, 2),0]
p "(5)",m.lup.solve(b)
#(6) フィボナッチ数列の一般項
require 'matrix'
p "(6) フィボナッチ数列ランダムアクセス"
[2,4,5,6,7,8,9,16,17,32,33,64,65,255,256,257].each {|n|
f=(Matrix[[0,1],[1,1]] ** (n))[0,0]
p [n,f.to_s().length, f]
}
p "#(7) 一次方程式の解の法則"
def SolvingLinearEquations(y,a,b)
x= (y -b) / a
end
p "(7-1) 実数解",SolvingLinearEquations(1.0,5, 0.5)
p "(7-2) 分数解",SolvingLinearEquations(Rational(1,1), Rational(5,1), Rational(1,2))
p "(7-3) 虚数解",SolvingLinearEquations(1+1i, 5, 1.0 /(2+ 2i))
p "(7-4) 虚数&分数解",SolvingLinearEquations(Rational(1+1i, 1), Rational(5,1), Rational(1,2+ 2i))
p "(7-5) 多倍長整数の行列解",SolvingLinearEquations(Matrix[[Rational(1234567890123456789890,1),Rational(0,1)]],
Matrix[ [Rational(1234567890123456789890,1), Rational(1234567890123456789890*2,1)],
[Rational(1234567890123456789890,1), Rational(1234567890123456789890*3,1)] ],
Matrix[[Rational(1234567890,1),Rational(123456789,1)]] )
p "(7-7) 多倍長整数 複素数分数の行列解",SolvingLinearEquations(Matrix[[Rational(1234567890123456789890,1i),Rational(0,1)]],
Matrix[ [Rational(1234567890123456789890,1), Rational(1234567890123456789890*2,1i)],
[Rational(1234567890123456789890,1), Rational(1234567890123456789890*3,1i)] ],
Matrix[[1234567890, 0+1i]] )
IyDpq5jmoKHjgb7jgafjga7mlbDlrabjga7oqIjnrpfllY/poYwKIygxKSDlpJrlgI3plbfmlbTmlbDjgajliIbmlbDoqIjnrpcKcCAiKDEpIix4PVJhdGlvbmFsKDEyMzQ1Njc4OTAxMjM0NTY3ODkwICwoMSAtIDEyMzQ1Njc4OTAxMjM0NTY3ODkxaSkpIC0KICBSYXRpb25hbCgxMjM0NTY3ODkwMTIzNDU2Nzg5MSwoMSArIDEyMzQ1Njc4OTAxMjM0NTY3ODkwaSkpCgojKDIpIOWkmuWAjemVt+aVtOaVsOOBqOikh+e0oOaVsOOBruWIhuaVsOioiOeulwoKcCAiKDItMSkiLHg9UmF0aW9uYWwoMTIzNDU2Nzg5MDEyMzQ1Njc4OTAgLCgxIC0gMTIzNDU2Nzg5MDEyMzQ1Njc4OTFpKSkgLQogIFJhdGlvbmFsKDEyMzQ1Njc4OTAxMjM0NTY3ODkxLCgxICsgMTIzNDU2Nzg5MDEyMzQ1Njc4OTBpKSkKcCAiKDItMikiLHgrMC4wCgojKDMpIOaVsOahgeOBruaVsOWtl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