-
Notifications
You must be signed in to change notification settings - Fork 0
/
conversions.go
192 lines (154 loc) · 5.21 KB
/
conversions.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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
// Package mathem is a sub-package of the gophysics library.
//
// Its porpuse is to make the most common Math calculations used for Physics.
//
// Some of the most basic operations are radiants to degrees (and viceversa) conversions, area calculations, summatory, ecc...
//
// Here is the documentation https://pkg.go.dev/github.com/Gabri432/gophysics/mathem
package mathem
import "math"
// It converts radiants to degrees
func RadToDeg(angleInRad float64) (degrees float64, measurementUnit string) {
return angleInRad * Radiant, "Deg"
}
// It converts degrees to radiants
func DegToRad(angleInDeg float64) (radiants float64, measurementUnit string) {
return angleInDeg / Radiant, "Rad"
}
// It calculates the circle area
func CircleArea(radius float64) (area float64, measurementUnit string) {
return Pi * radius * radius, "Squares"
}
// It calculates sphere volume
func SphereVolume(radius float64) (volume float64, measurementUnit string) {
return (4 / 3) * Pi * (radius * radius * radius), "Cubes"
}
// It calculates sine^2 of the angle in degrees
func SineSquare(angleInDeg float64) float64 {
return (1 - math.Cos(2*(angleInDeg/Radiant))) / 2
}
// It calculates cosine^2 of the angle in degrees
func CosineSquare(angleInDeg float64) float64 {
return 1 - SineSquare(angleInDeg)
}
//PowerAt calls the math.Pow method to return base**power
func PowerAt(base, power float64) float64 {
return math.Pow(base, power)
}
//Power10 calls the math.Pow10 method to return base*10**power
func Power10(base float64, power int) float64 {
return base * math.Pow10(power)
}
/*
Summatory function will calculate the user defined function from the starting to the ending integer value.
Each output will be added to the following one and then store into the resulting sum that will be returned.
Example:
import "github.com/Gabri432/gophysics/mathem"
func main() {
var oddNumberFunction = func(value int) float64 {
return float64(2*value+1) //2n+1
}
fmt.Println(mathem.Summatory(1, 3, oddNumberFunction)) // [2*(1)+1] + [2*(2)+1] + [2*(3)+1] = 3 + 5 + 7 = 15
}
*/
func Summatory(startingValue, endingValue int, function func(value int) float64) (sum float64) {
for i := startingValue; i <= endingValue; i++ {
sum += function(i)
}
return sum
}
/*
ProductOfSequence function will calculate the user defined function from the starting to the ending integer value.
Each output will be multiplied to the following one and then store into the resulting product that will be returned.
Example:
import "github.com/Gabri432/gophysics/mathem"
func main() {
var squareFunction = func(value int) float64 {
return float64(value*value) //n*n
}
fmt.Println(mathem.ProductOfSequence(1, 3, squareFunction)) // [1*1] * [2*2] * [3*3] = 1 * 4 * 9 = 36
}
*/
func ProductOfSequence(startingValue, endingValue int, function func(value int) float64) (product float64) {
product = 1
for i := startingValue; i <= endingValue; i++ {
product *= function(i)
}
return product
}
/*
Calculates the arithmetic mean of the user given values.
Example:
func main() {
mathem.ArithemticMean(1,2,3,4,6) // (1+2+3+4+6)/5 => 16/5 == 3.2
}
*/
func ArithmeticMean(values ...float64) float64 {
sum := 0.0
for i := 0; i < len(values); i++ {
sum += values[i]
}
return sum / float64(len(values))
}
/*
Calculates the geometric mean of the user given values.
Example:
func main() {
mathem.GeometricMean(1,2,3,4) // (1*2*3*4)^(1/4) == 2.213
}
*/
func GeometricMean(values ...float64) float64 {
product := 1.0
for i := 0; i < len(values); i++ {
product *= values[i]
}
return math.Pow(product, float64(1.0/len(values)))
}
/*
Calculates the Harmonic mean of the user given values.
Example:
func main() {
mathem.HarmonicMean(1,2,3,4) // [4/(1/1 + 1/2 + 1/3 + 1/4)] == 1.92
}
*/
func HarmonicMean(values ...float64) float64 {
n := len(values)
var div float64
for i := 0; i < n; i++ {
div += 1 / values[i]
}
return float64(n) / div
}
/*
Variance is the ratio between the square deviations of each value from the mean and the number of values.
Example:
func main() {
fmt.Println(mathem.Variance(2, 4, 4, 4, 5, 5, 7, 9)) // returns 4
// STEP 1: Arithmetic mean = 5;
// STEP 2: Sum of the square deviations =
// = [(2-5)^2 + (4-5)^2 + (4-5)^2 + (4-5)^2 + (5-5)^2 + (5-5)^2 + (7-5)^2 + (9-5)^2] = 40;
// STEP 3: Mean of those values = [(9 + 1 + 1 + 1 + 0 + 0 + 4 + 16) / 8] == 4
}
*/
func Variance(values ...float64) float64 {
arithMean := ArithmeticMean(values...)
var squareDifference = func(index int) float64 {
return (values[index] - arithMean) * (values[index] - arithMean)
}
return Summatory(0, len(values)-1, squareDifference) / float64(len(values))
}
/*
StandardDeviation is the square root of the Variance.
Example:
func main() {
fmt.Println(mathem.StandardDeviation(2, 4, 4, 4, 5, 5, 7, 9)) // returns 2
// STEP 1: Arithmetic mean = 5;
// STEP 2: Sum of the square deviations =
// = [(2-5)^2 + (4-5)^2 + (4-5)^2 + (4-5)^2 + (5-5)^2 + (5-5)^2 + (7-5)^2 + (9-5)^2] = 40;
// STEP 3: Mean of those values = [(9 + 1 + 1 + 1 + 0 + 0 + 4 + 16) / 8] == 4;
// STEP 4: Square Root of Variance = 2;
}
*/
func StandardDeviation(values ...float64) float64 {
return math.Sqrt(Variance(values...))
}