Stokes.h
1//-*-C++-*-
2/***************************************************************************
3 *
4 * Copyright (C) 2003 by Willem van Straten
5 * Licensed under the Academic Free License version 2.1
6 *
7 ***************************************************************************/
8
9// espic/src/Stokes.h
10
11#ifndef __Stokes_H
12#define __Stokes_H
13
14#include "Vector.h"
15#include "Estimate.h"
16#include "random.h"
17
18#include <stdexcept>
19
20template <typename T>
21class Stokes : public Vector<4,T>
22{
23 public:
24
26 Stokes (T a = T(0.0), T b = T(0.0), T c = T(0.0), T d = T(0.0))
27 : Vector<4,T> (a,b,c,d) {}
28
30 template<typename U>
31 Stokes (const Vector<4,U>& v)
32 : Vector<4,T> (T(v[0]), T(v[1]), T(v[2]), T(v[3])) {}
33
35 template<typename U>
36 Stokes (T s, const Vector<3,U>& v)
37 : Vector<4,T> (s, v[0], v[1], v[2]) {}
38
39 template<typename U>
40 Stokes (const Stokes<U>& s)
41 : Vector<4,T> (s[0], s[1], s[2], s[3]) {}
42
43 template<typename U, typename Unary>
44 Stokes (const Stokes<U>& s, Unary f)
45 : Vector<4,T>( f(s[0]), f(s[1]), f(s[2]), f(s[3]) ) {}
46
48 T get_scalar () const { return this->x[0]; }
49
51 void set_scalar (T s) { this->x[0] = s; }
52
54 Vector<3,T> get_vector () const
55 { return Vector<3,T> (this->x[1], this->x[2], this->x[3]); }
56
58 template<typename U>
59 void set_vector (const Vector<3,U>& v)
60 { this->x[1]=v[0]; this->x[2]=v[1]; this->x[3]=v[2]; }
61
62 T sqr_vect () const { return normsq (get_vector()); }
63
64 T abs_vect () const { return sqrt (sqr_vect()); }
65
66 T invariant () const { return this->x[0]*this->x[0] - sqr_vect(); }
67
68};
69
70// useful method for generating random source polarization
71template <class T, class U>
72void random_value (Stokes<T>& val, U scale, float max_polarization = 1.0)
73{
74 // total intensity is always equal to scale
75 val[0] = scale;
76
77 // generate a random fractional polarization, from 0 to 1
78 T fraction_polarized;
79 random_value (fraction_polarized, 0.5);
80 fraction_polarized += 0.5;
81 fraction_polarized *= max_polarization;
82
83 unsigned i=0;
84
85 for (i=1; i<4; i++)
86 random_value (val[i], scale);
87
88 T modp = val.abs_vect();
89
90 scale *= fraction_polarized / modp;
91
92 for (i=1; i<4; i++)
93 val[i] *= scale;
94
95 if (val.invariant() < -1e-10)
96 throw std::runtime_error ("random_value (Stokes) invariant less than zero");
97}
98
99template <class T, class U>
100void random_vector (Stokes<T>& val, U scale)
101{
102 random_value (val, scale);
103}
104
105template<typename T>
106Estimate<T> invariant ( const Stokes< Estimate<T> >& stokes )
107{
108 Estimate<T> result = stokes.invariant();
109
110 // the value is underestimated due to noise
111 double bias = stokes[0].get_variance();
112 for (unsigned i=1; i<4; i++)
113 bias -= stokes[i].get_variance();
114
115 result.val -= bias;
116
117 // the variance is underestimated by Estimate<T>::operator * (x,x)
118 result.var *= 2;
119
120 return result;
121}
122
123template<typename T> struct DatumTraits< Stokes<T> >
124 : public DatumTraits< Vector<4,T> > {};
125
126#endif
T val
Definition Estimate.h:43
U var
Definition Estimate.h:45
U get_variance() const

Generated using doxygen 1.14.0