SNAP Library 6.0, User Reference  2020-12-09 16:24:20
SNAP, a general purpose, high performance system for analysis and manipulation of large networks
TMAGAffMtx Class Reference

#include <mag.h>

Public Member Functions

 TMAGAffMtx ()
 
 TMAGAffMtx (const int &Dim)
 
 TMAGAffMtx (const TFltV &SeedMatrix)
 
 TMAGAffMtx (const TMAGAffMtx &Kronecker)
 
TMAGAffMtxoperator= (const TMAGAffMtx &Kronecker)
 
bool operator== (const TMAGAffMtx &Kronecker) const
 
int GetPrimHashCd () const
 
int GetSecHashCd () const
 
int GetDim () const
 
int Len () const
 
bool Empty () const
 
bool IsProbMtx () const
 
TFltVGetMtx ()
 
const TFltVGetMtx () const
 
void SetMtx (const TFltV &ParamV)
 
void SetRndMtx (TRnd &Rnd, const int &PrmMtxDim=2, const double &MinProb=0.0)
 
void PutAllMtx (const double &Val)
 
void GenMtx (const int &Dim)
 
void SetEpsMtx (const double &Eps1, const double &Eps0, const int &Eps1Val=1, const int &Eps0Val=0)
 
void AddRndNoise (TRnd &Rnd, const double &SDev)
 
TStr GetMtxStr () const
 
const double & At (const int &Row, const int &Col) const
 
double & At (const int &Row, const int &Col)
 
const double & At (const int &ValN) const
 
double & At (const int &ValN)
 
double GetMtxSum () const
 
double GetRowSum (const int &RowId) const
 
double GetColSum (const int &ColId) const
 
double Normalize ()
 
void GetLLMtx (TMAGAffMtx &LLMtx)
 
void GetProbMtx (TMAGAffMtx &ProbMtx)
 
void Swap (TMAGAffMtx &Mtx)
 
void Dump (const TStr &MtxNm=TStr(), const bool &Sort=false) const
 

Static Public Member Functions

static double GetAvgAbsErr (const TMAGAffMtx &Mtx1, const TMAGAffMtx &Mtx2)
 
static double GetAvgFroErr (const TMAGAffMtx &Mtx1, const TMAGAffMtx &Mtx2)
 
static TMAGAffMtx GetMtx (TStr MatlabMtxStr)
 
static TMAGAffMtx GetRndMtx (TRnd &Rnd, const int &Dim=2, const double &MinProb=0.0)
 

Private Attributes

TInt MtxDim
 
TFltV SeedMtx
 

Static Private Attributes

static const double NInf = -DBL_MAX
 

Detailed Description

Definition at line 10 of file mag.h.

Constructor & Destructor Documentation

TMAGAffMtx::TMAGAffMtx ( )
inline

Definition at line 16 of file mag.h.

16 : MtxDim(-1), SeedMtx() { }
TFltV SeedMtx
Definition: mag.h:14
TInt MtxDim
Definition: mag.h:13
TMAGAffMtx::TMAGAffMtx ( const int &  Dim)
inline

Definition at line 17 of file mag.h.

17 : MtxDim(Dim), SeedMtx(Dim*Dim) { }
TFltV SeedMtx
Definition: mag.h:14
TInt MtxDim
Definition: mag.h:13
TMAGAffMtx::TMAGAffMtx ( const TFltV SeedMatrix)

Definition at line 14 of file mag.cpp.

14  : SeedMtx(SeedMatrix) {
15  MtxDim = (int) sqrt((double)SeedMatrix.Len());
17 }
#define IAssert(Cond)
Definition: bd.h:262
TFltV SeedMtx
Definition: mag.h:14
TSizeTy Len() const
Returns the number of elements in the vector.
Definition: ds.h:575
TInt MtxDim
Definition: mag.h:13
TMAGAffMtx::TMAGAffMtx ( const TMAGAffMtx Kronecker)
inline

Definition at line 19 of file mag.h.

19 : MtxDim(Kronecker.MtxDim), SeedMtx(Kronecker.SeedMtx) { }
TFltV SeedMtx
Definition: mag.h:14
TInt MtxDim
Definition: mag.h:13

Member Function Documentation

void TMAGAffMtx::AddRndNoise ( TRnd Rnd,
const double &  SDev 
)

Definition at line 52 of file mag.cpp.

