- iOS/Objective-C 元类和类别
- objective-c - -1001 错误,当 NSURLSession 通过 httpproxy 和/etc/hosts
- java - 使用网络类获取 url 地址
- ios - 推送通知中不播放声音
我有一个 CSR 格式的矩阵,需要一个 C++ vector ,其中包含每行的非零条目数(计数),限制为不同大小的正方形、对角线 block 。
// The matrix in CSR format
std::vector<int> row_idx = {0,2,4,6,10,13}; // size n+1 where 0-n are the idx of the row starts in values and column_idx and n+1 the TOTAL number of values
std::vector<int> values = {1,6,2,7,3,8,10,11,4,9,12,13,5}; // nonzero matrix values
std::vector<int> column_idx = {0,3,1,3,2,4,0,1,3,4,2,3,4}; // column indices of the values
下面的例子有两个不同大小的 block A 和 B(感兴趣的 block 总是正方形并且在对角线上)。
这个例子的期望结果是 nnz_in_ranges[n] = {1,1,2,2,3} 但因为它需要嵌入到另一个例程中,我主要是在寻找一个例程来计算单个 block 使用C++。像这样:
// block A
int rangeStart = 0;
int rangeEnd = 2;
// block B
//int rangeStart = 2;
//int rangeEnd = n;
for (int i = rangeStart; i<rangeEND; ++i)
{
nnz_in_ranges[n] = ...
}
// desired result for block A: nnz_in_ranges[n] = {1,1,0,0,0}
// desired result for block B: nnz_in_ranges[n] = {0,0,2,2,3}
我尝试使用 std::count... 函数解决它,但我无法扩展下面的代码,计算每行的非零值,因为我无法引入列范围。
有人知道如何解决这个问题吗?
#include <stdio.h>
#include <iostream>
#include <vector>
#include <algorithm>
int main()
{
// NxN matrix example
/*
index 0 1 2 3 4
______________________
0 | 1 0 | 0 6 0 |
| A | |
1 | 0 2 | 0 7 0 |
|--------------------|
2 | 0 0 | 3 0 8 |
| | B |
3 | 10 11 | 0 4 9 | expected result: nnz_in_ranges[n] = {1,1,2,2,3}
| | | here ranges are A and B
4 | 0 0 |12 13 5 |
----------------------
*/
// matrix in CSR format
int n = 5; // matrix size
int nnz = 13; // number of nonzero values
// The matrix in CSR format
std::vector<int> row_idx = {0,2,4,6,10,13}; // size n+1 where 0-n are the idx of the row starts in values and column_idx and n+1 the TOTAL number of values
std::vector<int> values = {1,6,2,7,3,8,10,11,4,9,12,13,5}; // nonzero matrix values
std::vector<int> column_idx = {0,3,1,3,2,4,0,1,3,4,2,3,4}; // column indices of the values
std::vector<int> tmp = {0,0,0,0,0,0,0,0,0,0,0,0,0};
std::vector<int> sum(n);
// count nonzeros per row sum[] = {2,2,2,4,3}
for(size_t i = 0; i < row_idx.size()-1; ++i) {
sum[i] = std::count(tmp.begin() + row_idx[i], tmp.begin() + row_idx[i + 1], 0);
}
std::cout << "nnz_in_range = " << std::endl;
for (int i=0; i<n; i++)
{
std::cout << ' ' << sum[i];
}
return 0;
}
最佳答案
稀疏矩阵可以显式包含零项。根据您的问题,不清楚您是要计算头寸还是实际值。我会假设前者,因为您的计数代码不使用 values
。
然后我们只要按照CSR格式的定义就可以得到这样的东西:
std::vector<int> count_positions_in_block(int block_begin, int block_size,
const std::vector<int>& row_idx, const std::vector<int>& column_idx)
{
std::vector<int> cnt(block_size, 0);
const auto block_end = block_begin + block_size;
assert(block_end < row_idx.size());
for (auto row = block_begin; row < block_end; ++row)
{
auto first = row_idx[row];
auto last = row_idx[row + 1];
assert(first <= last);
for (auto i = first; i < last; ++i)
if (column_idx[i] >= block_begin && column_idx[i] < block_end)
++cnt[row - block_begin];
}
return cnt;
}
auto nnz1 = count_positions_in_block(0, 2, row_idx, column_idx);
// nnz1 = [1, 1]
auto nnz2 = count_positions_in_block(2, 3, row_idx, column_idx);
// nnz2 = [2, 2, 3]
可以使用std::count_if
重写:
std::vector<int> count_positions_in_block(int block_begin, int block_size,
const std::vector<int>& row_idx, const std::vector<int>& column_idx)
{
std::vector<int> cnt(block_size, 0);
const auto block_end = block_begin + block_size;
assert(block_end < row_idx.size());
const auto is_in_block = [&](auto col)
{ return (col >= block_begin && col < block_end); };
for (auto row = block_begin; row < block_end; ++row)
{
auto first = row_idx[row];
auto last = row_idx[row + 1];
assert(first <= last);
const auto cib = column_idx.begin();
cnt[row - block_begin] = std::count_if(
cib + first, cib + last, is_in_block);
}
return cnt;
}
关于c++ - 如何计算 CSR 矩阵对角 block 中每行的非零值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52140271/
我真的不知道这个问题以前是否有人问过(我真的找不到) 所以,我正在学习如何创建基本的颜色切换游戏(随机颜色球下降,你需要旋转轮子与相同颜色的球碰撞) 通过这种轮换,我遇到了一个非常大的问题。我需要以某
我必须找到具有 M 对角线和 M << N 的对称方 NxN 矩阵的行列式.有没有比LU分解矩阵更快的方法? 最佳答案 是的,带(ed)矩阵有特殊的方法可以解决复杂度为 O(N*M^2) 的消元问题。
我有一个列数和行数相等的二维 numpy 数组。我想将它们排列成一个更大的阵列,对角线上有较小的阵列。应该可以指定起始矩阵在对角线上的频率。例如: a = numpy.array([[5, 7],
我是一名优秀的程序员,十分优秀!