-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSparseMatrix.C
More file actions
executable file
·53 lines (45 loc) · 1.4 KB
/
SparseMatrix.C
File metadata and controls
executable file
·53 lines (45 loc) · 1.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
//
// SparseMatrix.C
//
//
// Created by Ling Zou on 9/19/13.
//
//
#include <stdio.h>
#include <iostream>
#include <sstream>
#include <cstdlib> // exit
#include "SparseMatrix.h"
void
SparseMatrix::SetRowNumber(unsigned int total_row_number)
{
// Initialize the vector
EntryData.resize(total_row_number);
}
void
SparseMatrix::AddEntry(unsigned int row, unsigned int col, double value)
{
ColumnEntry entry;
entry.col_number = col;
entry.value = value;
if(row < EntryData.size())
EntryData[row].push_back(entry);
else
{
std::cerr << "ERROR: The ColumnEntry being added is out of range. It's index, " << row << " (starts from 0), is beyond the EntryData size, " << EntryData.size() << "\n";
exit(1);
}
}
void
SparseMatrix::Insert(const SparseMatrix & sparse_matrix, unsigned int row, unsigned int col)
{
const std::vector<std::vector<ColumnEntry> > & data_entries_being_inserted = sparse_matrix.getEntryData();
if((EntryData.size() < data_entries_being_inserted.size() + row) || (EntryData.size() < data_entries_being_inserted.size() + col))
{
std::cerr << "Not enough room for the SparseMatrix being inserted.\n";
exit(1);
}
for(int i = 0; i < data_entries_being_inserted.size(); i++)
for(int j = 0; j < data_entries_being_inserted[i].size(); j++)
AddEntry(i + row, data_entries_being_inserted[i][j].col_number + col, data_entries_being_inserted[i][j].value);
}