52  {
53  Dump("before");
54  double NewVal;
55  int c =0;
56  for (int i = 0; i < Len(); i++) {
57  for(c = 0; ((NewVal = At(i)*Rnd.GetNrmDev(1, SDev, 0.8, 1.2)) < 0.01 || NewVal>0.99) && c <1000; c++) { }
58  if (c < 999) { At(i) = NewVal; } else { printf("XXXXX\n"); }
59  }
60  Dump("after");
61 }
void Dump(const TStr &MtxNm=TStr(), const bool &Sort=false) const
Definition: mag.cpp:128
int Len() const
Definition: mag.h:27
double GetNrmDev()
Definition: dt.cpp:63
const double & At(const int &Row, const int &Col) const
Definition: mag.h:41
const double& TMAGAffMtx::At ( const int &  Row,
const int &  Col 
) const
inline

Definition at line 41 of file mag.h.

41 { return SeedMtx[MtxDim*Row+Col].Val; }
TFltV SeedMtx
Definition: mag.h:14
TInt MtxDim
Definition: mag.h:13
double& TMAGAffMtx::At ( const int &  Row,
const int &  Col 
)
inline

Definition at line 42 of file mag.h.

42 { return SeedMtx[MtxDim*Row+Col].Val; }
TFltV SeedMtx
Definition: mag.h:14
TInt MtxDim
Definition: mag.h:13
const double& TMAGAffMtx::At ( const int &  ValN) const
inline

Definition at line 43 of file mag.h.

43 { return SeedMtx[ValN].Val; }
TFltV SeedMtx
Definition: mag.h:14
double& TMAGAffMtx::At ( const int &  ValN)
inline

Definition at line 44 of file mag.h.

44 { return SeedMtx[ValN].Val; }
TFltV SeedMtx
Definition: mag.h:14
void TMAGAffMtx::Dump ( const TStr MtxNm = TStr(),
const bool &  Sort = false 
) const

Definition at line 128 of file mag.cpp.

128  {
129  /*printf("%s: %d x %d\n", MtxNm.Empty()?"Mtx":MtxNm.CStr(), GetDim(), GetDim());
130  for (int r = 0; r < GetDim(); r++) {
131  for (int c = 0; c < GetDim(); c++) { printf(" %8.2g", At(r, c)); }
132  printf("\n");
133  }*/
134  if (! MtxNm.Empty()) printf("%s\n", MtxNm.CStr());
135  double Sum=0.0;
136  TFltV ValV = SeedMtx;
137  if (Sort) { ValV.Sort(false); }
138  for (int i = 0; i < ValV.Len(); i++) {
139  printf(" %10.4g", ValV[i]());
140  Sum += ValV[i];
141  if ((i+1) % GetDim() == 0) { printf("\n"); }
142  }
143  printf(" (sum:%.4f)\n", Sum);
144 }
TFltV SeedMtx
Definition: mag.h:14
TSizeTy Len() const
Returns the number of elements in the vector.
Definition: ds.h:575
void Sort(const bool &Asc=true)
Sorts the elements of the vector.
Definition: ds.h:1318
bool Empty() const
Definition: dt.h:491
char * CStr()
Definition: dt.h:479
int GetDim() const
Definition: mag.h:26
bool TMAGAffMtx::Empty ( ) const
inline

Definition at line 28 of file mag.h.

28 { return SeedMtx.Empty(); }
TFltV SeedMtx
Definition: mag.h:14
bool Empty() const
Tests whether the vector is empty.
Definition: ds.h:570
void TMAGAffMtx::GenMtx ( const int &  Dim)
inline

Definition at line 36 of file mag.h.

36 { MtxDim=Dim; SeedMtx.Gen(Dim*Dim); }
TFltV SeedMtx
Definition: mag.h:14
TInt MtxDim
Definition: mag.h:13
void Gen(const TSizeTy &_Vals)
Constructs a vector (an array) of _Vals elements.
Definition: ds.h:523
double TMAGAffMtx::GetAvgAbsErr ( const TMAGAffMtx Mtx1,
const TMAGAffMtx Mtx2 
)
static

Definition at line 147 of file mag.cpp.

