Notice
Recent Posts
Recent Comments
Link
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
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 |
Tags
- DP
- dfs
- 플로이드와샬
- vector
- 백준
- 코테
- 시스템콜
- 코딩테스트연습
- 코테준비
- 다익스트라
- 리시프
- 다이나믹프로그래밍
- 정렬
- sql고득점kit
- 최대공약수
- C++
- 우선순위큐
- String
- 알고리즘
- 참고 문헌 : MACHINE LEARNING 기계학습 _ 오일석
- MAP
- unordered_map
- 동적계획법
- 프로그래머스
- substr
- Dijkstra
- MySQL
- 문자열
- 스택
- set
Archives
- Today
- Total
YeJin's Footsteps
SparseMatrix::Multiply 본문
SparseMatrix SparseMatrix::Multiply(SparseMatrix b)
{ // Return the product of the sparse matrices *this and b.
if (cols != b.rows)
throw "Incompatible matrices";
SparseMatrix bXpose = b.FastTranspose();
SparseMatrix d(rows, b.cols, 0);
int currRowIndex = 0,
currRowBegin = 0,
currRowA = smArray[0].row;
// set boundary conditions
if (terms == capacity)
ChangeSize1D(terms + 1);
bXpose.ChangeSize1D(bXpose.terms + 1);
smArray[terms].row = rows;
bXpose.smArray[b.terms].row = b.cols;
bXpose.smArray[b.terms].col = -1;
int sum = 0;
while (currRowIndex < terms)
{ // generate row currentRowA of d
int currColB = bXpose.smArray[0].row;
int currColIndex = 0;
while (currColIndex <= b.terms)
{ // multiply row currRowA of *this by column currColB of b
if (smArray[currRowIndex].row != currRowA)
{ // end of row currRowA
d.StoreSum(sum, currRowA, currColB);
sum = 0; // reset sum
currRowIndex = currRowBegin;
// advance to next column
while (bXpose.smArray[currColIndex].row == currColB)
currColIndex++;
currColB = bXpose.smArray[currColIndex].row;
}
else if (bXpose.smArray[currColIndex].row != currColB)
{ // end of column currColB of b
d.StoreSum(sum, currRowA, currColB);
sum = 0; // reset sum
// set to multiply row currRowA with next column
currRowIndex = currRowBegin;
currColB = bXpose.smArray[currColIndex].row;
}
else if (smArray[currRowIndex].col <
bXpose.smArray[currColIndex].col)
currRowIndex++; // advance to next term in row
else if (smArray[currRowIndex].col ==
bXpose.smArray[currColIndex].col)
{ // add to sum
sum += smArray[currRowIndex].value *
bXpose.smArray[currColIndex].value;
currRowIndex++;
currColIndex++;
}
else
currColIndex++; // next term in currColB
} // end of while (currColIndex <= b.terms)
while (smArray[currRowIndex].row == currRowA) // advance to next row
currRowIndex++;
currRowBegin = currRowIndex;
currRowA = smArray[currRowIndex].row;
} // end of while (currRowIndex < terms)
return d;
}
'Computer Science & Engineering > 자료구조' 카테고리의 다른 글
자료구조_중간고사 공부 (최종 정리) (0) | 2021.05.02 |
---|---|
중간고사 공부_Introduction_part1_2 (0) | 2021.04.09 |
중간고사 공부_Introduction_part1 (0) | 2021.04.09 |
shallow copy(얕은 복사) vs deep copy(깊은 복사) (0) | 2021.04.07 |
Comments