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
TSparseRowMatrix Class Reference

#include <linalg.h>

Inherits TMatrix.

Public Member Functions

 TSparseRowMatrix ()
 
 TSparseRowMatrix (TVec< TIntFltKdV > _RowSpVV)
 
 TSparseRowMatrix (TVec< TIntFltKdV > _RowSpVV, const int &_RowN, const int &_ColN)
 
 TSparseRowMatrix (const TStr &MatlabMatrixFNm)
 
void Save (TSOut &SOut)
 
void Load (TSIn &SIn)
 
- Public Member Functions inherited from TMatrix
 TMatrix ()
 
virtual ~TMatrix ()
 
void Multiply (const TFltVV &B, int ColId, TFltV &Result) const
 
void Multiply (const TFltV &Vec, TFltV &Result) const
 
void MultiplyT (const TFltVV &B, int ColId, TFltV &Result) const
 
void MultiplyT (const TFltV &Vec, TFltV &Result) const
 
int GetRows () const
 
int GetCols () const
 
void Transpose ()
 

Public Attributes

int RowN
 
int ColN
 
TVec< TIntFltKdVRowSpVV
 

Protected Member Functions

virtual void PMultiply (const TFltVV &B, int ColId, TFltV &Result) const
 
virtual void PMultiply (const TFltV &Vec, TFltV &Result) const
 
virtual void PMultiplyT (const TFltVV &B, int ColId, TFltV &Result) const
 
virtual void PMultiplyT (const TFltV &Vec, TFltV &Result) const
 
int PGetRows () const
 
int PGetCols () const
 

Detailed Description

Definition at line 90 of file linalg.h.

Constructor & Destructor Documentation

TSparseRowMatrix::TSparseRowMatrix ( )
inline

Definition at line 110 of file linalg.h.

110 : TMatrix() {}
TMatrix()
Definition: linalg.h:20
TSparseRowMatrix::TSparseRowMatrix ( TVec< TIntFltKdV _RowSpVV)
inline

Definition at line 111 of file linalg.h.

111 : TMatrix(), RowSpVV(_RowSpVV) {}
TVec< TIntFltKdV > RowSpVV
Definition: linalg.h:95
TMatrix()
Definition: linalg.h:20
TSparseRowMatrix::TSparseRowMatrix ( TVec< TIntFltKdV _RowSpVV,
const int &  _RowN,
const int &  _ColN 
)
inline

Definition at line 112 of file linalg.h.

112  :
113  TMatrix(), RowN(_RowN), ColN(_ColN), RowSpVV(_RowSpVV) {}
TVec< TIntFltKdV > RowSpVV
Definition: linalg.h:95
TMatrix()
Definition: linalg.h:20
TSparseRowMatrix::TSparseRowMatrix ( const TStr MatlabMatrixFNm)

Definition at line 53 of file linalg.cpp.

53  {
54  FILE *F = fopen(MatlabMatrixFNm.CStr(), "rt"); IAssert(F != NULL);
56  RowN = 0; ColN = 0;
57  while (! feof(F)) {
58  int row=-1, col=-1; float val;
59  if (fscanf(F, "%d %d %f\n", &row, &col, &val) == 3) {
60  IAssert(row > 0 && col > 0);
61  MtxV.Add(TTriple<TInt, TInt, TSFlt>(row, col, val));
62  RowN = TMath::Mx(RowN, row);
63  ColN = TMath::Mx(ColN, col);
64  }
65  }
66  fclose(F);
67  // create matrix
68  MtxV.Sort();
69  RowSpVV.Gen(RowN);
70  int cnt = 0;
71  for (int row = 1; row <= RowN; row++) {
72  while (cnt < MtxV.Len() && MtxV[cnt].Val1 == row) {
73  RowSpVV[row-1].Add(TIntFltKd(MtxV[cnt].Val2-1, MtxV[cnt].Val3()));
74  cnt++;
75  }
76  }
77 }
#define IAssert(Cond)
Definition: bd.h:262
Definition: ds.h:130
static const T & Mx(const T &LVal, const T &RVal)
Definition: xmath.h:32
TVec< TIntFltKdV > RowSpVV
Definition: linalg.h:95
TSizeTy Len() const
Returns the number of elements in the vector.
Definition: ds.h:575
TKeyDat< TInt, TFlt > TIntFltKd
Definition: ds.h:381
void Sort(const bool &Asc=true)
Sorts the elements of the vector.
Definition: ds.h:1318
char * CStr()
Definition: dt.h:479
TSizeTy Add()
Adds a new element at the end of the vector, after its current last element.
Definition: ds.h:602
Vector is a sequence TVal objects representing an array that can change in size.
Definition: ds.h:430

Member Function Documentation

void TSparseRowMatrix::Load ( TSIn SIn)
inline

Definition at line 119 of file linalg.h.

119  {
120  SIn.Load(RowN); SIn.Load(ColN); RowSpVV = TVec<TIntFltKdV>(SIn); }
TVec< TIntFltKdV > RowSpVV
Definition: linalg.h:95
void Load(bool &Bool)
Definition: fl.h:84
Vector is a sequence TVal objects representing an array that can change in size.
Definition: ds.h:430
int TSparseRowMatrix::PGetCols ( ) const
inlineprotectedvirtual