147  {
148  TFltV P1 = Mtx1.GetMtx();
149  TFltV P2 = Mtx2.GetMtx();
150  IAssert(P1.Len() == P2.Len());
151  P1.Sort(); P2.Sort();
152  double delta = 0.0;
153  for (int i = 0; i < P1.Len(); i++) {
154  delta += fabs(P1[i] - P2[i]);
155  }
156  return delta/P1.Len();
157 }
#define IAssert(Cond)
Definition: bd.h:262
TSizeTy Len() const
Returns the number of elements in the vector.
Definition: ds.h:575
void Sort(const bool &Asc=true)
Sorts the elements of the vector.
Definition: ds.h:1318
TFltV & GetMtx()
Definition: mag.h:31
double TMAGAffMtx::GetAvgFroErr ( const TMAGAffMtx Mtx1,
const TMAGAffMtx Mtx2 
)
static

Definition at line 160 of file mag.cpp.

160  {
161  TFltV P1 = Mtx1.GetMtx();
162  TFltV P2 = Mtx2.GetMtx();
163  IAssert(P1.Len() == P2.Len());
164  P1.Sort(); P2.Sort();
165  double delta = 0.0;
166  for (int i = 0; i < P1.Len(); i++) {
167  delta += pow(P1[i] - P2[i], 2);
168  }
169  return sqrt(delta/P1.Len());
170 }
#define IAssert(Cond)
Definition: bd.h:262
TSizeTy Len() const
Returns the number of elements in the vector.
Definition: ds.h:575
void Sort(const bool &Asc=true)
Sorts the elements of the vector.
Definition: ds.h:1318
TFltV & GetMtx()
Definition: mag.h:31
double TMAGAffMtx::GetColSum ( const int &  ColId) const

Definition at line 109 of file mag.cpp.

109  {
110  double Sum = 0;
111  for (int r = 0; r < GetDim(); r++) {
112  Sum += At(r, ColId); }
113  return Sum;
114 }
const double & At(const int &Row, const int &Col) const
Definition: mag.h:41
int GetDim() const
Definition: mag.h:26
int TMAGAffMtx::GetDim ( ) const
inline

Definition at line 26 of file mag.h.

26 { return MtxDim; }
TInt MtxDim
Definition: mag.h:13
void TMAGAffMtx::GetLLMtx ( TMAGAffMtx LLMtx)

Definition at line 74 of file mag.cpp.

74  {
75  LLMtx.GenMtx(MtxDim);
76  for (int i = 0; i < Len(); i++) {
77  if (At(i) != 0.0) { LLMtx.At(i) = log(At(i)); }
78  else { LLMtx.At(i) = NInf; }
79  }
80 }
static const double NInf
Definition: mag.h:11
int Len() const
Definition: mag.h:27
TInt MtxDim
Definition: mag.h:13
const double & At(const int &Row, const int &Col) const
Definition: mag.h:41
void GenMtx(const int &Dim)
Definition: mag.h:36
TFltV& TMAGAffMtx::GetMtx ( )
inline

Definition at line 31 of file mag.h.

31 { return SeedMtx; }
TFltV SeedMtx
Definition: mag.h:14
const TFltV& TMAGAffMtx::GetMtx ( ) const
inline

Definition at line 32 of file mag.h.

32 { return SeedMtx; }
TFltV SeedMtx
Definition: mag.h:14
TMAGAffMtx TMAGAffMtx::GetMtx ( TStr  MatlabMtxStr)
static

Definition at line 173 of file mag.cpp.

173  {
174  TStrV RowStrV, ColStrV;
175  MatlabMtxStr.ChangeChAll(',', ' ');
176  MatlabMtxStr.SplitOnAllCh(';', RowStrV); IAssert(! RowStrV.Empty());
177  RowStrV[0].SplitOnWs(ColStrV); IAssert(! ColStrV.Empty());
178  const int Rows = RowStrV.Len();
179  const int Cols = ColStrV.Len();
180  IAssert(Rows == Cols);
181  TMAGAffMtx Mtx(Rows);
182  for (int r = 0; r < Rows; r++) {
183  RowStrV[r].SplitOnWs(ColStrV);
184  IAssert(ColStrV.Len() == Cols);
185  for (int c = 0; c < Cols; c++) {
186  Mtx.At(r, c) = (double) ColStrV[c].GetFlt(); }
187  }
188  return Mtx;
189 }
#define IAssert(Cond)
Definition: bd.h:262
TSizeTy Len() const
Returns the number of elements in the vector.
Definition: ds.h:575
int ChangeChAll(const char &SrcCh, const char &DstCh)
Definition: dt.cpp:1113
bool Empty() const
Tests whether the vector is empty.
Definition: ds.h:570
Definition: mag.h:10
void SplitOnAllCh(const char &SplitCh, TStrV &StrV, const bool &SkipEmpty=true) const
Definition: dt.cpp:926
TStr TMAGAffMtx::GetMtxStr ( ) const

