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