comparison src/samer/maths/random/BinaryVec.java @ 0:bf79fb79ee13

Initial Mercurial check in.
author samer
date Tue, 17 Jan 2012 17:50:20 +0000
parents
children
comparison
equal deleted inserted replaced
-1:000000000000 0:bf79fb79ee13
1 /*
2 * Copyright (c) 2000, Samer Abdallah, King's College London.
3 * All rights reserved.
4 *
5 * This software is provided AS iS and WITHOUT ANY WARRANTY;
6 * without even the implied warranty of MERCHANTABILITY or
7 * FITNESS FOR A PARTICULAR PURPOSE.
8 */
9
10 /* Similar to binary, but has different bit probabilities
11 for elements of vector
12 */
13
14 package samer.maths.random;
15 import samer.maths.*;
16 import samer.core.types.*;
17
18 public class BinaryVec extends BaseRandom
19 {
20 int n;
21 VVector probs;
22 double [] p;
23 boolean ownp;
24
25 public BinaryVec(int n, double pp) {
26 this(new VVector("probs",n));
27 ownp=true;
28 for (int i=0; i<n; i++) p[i]=pp;
29 }
30
31 public BinaryVec(VVector probs) {
32 this.n=probs.size();
33 this.probs=probs;
34 p=probs.array();
35 ownp=false;
36 }
37
38 public int getSize() { return n; }
39 public void dispose() { if (ownp) probs.dispose(); }
40 public double next() { return (rnd.nextDouble()<p[0]) ? 1.0 : 0.0; }
41 public void next(double [] x) {
42 int i, j;
43 for (i=0,j=0; i<x.length; i++,j++) {
44 if (j>=n) j=0;
45 x[i]=(rnd.nextDouble()<p[j]) ? 1.0 : 0.0;
46 }
47 }
48 }