Definition at line 63 of file mag.cpp.

63  {
64  TChA ChA("[");
65  for (int i = 0; i < Len(); i++) {
66  ChA += TStr::Fmt("%g", At(i));
67  if ((i+1)%GetDim()==0 && (i+1<Len())) { ChA += "; "; }
68  else if (i+1<Len()) { ChA += " "; }
69  }
70  ChA += "]";
71  return TStr(ChA);
72 }
int Len() const
Definition: mag.h:27
Definition: dt.h:201
Definition: dt.h:412
static TStr Fmt(const char *FmtStr,...)
Definition: dt.cpp:1599
const double & At(const int &Row, const int &Col) const
Definition: mag.h:41
int GetDim() const
Definition: mag.h:26
double TMAGAffMtx::GetMtxSum ( ) const

Definition at line 95 of file mag.cpp.

95  {
96  double Sum = 0;
97  for (int i = 0; i < Len(); i++) {
98  Sum += At(i); }
99  return Sum;
100 }
int Len() const
Definition: mag.h:27
const double & At(const int &Row, const int &Col) const
Definition: mag.h:41
int TMAGAffMtx::GetPrimHashCd ( ) const
inline

Definition at line 22 of file mag.h.

22 { return SeedMtx.GetPrimHashCd(); }
int GetPrimHashCd() const
Returns primary hash code of the vector. Used by THash.
Definition: ds.h:999
TFltV SeedMtx
Definition: mag.h:14
void TMAGAffMtx::GetProbMtx ( TMAGAffMtx ProbMtx)

Definition at line 82 of file mag.cpp.

82  {
83  ProbMtx.GenMtx(MtxDim);
84  for (int i = 0; i < Len(); i++) {
85  if (At(i) != NInf) { ProbMtx.At(i) = exp(At(i)); }
86  else { ProbMtx.At(i) = 0.0; }
87  }
88 }
static const double NInf
Definition: mag.h:11
int Len() const
Definition: mag.h:27
TInt MtxDim
Definition: mag.h:13
const double & At(const int &Row, const int &Col) const
Definition: mag.h:41
void GenMtx(const int &Dim)
Definition: mag.h:36
TMAGAffMtx TMAGAffMtx::GetRndMtx ( TRnd Rnd,
const int &  Dim = 2,
const double &  MinProb = 0.0 
)
static

Definition at line 191 of file mag.cpp.

191  {
192  TMAGAffMtx Mtx;
193  Mtx.SetRndMtx(Rnd, Dim, MinProb);
194  return Mtx;
195 }
Definition: mag.h:10
void SetRndMtx(TRnd &Rnd, const int &PrmMtxDim=2, const double &MinProb=0.0)
Definition: mag.cpp:34
double TMAGAffMtx::GetRowSum ( const int &  RowId) const

Definition at line 102 of file mag.cpp.

102  {
103  double Sum = 0;
104  for (int c = 0; c < GetDim(); c++) {
105  Sum += At(RowId, c); }
106  return Sum;
107 }
const double & At(const int &Row, const int &Col) const
Definition: mag.h:41
int GetDim() const
Definition: mag.h:26
int TMAGAffMtx::GetSecHashCd ( ) const
inline

Definition at line 23 of file mag.h.

23 { return SeedMtx.GetSecHashCd(); }
TFltV SeedMtx
Definition: mag.h:14
int GetSecHashCd() const
Returns secondary hash code of the vector. Used by THash.
Definition: ds.h:1011
bool TMAGAffMtx::IsProbMtx ( ) const

Definition at line 27 of file mag.cpp.

27  {
28  for (int i = 0; i < Len(); i++) {
29  if (At(i) < 0.0 || At(i) > 1.0) return false;
30  }
31  return true;
32 }
int Len() const
Definition: mag.h:27
const double & At(const int &Row, const int &Col) const
Definition: mag.h:41
int TMAGAffMtx::Len ( ) const
inline

Definition at line 27 of file mag.h.