Implements TMatrix.

Definition at line 107 of file linalg.h.

107 { return ColN; }
int TSparseRowMatrix::PGetRows ( ) const
inlineprotectedvirtual

Implements TMatrix.

Definition at line 106 of file linalg.h.

106 { return RowN; }
void TSparseRowMatrix::PMultiply ( const TFltVV B,
int  ColId,
TFltV Result 
) const
protectedvirtual

Implements TMatrix.

Definition at line 101 of file linalg.cpp.

101  {
102  Assert(B.GetRows() >= ColN && Result.Len() >= RowN);
103  for (int j = 0; j < RowN; j++) {
104  const TIntFltKdV& RowV = RowSpVV[j];
105  int len = RowV.Len(); Result[j] = 0.0;
106  for (int i = 0; i < len; i++) {
107  Result[j] += RowV[i].Dat * B(RowV[i].Key, ColId);
108  }
109  }
110 }
TVec< TIntFltKdV > RowSpVV
Definition: linalg.h:95
TSizeTy Len() const
Returns the number of elements in the vector.
Definition: ds.h:575
#define Assert(Cond)
Definition: bd.h:251
TSizeTy GetRows() const
Definition: ds.h:2252
Vector is a sequence TVal objects representing an array that can change in size.
Definition: ds.h:430
void TSparseRowMatrix::PMultiply ( const TFltV Vec,
TFltV Result 
) const
protectedvirtual

Implements TMatrix.

Definition at line 112 of file linalg.cpp.

112  {
113  Assert(Vec.Len() >= ColN && Result.Len() >= RowN);
114  for (int j = 0; j < RowN; j++) {
115  const TIntFltKdV& RowV = RowSpVV[j];
116  int len = RowV.Len(); Result[j] = 0.0;
117  for (int i = 0; i < len; i++) {
118  Result[j] += RowV[i].Dat * Vec[RowV[i].Key];
119  }
120  }
121 }
TVec< TIntFltKdV > RowSpVV
Definition: linalg.h:95
TSizeTy Len() const
Returns the number of elements in the vector.
Definition: ds.h:575
#define Assert(Cond)
Definition: bd.h:251
Vector is a sequence TVal objects representing an array that can change in size.
Definition: ds.h:430
void TSparseRowMatrix::PMultiplyT ( const TFltVV B,
int  ColId,
TFltV Result 
) const
protectedvirtual

Implements TMatrix.

Definition at line 79 of file linalg.cpp.

79  {
80  Assert(B.GetRows() >= RowN && Result.Len() >= ColN);
81  for (int i = 0; i < ColN; i++) Result[i] = 0.0;
82  for (int j = 0; j < RowN; j++) {
83  const TIntFltKdV& RowV = RowSpVV[j]; int len = RowV.Len();
84  for (int i = 0; i < len; i++) {
85  Result[RowV[i].Key] += RowV[i].Dat * B(j,ColId);
86  }
87  }
88 }
TVec< TIntFltKdV > RowSpVV
Definition: linalg.h:95
TSizeTy Len() const
Returns the number of elements in the vector.
Definition: ds.h:575
#define Assert(Cond)
Definition: bd.h:251
TSizeTy GetRows() const
Definition: ds.h:2252
Vector is a sequence TVal objects representing an array that can change in size.
Definition: ds.h:430
void TSparseRowMatrix::PMultiplyT ( const TFltV Vec,
TFltV Result 
) const
protectedvirtual

Implements TMatrix.

Definition at line 90 of file linalg.cpp.

90  {
91  Assert(Vec.Len() >= RowN && Result.Len() >= ColN);
92  for (int i = 0; i < ColN; i++) Result[i] = 0.0;
93  for (int j = 0; j < RowN; j++) {
94  const TIntFltKdV& RowV = RowSpVV[j]; int len = RowV.Len();
95  for (int i = 0; i < len; i++) {
96  Result[RowV[i].Key] += RowV[i].Dat * Vec[j];
97  }
98  }
99 }
TVec< TIntFltKdV > RowSpVV
Definition: linalg.h:95
TSizeTy Len() const
Returns the number of elements in the vector.
Definition: ds.h:575
#define Assert(Cond)
Definition: bd.h:251
Vector is a sequence TVal objects representing an array that can change in size.
Definition: ds.h:430
void TSparseRowMatrix::Save ( TSOut SOut)
inline

Definition at line 117 of file linalg.h.

117  {
118  SOut.Save(RowN); SOut.Save(ColN); RowSpVV.Save(SOut); }
TVec< TIntFltKdV > RowSpVV
Definition: linalg.h:95
void Save(const bool &Bool)
Definition: fl.h:173

Member Data Documentation

int TSparseRowMatrix::ColN

Definition at line 93 of file linalg.h.

int TSparseRowMatrix::RowN

Definition at line 93 of file linalg.h.

TVec<TIntFltKdV> TSparseRowMatrix::RowSpVV

Definition at line 95 of file linalg.h.


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