summaryrefslogtreecommitdiff
path: root/src/de/lmu/ifi/dbs/elki/math/SinCosTable.java
blob: 6cabb172d35897e2b3f5c2b17574d22825f2cf99 (plain)
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
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
package de.lmu.ifi.dbs.elki.math;

/*
 This file is part of ELKI:
 Environment for Developing KDD-Applications Supported by Index-Structures

 Copyright (C) 2013
 Ludwig-Maximilians-Universität München
 Lehr- und Forschungseinheit für Datenbanksysteme
 ELKI Development Team

 This program is free software: you can redistribute it and/or modify
 it under the terms of the GNU Affero General Public License as published by
 the Free Software Foundation, either version 3 of the License, or
 (at your option) any later version.

 This program is distributed in the hope that it will be useful,
 but WITHOUT ANY WARRANTY; without even the implied warranty of
 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 GNU Affero General Public License for more details.

 You should have received a copy of the GNU Affero General Public License
 along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

/**
 * Class to precompute / cache Sinus and Cosinus values.
 * 
 * Note that the functions use integer offsets, not radians.
 * 
 * TODO: add an interpolation function.
 * 
 * TODO: add caching
 * 
 * @author Erich Schubert
 */
public abstract class SinCosTable {
  /**
   * Number of steps.
   */
  protected final int steps;

  /**
   * Constructor.
   * 
   * @param steps Number of steps (ideally, {@code steps % 4 = 0}!)
   */
  private SinCosTable(final int steps) {
    this.steps = steps;
  }

  /**
   * Get Cosine by step value.
   * 
   * @param step Step value
   * @return Cosinus
   */
  public abstract double cos(int step);

  /**
   * Get Sinus by step value.
   * 
   * @param step Step value
   * @return Sinus
   */
  public abstract double sin(int step);

  /**
   * Table that can't exploit much symmetry, because the steps are not divisible
   * by 2.
   * 
   * @author Erich Schubert
   * 
   * @apiviz.exclude
   */
  private static class FullTable extends SinCosTable {
    /**
     * Data store
     */
    private final double[] costable;

    /**
     * Data store
     */
    private final double[] sintable;

    /**
     * Constructor for tables with
     * 
     * @param steps
     */
    public FullTable(int steps) {
      super(steps);
      final double radstep = Math.toRadians(360. / steps);
      this.costable = new double[steps];
      this.sintable = new double[steps];
      double ang = 0.;
      for (int i = 0; i < steps; i++, ang += radstep) {
        this.costable[i] = Math.cos(ang);
        this.sintable[i] = MathUtil.cosToSin(ang, this.costable[i]);
      }
    }

    /**
     * Get Cosine by step value.
     * 
     * @param step Step value
     * @return Cosinus
     */
    @Override
    public double cos(int step) {
      step = Math.abs(step) % steps;
      return costable[step];
    }

    /**
     * Get Sinus by step value.
     * 
     * @param step Step value
     * @return Sinus
     */
    @Override
    public double sin(int step) {
      step = step % steps;
      if (step < 0) {
        step += steps;
      }
      return sintable[step];
    }
  }

  /**
   * Table that exploits just one symmetry, as the number of steps is divisible
   * by two.
   * 
   * @author Erich Schubert
   * 
   * @apiviz.exclude
   */
  private static class HalfTable extends SinCosTable {
    /**
     * Number of steps div 2
     */
    private final int halfsteps;

    /**
     * Data store
     */
    private final double[] costable;

    /**
     * Data store
     */
    private final double[] sintable;

    /**
     * Constructor for tables with
     * 
     * @param steps
     */
    public HalfTable(int steps) {
      super(steps);
      this.halfsteps = steps >> 1;
      final double radstep = Math.toRadians(360. / steps);
      this.costable = new double[halfsteps + 1];
      this.sintable = new double[halfsteps + 1];
      double ang = 0.;
      for (int i = 0; i < halfsteps + 1; i++, ang += radstep) {
        this.costable[i] = Math.cos(ang);
        this.sintable[i] = Math.sin(ang);
      }
    }

    /**
     * Get Cosine by step value.
     * 
     * @param step Step value
     * @return Cosinus
     */
    @Override
    public double cos(int step) {
      // Tabularizing cosine is a bit more straightforward than sine
      // As we can just drop the sign here:
      step = Math.abs(step) % steps;
      if (step < costable.length) {
        return costable[step];
      }
      // Symmetry at PI:
      return costable[steps - step];
    }

    /**
     * Get Sinus by step value.
     * 
     * @param step Step value
     * @return Sinus
     */
    @Override
    public double sin(int step) {
      step = step % steps;
      if (step < 0) {
        step += steps;
      }
      if (step < sintable.length) {
        return sintable[step];
      }
      // Anti symmetry at PI:
      return -sintable[steps - step];
    }
  }

  /**
   * Table that exploits both symmetries, as the number of steps is divisible by
   * four.
   * 
   * @author Erich Schubert
   * 
   * @apiviz.exclude
   */
  private static class QuarterTable extends SinCosTable {
    /**
     * Number of steps div 4
     */
    private final int quarsteps;

    /**
     * Number of steps div 2
     */
    private final int halfsteps;

    /**
     * Data store
     */
    private final double[] costable;

    /**
     * Constructor for tables with
     * 
     * @param steps
     */
    public QuarterTable(int steps) {
      super(steps);
      this.halfsteps = steps >> 1;
      this.quarsteps = steps >> 2;
      final double radstep = Math.toRadians(360. / steps);
      this.costable = new double[quarsteps + 1];
      double ang = 0.;
      for (int i = 0; i < quarsteps + 1; i++, ang += radstep) {
        this.costable[i] = Math.cos(ang);
      }
    }

    /**
     * Get Cosine by step value.
     * 
     * @param step Step value
     * @return Cosinus
     */
    @Override
    public double cos(int step) {
      // Tabularizing cosine is a bit more straightforward than sine
      // As we can just drop the sign here:
      step = Math.abs(step) % steps;
      if (step < costable.length) {
        return costable[step];
      }
      // Symmetry at PI:
      if (step > halfsteps) {
        step = steps - step;
        if (step < costable.length) {
          return costable[step];
        }
      }
      // Inverse symmetry at PI/2:
      step = halfsteps - step;
      return -costable[step];
    }

    /**
     * Get Sinus by step value.
     * 
     * @param step Step value
     * @return Sinus
     */
    @Override
    public double sin(int step) {
      return -cos(step + quarsteps);
    }
  }

  /**
   * Make a table for the given number of steps.
   * 
   * For step numbers divisible by 4, an optimized implementation will be used.
   * 
   * @param steps Number of steps
   * @return Table
   */
  public static SinCosTable make(int steps) {
    if ((steps & 0x3) == 0) {
      return new QuarterTable(steps);
    }
    if ((steps & 0x1) == 0) {
      return new HalfTable(steps);
    }
    return new FullTable(steps);
  }
}