mirror of
https://github.com/c9s/bbgo.git
synced 2024-11-22 23:05:15 +00:00
136 lines
1.7 KiB
Go
136 lines
1.7 KiB
Go
package fixedpoint
|
|
|
|
type ValueMap map[string]Value
|
|
|
|
func (m ValueMap) Eq(n ValueMap) bool {
|
|
if len(m) != len(n) {
|
|
return false
|
|
}
|
|
|
|
for m_k, m_v := range m {
|
|
n_v, ok := n[m_k]
|
|
if !ok {
|
|
return false
|
|
}
|
|
|
|
if !m_v.Eq(n_v) {
|
|
return false
|
|
}
|
|
}
|
|
|
|
return true
|
|
}
|
|
|
|
func (m ValueMap) Add(n ValueMap) ValueMap {
|
|
if len(m) != len(n) {
|
|
panic("unequal length")
|
|
}
|
|
|
|
o := ValueMap{}
|
|
|
|
for k, v := range m {
|
|
o[k] = v.Add(n[k])
|
|
}
|
|
|
|
return o
|
|
}
|
|
|
|
func (m ValueMap) Sub(n ValueMap) ValueMap {
|
|
if len(m) != len(n) {
|
|
panic("unequal length")
|
|
}
|
|
|
|
o := ValueMap{}
|
|
|
|
for k, v := range m {
|
|
o[k] = v.Sub(n[k])
|
|
}
|
|
|
|
return o
|
|
}
|
|
|
|
func (m ValueMap) Mul(n ValueMap) ValueMap {
|
|
if len(m) != len(n) {
|
|
panic("unequal length")
|
|
}
|
|
|
|
o := ValueMap{}
|
|
|
|
for k, v := range m {
|
|
o[k] = v.Mul(n[k])
|
|
}
|
|
|
|
return o
|
|
}
|
|
|
|
func (m ValueMap) Div(n ValueMap) ValueMap {
|
|
if len(m) != len(n) {
|
|
panic("unequal length")
|
|
}
|
|
|
|
o := ValueMap{}
|
|
|
|
for k, v := range m {
|
|
o[k] = v.Div(n[k])
|
|
}
|
|
|
|
return o
|
|
}
|
|
|
|
func (m ValueMap) AddScalar(x Value) ValueMap {
|
|
o := ValueMap{}
|
|
|
|
for k, v := range m {
|
|
o[k] = v.Add(x)
|
|
}
|
|
|
|
return o
|
|
}
|
|
|
|
func (m ValueMap) SubScalar(x Value) ValueMap {
|
|
o := ValueMap{}
|
|
|
|
for k, v := range m {
|
|
o[k] = v.Sub(x)
|
|
}
|
|
|
|
return o
|
|
}
|
|
|
|
func (m ValueMap) MulScalar(x Value) ValueMap {
|
|
o := ValueMap{}
|
|
|
|
for k, v := range m {
|
|
o[k] = v.Mul(x)
|
|
}
|
|
|
|
return o
|
|
}
|
|
|
|
func (m ValueMap) DivScalar(x Value) ValueMap {
|
|
o := ValueMap{}
|
|
|
|
for k, v := range m {
|
|
o[k] = v.Div(x)
|
|
}
|
|
|
|
return o
|
|
}
|
|
|
|
func (m ValueMap) Sum() Value {
|
|
var sum Value
|
|
for _, v := range m {
|
|
sum = sum.Add(v)
|
|
}
|
|
return sum
|
|
}
|
|
|
|
func (m ValueMap) Normalize() ValueMap {
|
|
sum := m.Sum()
|
|
if sum.Eq(Zero) {
|
|
panic("zero sum")
|
|
}
|
|
|
|
return m.DivScalar(sum)
|
|
}
|