A Discrete-Event Network Simulator
API
spectrum-value.cc
Go to the documentation of this file.
1 /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2 
3 /*
4  * Copyright (c) 2009 CTTC
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License version 2 as
8  * published by the Free Software Foundation;
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with this program; if not, write to the Free Software
17  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18  *
19  * Author: Nicola Baldo <nbaldo@cttc.es>
20  */
21 
22 #include <ns3/spectrum-value.h>
23 #include <ns3/math.h>
24 #include <ns3/log.h>
25 
26 namespace ns3 {
27 
28 NS_LOG_COMPONENT_DEFINE ("SpectrumValue");
29 
31 {
32 }
33 
35  : m_spectrumModel (sof),
36  m_values (sof->GetNumBands ())
37 {
38 
39 }
40 
41 double&
43 {
44  return m_values.at (index);
45 }
46 
47 const double&
48 SpectrumValue:: operator[] (size_t index) const
49 {
50  return m_values.at (index);
51 }
52 
53 
56 {
57  return m_spectrumModel->GetUid ();
58 }
59 
60 
63 {
64  return m_spectrumModel;
65 }
66 
67 
68 Values::const_iterator
70 {
71  return m_values.begin ();
72 }
73 
74 Values::const_iterator
76 {
77  return m_values.end ();
78 }
79 
80 
81 Values::iterator
83 {
84  return m_values.begin ();
85 }
86 
87 Values::iterator
89 {
90  return m_values.end ();
91 }
92 
93 Bands::const_iterator
95 {
96  return m_spectrumModel->Begin ();
97 }
98 
99 Bands::const_iterator
101 {
102  return m_spectrumModel->End ();
103 }
104 
105 
106 void
108 {
109  Values::iterator it1 = m_values.begin ();
110  Values::const_iterator it2 = x.m_values.begin ();
111 
113 
114  while (it1 != m_values.end ())
115  {
116  NS_ASSERT ( it2 != x.m_values.end ());
117  *it1 += *it2;
118  ++it1;
119  ++it2;
120  }
121 }
122 
123 
124 void
126 {
127  Values::iterator it1 = m_values.begin ();
128 
129  while (it1 != m_values.end ())
130  {
131  *it1 += s;
132  ++it1;
133  }
134 }
135 
136 
137 
138 void
140 {
141  Values::iterator it1 = m_values.begin ();
142  Values::const_iterator it2 = x.m_values.begin ();
143 
145 
146  while (it1 != m_values.end ())
147  {
148  NS_ASSERT ( it2 != x.m_values.end ());
149  *it1 -= *it2;
150  ++it1;
151  ++it2;
152  }
153 }
154 
155 
156 void
158 {
159  Add (-s);
160 }
161 
162 
163 
164 void
166 {
167  Values::iterator it1 = m_values.begin ();
168  Values::const_iterator it2 = x.m_values.begin ();
169 
171 
172  while (it1 != m_values.end ())
173  {
174  NS_ASSERT ( it2 != x.m_values.end ());
175  *it1 *= *it2;
176  ++it1;
177  ++it2;
178  }
179 }
180 
181 
182 void
184 {
185  Values::iterator it1 = m_values.begin ();
186 
187  while (it1 != m_values.end ())
188  {
189  *it1 *= s;
190  ++it1;
191  }
192 }
193 
194 
195 
196 
197 void
199 {
200  Values::iterator it1 = m_values.begin ();
201  Values::const_iterator it2 = x.m_values.begin ();
202 
204 
205  while (it1 != m_values.end ())
206  {
207  NS_ASSERT ( it2 != x.m_values.end ());
208  *it1 /= *it2;
209  ++it1;
210  ++it2;
211  }
212 }
213 
214 
215 void
217 {
218  NS_LOG_FUNCTION (this << s);
219  Values::iterator it1 = m_values.begin ();
220 
221  while (it1 != m_values.end ())
222  {
223  *it1 /= s;
224  ++it1;
225  }
226 }
227 
228 
229 
230 
231 void
233 {
234  Values::iterator it1 = m_values.begin ();
235 
236  while (it1 != m_values.end ())
237  {
238  *it1 = -(*it1);
239  ++it1;
240  }
241 }
242 
243 
244 void
246 {
247  int i = 0;
248  while (i < (int) m_values.size () - n)
249  {
250  m_values.at (i) = m_values.at (i + n);
251  i++;
252  }
253  while (i < (int)m_values.size ())
254  {
255  m_values.at (i) = 0;
256  i++;
257  }
258 }
259 
260 
261 void
263 {
264  int i = m_values.size () - 1;
265  while (i - n >= 0)
266  {
267  m_values.at (i) = m_values.at (i - n);
268  i = i - 1;
269  }
270  while (i >= 0)
271  {
272  m_values.at (i) = 0;
273  --i;
274  }
275 }
276 
277 
278 
279 void
280 SpectrumValue::Pow (double exp)
281 {
282  NS_LOG_FUNCTION (this << exp);
283  Values::iterator it1 = m_values.begin ();
284 
285  while (it1 != m_values.end ())
286  {
287  *it1 = std::pow (*it1, exp);
288  ++it1;
289  }
290 }
291 
292 
293 void
294 SpectrumValue::Exp (double base)
295 {
296  NS_LOG_FUNCTION (this << base);
297  Values::iterator it1 = m_values.begin ();
298 
299  while (it1 != m_values.end ())
300  {
301  *it1 = std::pow (base, *it1);
302  ++it1;
303  }
304 }
305 
306 
307 void
309 {
310  NS_LOG_FUNCTION (this);
311  Values::iterator it1 = m_values.begin ();
312 
313  while (it1 != m_values.end ())
314  {
315  *it1 = std::log10 (*it1);
316  ++it1;
317  }
318 }
319 
320 void
322 {
323  NS_LOG_FUNCTION (this);
324  Values::iterator it1 = m_values.begin ();
325 
326  while (it1 != m_values.end ())
327  {
328  *it1 = log2 (*it1);
329  ++it1;
330  }
331 }
332 
333 
334 void
336 {
337  NS_LOG_FUNCTION (this);
338  Values::iterator it1 = m_values.begin ();
339 
340  while (it1 != m_values.end ())
341  {
342  *it1 = std::log (*it1);
343  ++it1;
344  }
345 }
346 
347 double
349 {
350  double s = 0;
351  Values::const_iterator it1 = x.ConstValuesBegin ();
352  while (it1 != x.ConstValuesEnd ())
353  {
354  s += (*it1) * (*it1);
355  ++it1;
356  }
357  return std::sqrt (s);
358 }
359 
360 
361 double
363 {
364  double s = 0;
365  Values::const_iterator it1 = x.ConstValuesBegin ();
366  while (it1 != x.ConstValuesEnd ())
367  {
368  s += (*it1);
369  ++it1;
370  }
371  return s;
372 }
373 
374 
375 
376 double
378 {
379  double s = 0;
380  Values::const_iterator it1 = x.ConstValuesBegin ();
381  while (it1 != x.ConstValuesEnd ())
382  {
383  s *= (*it1);
384  ++it1;
385  }
386  return s;
387 }
388 
389 double
391 {
392  double i = 0;
393  Values::const_iterator vit = arg.ConstValuesBegin ();
394  Bands::const_iterator bit = arg.ConstBandsBegin ();
395  while (vit != arg.ConstValuesEnd ())
396  {
397  NS_ASSERT (bit != arg.ConstBandsEnd ());
398  i += (*vit) * (bit->fh - bit->fl);
399  ++vit;
400  ++bit;
401  }
402  NS_ASSERT (bit == arg.ConstBandsEnd ());
403  return i;
404 }
405 
406 
407 
408 Ptr<SpectrumValue>
410 {
411  Ptr<SpectrumValue> p = Create<SpectrumValue> (m_spectrumModel);
412  *p = *this;
413  return p;
414 
415  // return Copy<SpectrumValue> (*this)
416 }
417 
418 
419 std::ostream&
420 operator << (std::ostream& os, const SpectrumValue& pvf)
421 {
422  Values::const_iterator it1 = pvf.ConstValuesBegin ();
423  while (it1 != pvf.ConstValuesEnd ())
424  {
425  os << *it1 << " ";
426  ++it1;
427  }
428  os << std::endl;
429  return os;
430 }
431 
432 
433 
434 SpectrumValue
435 operator+ (const SpectrumValue& lhs, const SpectrumValue& rhs)
436 {
437  SpectrumValue res = lhs;
438  res.Add (rhs);
439  return res;
440 }
441 
442 
443 SpectrumValue
444 operator+ (const SpectrumValue& lhs, double rhs)
445 {
446  SpectrumValue res = lhs;
447  res.Add (rhs);
448  return res;
449 }
450 
451 
452 SpectrumValue
453 operator+ (double lhs, const SpectrumValue& rhs)
454 {
455  SpectrumValue res = rhs;
456  res.Add (lhs);
457  return res;
458 }
459 
460 
461 SpectrumValue
462 operator- (const SpectrumValue& lhs, const SpectrumValue& rhs)
463 {
464  SpectrumValue res = rhs;
465  res.ChangeSign ();
466  res.Add (lhs);
467  return res;
468 }
469 
470 
471 
472 SpectrumValue
473 operator- (const SpectrumValue& lhs, double rhs)
474 {
475  SpectrumValue res = lhs;
476  res.Subtract (rhs);
477  return res;
478 }
479 
480 
481 SpectrumValue
482 operator- (double lhs, const SpectrumValue& rhs)
483 {
484  SpectrumValue res = rhs;
485  res.Subtract (lhs);
486  return res;
487 }
488 
489 SpectrumValue
490 operator* (const SpectrumValue& lhs, const SpectrumValue& rhs)
491 {
492  SpectrumValue res = lhs;
493  res.Multiply (rhs);
494  return res;
495 }
496 
497 
498 SpectrumValue
499 operator* (const SpectrumValue& lhs, double rhs)
500 {
501  SpectrumValue res = lhs;
502  res.Multiply (rhs);
503  return res;
504 }
505 
506 
507 SpectrumValue
508 operator* (double lhs, const SpectrumValue& rhs)
509 {
510  SpectrumValue res = rhs;
511  res.Multiply (lhs);
512  return res;
513 }
514 
515 
516 SpectrumValue
517 operator/ (const SpectrumValue& lhs, const SpectrumValue& rhs)
518 {
519  SpectrumValue res = lhs;
520  res.Divide (rhs);
521  return res;
522 }
523 
524 
525 SpectrumValue
526 operator/ (const SpectrumValue& lhs, double rhs)
527 {
528  SpectrumValue res = lhs;
529  res.Divide (rhs);
530  return res;
531 }
532 
533 
534 SpectrumValue
535 operator/ (double lhs, const SpectrumValue& rhs)
536 {
537  SpectrumValue res = rhs;
538  res.Divide (lhs);
539  return res;
540 }
541 
542 
543 SpectrumValue
545 {
546  return rhs;
547 }
548 
549 SpectrumValue
551 {
552  SpectrumValue res = rhs;
553  res.ChangeSign ();
554  return res;
555 }
556 
557 
558 SpectrumValue
559 Pow (double lhs, const SpectrumValue& rhs)
560 {
561  SpectrumValue res = rhs;
562  res.Exp (lhs);
563  return res;
564 }
565 
566 
567 SpectrumValue
568 Pow (const SpectrumValue& lhs, double rhs)
569 {
570  SpectrumValue res = lhs;
571  res.Pow (rhs);
572  return res;
573 }
574 
575 
576 SpectrumValue
577 Log10 (const SpectrumValue& arg)
578 {
579  SpectrumValue res = arg;
580  res.Log10 ();
581  return res;
582 }
583 
584 SpectrumValue
585 Log2 (const SpectrumValue& arg)
586 {
587  SpectrumValue res = arg;
588  res.Log2 ();
589  return res;
590 }
591 
592 SpectrumValue
593 Log (const SpectrumValue& arg)
594 {
595  SpectrumValue res = arg;
596  res.Log ();
597  return res;
598 }
599 
600 SpectrumValue&
602 {
603  Add (rhs);
604  return *this;
605 }
606 
609 {
610  Subtract (rhs);
611  return *this;
612 }
613 
616 {
617  Multiply (rhs);
618  return *this;
619 }
620 
623 {
624  Divide (rhs);
625  return *this;
626 }
627 
628 
631 {
632  Add (rhs);
633  return *this;
634 }
635 
638 {
639  Subtract (rhs);
640  return *this;
641 }
642 
645 {
646  Multiply (rhs);
647  return *this;
648 }
649 
652 {
653  Divide (rhs);
654  return *this;
655 }
656 
657 
660 {
661  Values::iterator it1 = m_values.begin ();
662 
663  while (it1 != m_values.end ())
664  {
665  *it1 = rhs;
666  ++it1;
667  }
668  return *this;
669 }
670 
671 
672 
675 {
676  SpectrumValue res = *this;
677  res.ShiftLeft (n);
678  return res;
679 }
680 
683 {
684  SpectrumValue res = *this;
685  res.ShiftRight (n);
686  return res;
687 }
688 
689 
690 
691 
692 } // namespace ns3
693 
Values::const_iterator ConstValuesEnd() const
SpectrumValue & operator*=(const SpectrumValue &rhs)
Multiply *this by the Right Hand Side of the operator, component by component.
#define NS_LOG_FUNCTION(parameters)
If log level LOG_FUNCTION is enabled, this macro will output all input parameters separated by "...
uint32_t SpectrumModelUid_t
SpectrumValue & operator/=(const SpectrumValue &rhs)
Divide *this by the Right Hand Side of the operator, component by component.
double Integral(const SpectrumValue &arg)
int64x64_t operator+(const int64x64_t &lhs)
Unary plus operator.
Definition: int64x64-128.h:410
void Subtract(const SpectrumValue &x)
SpectrumValue operator>>(int n) const
right shift operator
SpectrumValue & operator+=(const SpectrumValue &rhs)
Add the Right Hand Side of the operator to *this, component by component.
SpectrumModelUid_t GetSpectrumModelUid() const
#define NS_ASSERT(condition)
At runtime, in debugging builds, if this condition is not true, the program prints the source file...
Definition: assert.h:67
int64x64_t operator-(const int64x64_t &lhs)
Unary negation operator (change sign operator).
Definition: int64x64-128.h:418
#define NS_LOG_COMPONENT_DEFINE(name)
Define a Log component with a specific name.
Definition: log.h:201
Values::iterator ValuesEnd()
friend SpectrumValue Log(const SpectrumValue &arg)
void Divide(const SpectrumValue &x)
void Multiply(const SpectrumValue &x)
Bands::const_iterator End() const
friend SpectrumValue Log10(const SpectrumValue &arg)
int64x64_t operator*(const int64x64_t &lhs, const int64x64_t &rhs)
Multiplication operator.
Definition: int64x64.h:108
SpectrumValue & operator-=(const SpectrumValue &rhs)
Subtract the Right Hand Side of the operator from *this, component by component.
Bands::const_iterator ConstBandsEnd() const
SpectrumModelUid_t GetUid() const
SpectrumValue operator<<(int n) const
left shift operator
Ptr< const SpectrumModel > GetSpectrumModel() const
friend SpectrumValue Pow(const SpectrumValue &lhs, double rhs)
void Add(const SpectrumValue &x)
SpectrumValue Pow(double lhs, const SpectrumValue &rhs)
Ptr< const SpectrumModel > m_spectrumModel
void ShiftRight(int n)
int64x64_t operator/(const int64x64_t &lhs, const int64x64_t &rhs)
Division operator.
Definition: int64x64.h:119
Bands::const_iterator ConstBandsBegin() const
std::ostream & operator<<(std::ostream &os, const Angles &a)
print a struct Angles to output
Definition: angles.cc:42
Every class exported by the ns3 library is enclosed in the ns3 namespace.
SpectrumValue Log10(const SpectrumValue &arg)
Values m_values
Set of values which implement the codomain of the functions in the Function Space defined by Spectrum...
friend SpectrumValue Log2(const SpectrumValue &arg)
double & operator[](size_t index)
Access value at given frequency index.
Values::iterator ValuesBegin()
SpectrumValue Log(const SpectrumValue &arg)
double Norm(const SpectrumValue &x)
SpectrumValue & operator=(double rhs)
Assign each component of *this to the value of the Right Hand Side of the operator.
Bands::const_iterator Begin() const
double Sum(const SpectrumValue &x)
Ptr< SpectrumValue > Copy() const
Values::const_iterator ConstValuesBegin() const
SpectrumValue Log2(const SpectrumValue &arg)
Set of values corresponding to a given SpectrumModel.
void Exp(double base)
double Prod(const SpectrumValue &x)