27 { return SeedMtx.Len(); }
TFltV SeedMtx
Definition: mag.h:14
TSizeTy Len() const
Returns the number of elements in the vector.
Definition: ds.h:575
double TMAGAffMtx::Normalize ( )

Definition at line 116 of file mag.cpp.

116  {
117  double Sum = GetMtxSum();
118  if(Sum == 0) {
119  return 0;
120  }
121 
122  for(int i = 0; i < Len(); i++) {
123  At(i) = At(i) / Sum;
124  }
125  return Sum;
126 }
int Len() const
Definition: mag.h:27
double GetMtxSum() const
Definition: mag.cpp:95
const double & At(const int &Row, const int &Col) const
Definition: mag.h:41
TMAGAffMtx & TMAGAffMtx::operator= ( const TMAGAffMtx Kronecker)

Definition at line 19 of file mag.cpp.

19  {
20  if (this != &Kronecker){
21  MtxDim=Kronecker.MtxDim;
22  SeedMtx=Kronecker.SeedMtx;
23  }
24  return *this;
25 }
TFltV SeedMtx
Definition: mag.h:14
TInt MtxDim
Definition: mag.h:13
bool TMAGAffMtx::operator== ( const TMAGAffMtx Kronecker) const
inline

Definition at line 21 of file mag.h.

21 { return SeedMtx==Kronecker.SeedMtx; }
TFltV SeedMtx
Definition: mag.h:14
void TMAGAffMtx::PutAllMtx ( const double &  Val)
inline

Definition at line 35 of file mag.h.

35 { SeedMtx.PutAll(Val); }
TFltV SeedMtx
Definition: mag.h:14
void PutAll(const TVal &Val)
Sets all elements of the vector to value Val.
Definition: ds.h:1229
void TMAGAffMtx::SetEpsMtx ( const double &  Eps1,
const double &  Eps0,
const int &  Eps1Val = 1,
const int &  Eps0Val = 0 
)

Definition at line 44 of file mag.cpp.

44  {
45  for (int i = 0; i < Len(); i++) {
46  double& Val = At(i);
47  if (Val == Eps1Val) Val = double(Eps1);
48  else if (Val == Eps0Val) Val = double(Eps0);
49  }
50 }
int Len() const
Definition: mag.h:27
const double & At(const int &Row, const int &Col) const
Definition: mag.h:41
void TMAGAffMtx::SetMtx ( const TFltV ParamV)
inline

Definition at line 33 of file mag.h.

33 { SeedMtx = ParamV; }
TFltV SeedMtx
Definition: mag.h:14
void TMAGAffMtx::SetRndMtx ( TRnd Rnd,
const int &  PrmMtxDim = 2,
const double &  MinProb = 0.0 
)

Definition at line 34 of file mag.cpp.

34  {
35  MtxDim = PrmMtxDim;
37  for (int p = 0; p < SeedMtx.Len(); p++) {
38  do {
39  SeedMtx[p] = Rnd.GetUniDev();
40  } while (SeedMtx[p] < MinProb);
41  }
42 }
TFltV SeedMtx
Definition: mag.h:14
TSizeTy Len() const
Returns the number of elements in the vector.
Definition: ds.h:575
TInt MtxDim
Definition: mag.h:13
double GetUniDev()
Definition: dt.h:30
void Gen(const TSizeTy &_Vals)
Constructs a vector (an array) of _Vals elements.
Definition: ds.h:523
void TMAGAffMtx::Swap ( TMAGAffMtx Mtx)

Definition at line 90 of file mag.cpp.

90  {
91  ::Swap(MtxDim, Mtx.MtxDim);
92  SeedMtx.Swap(Mtx.SeedMtx);
93 }
void Swap(TMAGAffMtx &Mtx)
Definition: mag.cpp:90
TFltV SeedMtx
Definition: mag.h:14
void Swap(TVec< TVal, TSizeTy > &Vec)
Swaps the contents of the vector with Vec.
Definition: ds.h:1101
TInt MtxDim
Definition: mag.h:13

Member Data Documentation

TInt TMAGAffMtx::MtxDim
private

Definition at line 13 of file mag.h.

const double TMAGAffMtx::NInf = -DBL_MAX
staticprivate

Definition at line 11 of file mag.h.

TFltV TMAGAffMtx::SeedMtx
private

Definition at line 14 of file mag.h.


The documentation for this class was generated from the following files: