1 /*
2 Copyright � 1999 CERN - European Organization for Nuclear Research.
3 Permission to use, copy, modify, distribute and sell this software and its documentation for any purpose
4 is hereby granted without fee, provided that the above copyright notice appear in all copies and
5 that both that copyright notice and this permission notice appear in supporting documentation.
6 CERN makes no representations about the suitability of this software for any purpose.
7 It is provided "as is" without expressed or implied warranty.
8 */
9 package cern.jet.random;
10 
11 import cern.jet.random.engine.RandomEngine;
12 /**
13  * Mean-square BreitWigner distribution; See the <A HREF="http://www.cern.ch/RD11/rkb/AN16pp/node23.html#SECTION000230000000000000000"> math definition</A>.
14  * <p>
15  * Instance methods operate on a user supplied uniform random number generator; they are unsynchronized.
16  * <dt>
17  * Static methods operate on a default uniform random number generator; they are synchronized.
18  * <p>
19  * <b>Implementation:</b> This is a port of <A HREF="http://wwwinfo.cern.ch/asd/lhc++/clhep/manual/RefGuide/Random/RandBreitWigner.html">RandBreitWigner</A> used in <A HREF="http://wwwinfo.cern.ch/asd/lhc++/clhep">CLHEP 1.4.0</A> (C++).
20  *
21  * @author wolfgang.hoschek@cern.ch
22  * @version 1.0, 09/24/99
23  */
24 public class BreitWignerMeanSquare extends BreitWigner {
25 	protected Uniform uniform; // helper
26 
27 	// The uniform random number generated shared by all <b>static</b> methods.
28 	protected static BreitWigner shared = new BreitWignerMeanSquare(1.0,0.2,1.0,makeDefaultGenerator());
29 /**
30  * Constructs a mean-squared BreitWigner distribution.
31  * @param cut </tt>cut==Double.NEGATIVE_INFINITY</tt> indicates "don't cut".
32  */
BreitWignerMeanSquare(double mean, double gamma, double cut, RandomEngine randomGenerator)33 public BreitWignerMeanSquare(double mean, double gamma, double cut, RandomEngine randomGenerator) {
34 	super(mean,gamma,cut,randomGenerator);
35 	this.uniform = new Uniform(randomGenerator);
36 }
37 /**
38  * Returns a deep copy of the receiver; the copy will produce identical sequences.
39  * After this call has returned, the copy and the receiver have equal but separate state.
40  *
41  * @return a copy of the receiver.
42  */
clone()43 public Object clone() {
44 	BreitWignerMeanSquare copy = (BreitWignerMeanSquare) super.clone();
45 	if (this.uniform != null) copy.uniform = new Uniform(copy.randomGenerator);
46 	return copy;
47 }
48 /**
49  * Returns a mean-squared random number from the distribution; bypasses the internal state.
50  * @param cut </tt>cut==Double.NEGATIVE_INFINITY</tt> indicates "don't cut".
51  */
nextDouble(double mean,double gamma,double cut)52 public double nextDouble(double mean,double gamma,double cut) {
53 	if (gamma == 0.0) return mean;
54 	if (cut==Double.NEGATIVE_INFINITY) { // don't cut
55 		double val = Math.atan(-mean/gamma);
56 		double rval = this.uniform.nextDoubleFromTo(val, Math.PI/2.0);
57 		double displ = gamma*Math.tan(rval);
58 		return Math.sqrt(mean*mean + mean*displ);
59 	}
60 	else {
61 		double tmp = Math.max(0.0,mean-cut);
62 		double lower = Math.atan( (tmp*tmp-mean*mean)/(mean*gamma) );
63 		double upper = Math.atan( ((mean+cut)*(mean+cut)-mean*mean)/(mean*gamma) );
64 		double rval = this.uniform.nextDoubleFromTo(lower, upper);
65 
66 		double displ = gamma*Math.tan(rval);
67 		return Math.sqrt(Math.max(0.0, mean*mean + mean*displ));
68 	}
69 }
70 /**
71  * Returns a random number from the distribution.
72  * @param cut </tt>cut==Double.NEGATIVE_INFINITY</tt> indicates "don't cut".
73  */
staticNextDouble(double mean,double gamma,double cut)74 public static double staticNextDouble(double mean,double gamma,double cut) {
75 	synchronized (shared) {
76 		return shared.nextDouble(mean,gamma,cut);
77 	}
78 }
79 /**
80  * Sets the uniform random number generated shared by all <b>static</b> methods.
81  * @param randomGenerator the new uniform random number generator to be shared.
82  */
xstaticSetRandomGenerator(RandomEngine randomGenerator)83 private static void xstaticSetRandomGenerator(RandomEngine randomGenerator) {
84 	synchronized (shared) {
85 		shared.setRandomGenerator(randomGenerator);
86 	}
87 }
88 }
89