-
Notifications
You must be signed in to change notification settings - Fork 18
Expand file tree
/
Copy pathSeq.java
More file actions
118 lines (100 loc) · 3.34 KB
/
Seq.java
File metadata and controls
118 lines (100 loc) · 3.34 KB
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
package com.arrayfire;
import static com.arrayfire.Util.*;
import static com.arrayfire.ArrayFire.range;
import static com.arrayfire.ArrayFire.add;
import static com.arrayfire.ArrayFire.mul;
public class Seq extends AFLibLoader {
// Start position of the sequence
private double begin;
// End position of the sequence (inclusive)
private double end;
// Step size between sequence values
private double step;
private long size;
protected boolean gfor; // always false until implemented
public Seq() {
this(0);
}
public Seq(double length) {
if (length < 0) {
init(0, length, 1);
} else {
init(0, length - 1, 1);
}
}
public Seq(double begin, double end) throws Exception {
this(begin, end, 1);
}
public Seq(double begin, double end, double step) throws Exception {
if (step == 0 && begin != end) { // Span
throw new IllegalArgumentException("Invalid step size.");
}
if ((signbit(end) == signbit(begin)) && (signbit(end - begin) != signbit(step))) {
throw new Exception("Sequence is invalid");
}
init(begin, end, step);
}
public Seq(Seq other) {
set(other);
}
protected static Seq span() {
try {
return new Seq(1, 1, 0); // will never throw
} catch (Exception ex) {
return null;
}
}
public void set(Seq other) {
init(other.begin, other.end, other.step);
}
public Seq negate() throws Exception {
return new Seq(-begin, -end, -step);
}
public Seq addOffset(double offset) throws Exception {
return new Seq(begin + offset, end + offset, step);
}
public Seq subtractOffset(double offset) throws Exception {
return new Seq(begin - offset, end - offset, step);
}
public Seq spaceBy(double factor) throws Exception {
return new Seq(begin * factor, end * factor, step * factor);
}
public Array toArray() throws Exception {
double diff = end - begin;
int len = (int)((diff + Math.abs(step) * (signbit(diff) == 0 ? 1 : -1)) / step);
Array tmp = range(len);
Array res = new Array();
mul(res, (float) step, tmp);
add(res, (float)begin, res);
return res;
}
private void init(double begin, double end, double step) {
this.begin = begin;
this.end = end;
this.step = step;
this.gfor = false;
this.size = (long)(step != 0 ? Math.abs((end - begin) / step) + 1 : 0);
}
@Override
public boolean equals(Object obj) {
if (obj == this) {
return true;
}
if (obj == null || obj.getClass() != this.getClass()) {
return false;
}
Seq seq = (Seq) obj;
return this.begin == seq.begin && this.end == seq.end &&
this.step == seq.step && size == seq.size;
}
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + (int) ((int)begin ^ ((int)begin >>> 32));
result = prime * result + (int) ((int) end ^ ((int) end >>> 32));
result = prime * result + (int) ((int) step ^ ((int) step >>> 32));
result = prime * result + (int) ((int) size ^ ((int) size >>> 32));
return result;
}
}