-
Notifications
You must be signed in to change notification settings - Fork 2
/
mathslib.hoc
94 lines (75 loc) · 1.88 KB
/
mathslib.hoc
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
// mathslib.hoc
// Collection of miscellaneous maths functions
// Andrew Davison, The Babraham Institute, 2000.
// =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
// mod(x,y)
// Returns a number between 0 and y-
// e.g. mod(7,4) returns 3
func mod() { local x // 2 args - $1 mod $2
x = $1
while (x < 0) {
x = x+$2
}
while (x >= $2) {
x = x-$2
}
return x
}
// =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
// arraymax(M) and arraymin(M)
// Return maximum and minimum elements of a matrix object
objref mymaths_work
mymaths_work = new Vector()
func arraymax() { local i, max // arg - matrix object
max = -1e10
for i = 0,$o1.nrow-1 {
mymaths_work = $o1.getrow(i)
if (mymaths_work.max() > max) { max = mymaths_work.max() }
}
return max
}
func arraymin() { local i, min // arg - matrix object
min = 1e10
for i = 0,$o1.nrow-1 {
mymaths_work = $o1.getrow(i)
if (mymaths_work.min() < min) { min = mymaths_work.min() }
}
return min
}
// =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
// nint(x)
// Returns nearest integer
// e.g. nint(3.6) returns 4
func nint() {
if ( abs($1 - int($1)) <= 0.5) {
return int($1)
} else {
if ($1 < 0) { return int($1)-1 }
if ($1 >= 0) { return int($1)+1 }
}
}
// =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
// ceil(x)
// Round towards plus infinity.
// e.g. nint(3.1) returns 4
func ceil() {
if ( abs($1 - int($1)) == 0.0) {
return $1
} else {
if ($1 < 0) { return int($1) }
if ($1 > 0) { return int($1)+1 }
}
}
// =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
// invabs(x)
// Returns absolute value of the inverse of the argument
// e.g. invabs(-2) returns 0.5
func invabs() {
if ($1 == 0) {
print "Error in invabs(): divide by zero"
return 1e3
} else {
return abs(1.0/$1)
}
}
// =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=