赞 | 2 |
VIP | 335 |
好人卡 | 3 |
积分 | 1 |
经验 | 14077 |
最后登录 | 2020-1-28 |
在线时间 | 264 小时 |
Lv1.梦旅人 冰
- 梦石
- 0
- 星屑
- 142
- 在线时间
- 264 小时
- 注册时间
- 2006-11-22
- 帖子
- 1057
|
加入我们,或者,欢迎回来。
您需要 登录 才可以下载或查看,没有帐号?注册会员
x
修正log10、log bug
只经过初步测试…指数只能整数…- -
方法
self + other
self - other
self * other
self / other
self ** other
算术运算符。分别表示和(加)、差(减)、积(乘)、商(除)、幂(乘方)。
self == other
判断相等。
abs
绝对值。
polar
极式。
unploar
a = Complex(1,2).polar
p Complex(a[0],a[1]).unpolar # => Complex(1,2)
Math:
csc
sec
cot
sincostan的倒数。
qu
二次方程
fact
阶乘。
cbrt
立方根。
to_rad
度数转弧度。
to_deg
弧度转度数。
nCr
…这东西中文叫?
sqrt
exp
log
log10
sin
cos
tan
sinh
cosh
tanh
asin
acos
atan
asinh
acosh
atanh
扩展复数运算。
Object:
Complex(re,im)
建立一个复数。
Numeric:
to_c
- class Complex
- attr(:re)
- attr(:im)
- def initialize(re, im)
- @re = re
- @im = im
- end
- def +(other)
- if other.class == Complex
- return Complex(@re+other.re, @im+other.im)
- else
- return Complex(@re+other, @im)
- end
- end
- def -(other)
- if other.class == Complex
- return Complex(@re-other.re, @im-other.im)
- else
- return Complex(@re-other, @im)
- end
- end
- def *(other)
- if other.class == Complex
- return Complex(@re*other.re-@im*other.im, @im*other.re+@re*other.im)
- else
- return Complex(@re*other, @im*other)
- end
- end
- def /(other)
- if other.class == Complex
- a = (@re*other.re+@im*other.im)/(other.re**2+other.im**2)
- b = (@im*other.re-@re*other.im)/(other.re**2+other.im**2)
- return Complex(a, b)
- else
- return Complex(@re/other, @im/other)
- end
- end
- def **(other)
- times = other
- if @im == 0
- return @re**other
- else
- if times >= 2
- result = self*self
- times -= 2
- result**times
- end
- return result
- end
- end
- def ==(other)
- a = @re == other.re
- b = @im == other.im
- if a == true and b == true
- return ture
- else
- return false
- end
- end
- def abs
- return Math.sqrt(@re**2+@im**2)
- end
- def polar
- z = Math.sqrt(@re**2 + @im ** 2)
- theta = Math.atan(Float(@im)/Float(@re))
- return z, theta
- end
- def unpolar
- r = @re
- theta = @im
- return Complex(r*Math.cos(theta), r*Math.sin(theta))
- end
- def coerce(numeric)
- if numeric.class != Complex
- return numeric.to_c, self
- end
- end
- def +@
- return self
- end
- def -@
- return Complex(-@re,-@im)
- end
- end
- class Object
- def Complex(re,lm)
- return Complex.new(re,lm)
- end
- end
- class Numeric
- def to_c
- return Complex(self, 0)
- end
- end
- #==============================================================================
- # ■ Math
- #------------------------------------------------------------------------------
- # 支持浮点运算的模块。
- #==============================================================================
- module Math
- #--------------------------------------------------------------------------
- # ● 恒量
- #--------------------------------------------------------------------------
- I = Complex(0,1)
- #--------------------------------------------------------------------------
- # ● Csc
- #--------------------------------------------------------------------------
- def self.csc(x)
- return 1/self.sin(x)
- end
- #--------------------------------------------------------------------------
- # ● Sec
- #--------------------------------------------------------------------------
- def self.sec(x)
- return 1/self.cos(x)
- end
- #--------------------------------------------------------------------------
- # ● Cot
- #--------------------------------------------------------------------------
- def self.cot(x)
- return 1/self.tan(x)
- end
- #--------------------------------------------------------------------------
- # ● 二次方程一
- #--------------------------------------------------------------------------
- def self.qu(a, b, c)
- y1 = (-1*b + self.sqrt(b*b - 4*a*c)) / 2*a
- y2 = (-1*b - self.sqrt(b*b - 4*a*c)) / 2*a
- if y1 == y2
- return y1
- else
- return y1,y2
- end
- end
- #--------------------------------------------------------------------------
- # ● 阶乘
- #--------------------------------------------------------------------------
- def self.fact(numb)
- if numb == 0
- return 1
- end
- if numb.is_a?(Integer)
- for i in 1...numb
- numb *= i
- end
- return numb
- else
- result = self.sqrt(2*PI*numb) * (numb/E)**numb * (1+1/12*numb+1/288*numb**2-139/51840*numb**3-571/2488320*numb**4)
- return result
- end
- end
- #--------------------------------------------------------------------------
- # ● 立方根
- #--------------------------------------------------------------------------
- def self.cbrt(x)
- return self.exp(self.log(x)/3)
- end
- #--------------------------------------------------------------------------
- # ● 度数转弧度
- #--------------------------------------------------------------------------
- def self.to_rad(x)
- r = x*(PI/180)
- return r
- end
- #--------------------------------------------------------------------------
- # ● 弧度转度数
- #--------------------------------------------------------------------------
- def self.to_deg(x)
- d = x*(180/PI)
- return d
- end
- #--------------------------------------------------------------------------
- # ● nCr
- #--------------------------------------------------------------------------
- def self.nCr(n, r)
- return (self.fact(n))/(self.fact(r)*self.fact(n-r))
- end
- end
- #==============================================================================
- # ■ Math
- #------------------------------------------------------------------------------
- # 扩展复数运算。
- #==============================================================================
- class << Math
- alias old_sqrt sqrt
- alias old_exp exp
- alias old_log log
- alias old_log10 log10
- alias old_sin sin
- alias old_cos cos
- alias old_tan tan
- alias old_sinh sinh
- alias old_cosh cosh
- alias old_tanh tanh
- alias old_asin asin
- alias old_acos acos
- alias old_atan atan
- alias old_asinh asinh
- alias old_acosh acosh
- alias old_atanh atanh
- #--------------------------------------------------------------------------
- # ● 平方根
- #--------------------------------------------------------------------------
- def sqrt(z)
- if z.class == Complex
- r = z.abs
- x = z.re
- return Complex(old_sqrt((r + x) / 2), old_sqrt((r - x) / 2))
- else
- if z < 0
- return Complex.new(0, old_sqrt(z.abs))
- else
- old_sqrt(z)
- end
- end
- end
- #--------------------------------------------------------------------------
- # ● 指数函数
- #--------------------------------------------------------------------------
- def exp(z)
- if z.class == Complex
- return Complex(old_exp(z.re) * old_cos(z.im), old_exp(z.re) * old_sin(z.im))
- else
- old_exp(z)
- end
- end
- #--------------------------------------------------------------------------
- # ● 自然对数
- #--------------------------------------------------------------------------
- def log(z)
- if z.class == Complex
- return Complex(old_log(z.abs), z.polar[1])
- else
- if z < 0
- return Complex(log(z.abs), Math::PI)
- else
- old_log(z)
- end
- end
- end
- #--------------------------------------------------------------------------
- # ● 常用对数
- #--------------------------------------------------------------------------
- def log10(z)
- if z.class == Complex
- u = Complex(old_log10(z.abs), z.polar[1]/old_log(10))
- return u
- else
- if z < 0
- return Complex(old_log10(z.abs), old_log10(Math::E)*Math::PI)
- else
- old_log10(z)
- end
- end
- end
- #--------------------------------------------------------------------------
- # ● sin
- #--------------------------------------------------------------------------
- def sin(z)
- if z.class == Complex
- return Complex(old_sin(z.re) * old_cosh(z.im), old_cos(z.re) * old_sinh(z.im))
- else
- old_sin(z)
- end
- end
- #--------------------------------------------------------------------------
- # ● cos
- #--------------------------------------------------------------------------
- def cos(z)
- if z.class == Complex
- return Complex(old_cos(z.re) * old_cosh(z.im), -old_sin(z.re) * old_sinh(z.im))
- else
- old_cos(z)
- end
- end
- #--------------------------------------------------------------------------
- # ● tan
- #--------------------------------------------------------------------------
- def tan(z)
- if z.class == Complex
- return sin(z)/cos(z)
- else
- old_tan(z)
- end
- end
- #--------------------------------------------------------------------------
- # ● sinh
- #--------------------------------------------------------------------------
- def sinh(z)
- if z.class == Complex
- return Complex(old_sinh(z.re) * old_cos(z.im), old_cosh(z.re) * old_sin(z.im))
- else
- old_sinh(z)
- end
- end
- #--------------------------------------------------------------------------
- # ● cosh
- #--------------------------------------------------------------------------
- def cosh(z)
- if z.class == Complex
- return Complex(old_cosh(z.re) * old_cos(z.im), old_sinh(z.re) * old_sin(z.im))
- else
- old_cosh(z)
- end
- end
- #--------------------------------------------------------------------------
- # ● tanh
- #--------------------------------------------------------------------------
- def tanh(z)
- if z.class == Complex
- return sinh(z) / cosh(z)
- else
- old_tanh(z)
- end
- end
- #--------------------------------------------------------------------------
- # ● asin
- #--------------------------------------------------------------------------
- def asin(z)
- if z.class == Complex
- return log(Math::I * z + sqrt(1.0 - z * z))/Math::I
- else
- old_asin(z)
- end
- end
- #--------------------------------------------------------------------------
- # ● acos
- #--------------------------------------------------------------------------
- def acos(z)
- if z.class == Complex
- return log(z + Math::I * sqrt(1.0 - z * z))/Math::I
- else
- old_acos(z)
- end
- end
- #--------------------------------------------------------------------------
- # ● atan
- #--------------------------------------------------------------------------
- def atan(z)
- if z.class == Complex
- return log((1.0 + z * Math::I)/(1.0 - z * Math::I))/Math::I*2
- else
- old_atan(z)
- end
- end
- #--------------------------------------------------------------------------
- # ● asinh
- #--------------------------------------------------------------------------
- def asinh(z)
- if z.class == Complex
- return log(z + sqrt(1.0 + z * z))
- else
- old_asinh(z)
- end
- end
- #--------------------------------------------------------------------------
- # ● acosh
- #--------------------------------------------------------------------------
- def acosh(z)
- if z.class == Complex
- return log(z + sqrt(z * z - 1.0))
- else
- old_acosh(z)
- end
- end
- #--------------------------------------------------------------------------
- # ● atanh
- #--------------------------------------------------------------------------
- def atanh(z)
- if z.class == Complex
- return log((1.0 + z) / (1.0 - z)) / 2.0
- else
- old_atanh(z)
- end
- end
- end
复制代码 |
|