-
Notifications
You must be signed in to change notification settings - Fork 1
/
complex.go
101 lines (82 loc) · 2.17 KB
/
complex.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
// Copyright 2009 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
/* This package implements some basic functionality for
complex numbers.
Add
*/
package complex
import "math"
type Complex struct {
real float64;
im float64;
}
func NewComplex(_real, _im float64) *Complex {
c := new(Complex);
c.real = _real;
c.im = _im;
return c;
}
// Returns the absolute value
func Abs(z *Complex) float64 {
sq := (z.real * z.real) + (z.im * z.im);
result := math.Sqrt(sq);
return result;
}
// Returns the conjugate
func Conj(z *Complex) *Complex {
result := NewComplex(z.real, -(z.im));
return result;
}
// Adds another complex to this complex (similar to "+=" operator)
func (c *Complex) AddTo(b *Complex) {
c.real += b.real;
c.im += b.im;
}
// Adds two complex numbers together to make a third complex
func Add(a, b *Complex) *Complex {
c := NewComplex(a.real, a.im);
c.AddTo(b);
return c;
}
// Subtracts another complex from this complex (similar to "-=" operator)
func (c *Complex) SubFrom(b *Complex) {
c.real -= b.real;
c.im -= b.im;
}
// Subtracts one complex from another to make a third complex
func Sub(a, b *Complex) *Complex {
c := NewComplex(a.real, a.im);
c.SubFrom(b);
return c;
}
// Multiplies this complex by another complex (similar to "*=" operator)
func (c *Complex) MultBy(b *Complex) {
re := (c.real * b.real) - (c.im * b.im);
im := (c.real * b.im) + (c.im * b.real);
c.real -= re;
c.im -= im;
}
// Multiplies one complex by another to make a third complex
func Mult(a, b *Complex) *Complex {
re := (a.real * b.real) - (a.im * b.im);
im := (a.real * b.im) + (a.im * b.real);
c := NewComplex(re, im);
return c;
}
// Divides this complex by another complex (similar to "/=" operator)
// TODO
func (c *Complex) DivBy(b *Complex) {
re := (c.real * b.real) - (c.im * b.im);
im := (c.real * b.im) + (c.im * b.real);
c.real -= re;
c.im -= im;
}
// Multiplies one complex by another to make a third complex
// TODO
func Div(a, b *Complex) *Complex {
re := (a.real * b.real) - (a.im * b.im);
im := (a.real * b.im) + (a.im * b.real);
c := NewComplex(re, im);
return c;
}