- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
我无法理解问题中的“a 的行索引”指的是什么……您能举个例子吗?为什么
sum = sum + a[(row * a_cols) + k] * b[k * b_cols + col]?
谢谢!
double dotProduct(double a[], const unsigned a_rows, const unsigned a_cols,
/* a is a matrix with a_rows rows and a_cols columns */
double b[], const unsigned b_cols,
/* b is also a matrix. It has a_cols rows and b_cols columns */
unsigned row, // an **index of a row of a**
unsigned col) // an index of a column of b
{
int k; // loop variable
double sum = 0.0; // the result of the dot product is stored here
k = 0;
while (k < a_cols) { // recall: a_cols == b_rows
/* we need to multiply a[row, k] and b[k, col] and add that to sum */
sum = sum + a[(row * a_cols) + k] * b[k * b_cols + col];
/* recall a[i,j] is stored at a[(i * a_cols) + j]
and b[i,j] is stored at b[(i * b_cols) + j] */
k += 1;
}
return sum;
}
最佳答案
编辑我的不好,忘了我的第一个答案,我误解了问题...
回答:dotProduct() 方法的第 5 个参数,名为 row,作为 a 的行索引 给出。
这意味着它是一个 0 到 (a__rows -1) 的值,用于指定“矩阵”a 的一个特定行。 (实际上它可能是 1 到 a__rows 的值。这只是一个约定问题;数学人员倾向于喜欢“基于 1”的索引,程序员更喜欢基于 0 的值。请参阅 Adam Liss 对此主题的有趣评论在备注中)
由于矩阵 a 是在一维数组中实现的,因此您需要使用简单的算术来寻址 a 中由 row 索引的行上的所有单元格。
“公式”是按顺序访问所有这些单元格是
for (int c = 0; c < a_cols; c++)
{
A_Cell_Value = a[row * a_cols + c];
}
将应用类似的公式来扫描 b 的给定 列 的单元格。然而在这种情况下,索引 col 将用作偏移量,而 b_cols 将用作因子。具体来说,
// not an error this is a_cols which also corresponds to the number of rows of
// matrix b, so that these matrices would be compatible for multiplication
for (int r = 0; r < a_cols; r++)
{
B_Cell_Value = b[(r * b_cols) + col];
}
我认为以上内容为您提供了遍历行或列单元格的必要理解。我让您将所有这些放在您的应用程序中。
一些提示: - 做一些参数值检查。这将避免在这些矩阵上出现越界错误。 - 在您的程序中引入抽象的一个好方法是引入一个函数,该函数从矩阵维度和两个行和列索引返回矩阵的单个单元格的值。例如:
// returns the matrix's cell value at RowIdx and colIdx
double GetCell(double matrix[], int nbOfRow, int nbOfColumns,
int rowIdx, int colIdx)
{
if (rowIdx < 0 || rowIdx >= nbOfRows ||
colIdx <0 || colIdx >= nbOfColumns
)
{
printf("bad arguments in GetCell()\n");
return 0; // print
}
return matrix[rowIdx * nbOfColumns + colIdx];
}
以这种方式,您将抽象化(=隐藏相关细节)这些线性存储的丑陋矩阵,并能够在点积公式级别根据行和列索引来解决它们。
换句话说,GetCell() 函数担心根据其对矩阵实现结构的了解找到正确的单元格。它不必知道此单元格值将用于什么。 DotProduct 计算逻辑担心 A 的哪个系列的单元格与 B 的哪个系列的单元格相乘,根据它们的行/列(i/j 等)“协调”“自然地”解决这些单元格中的每一个,它不会' 需要知道数据是如何有效地存储在矩阵实现中的。
关于c - 指针,内存分配 : index of rows in Matrix Multiplication (C Programming),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1563855/
我认为这个问题说明了一切,但我有一个使用 .net 安装工具包的应用程序(在 vs.2005 中),并且用户问我是否可以将它安装在 c:\Program Files\ProgramName 而不是C:
这个问题不太可能帮助任何 future 的访问者;它只与一个小的地理区域、一个特定的时间点或一个非常狭窄的情况有关,这些情况并不普遍适用于互联网的全局受众。为了帮助使这个问题更广泛地适用,visit
我是 Stephen Wolfram 的忠实粉丝,但他绝对是一个不怕自吹自擂的人。在许多引用资料中,他将 Mathematica 颂扬为一种不同的符号编程范式。我不是 Mathematica 用户。
我现在正在使用 Squeak4.1 学习 SmallTalk。我使用 Squeak by Example 作为教程,在这里我反驳了一个 delema,“Morphic 是由...开发的,用于自编程语言
Wikipedia有话要说: Total functional programming (also known as strong functional programming, to be cont
在阅读 Paul Graham's Essays 时, 我对 Lisp 越来越好奇了。 在this article ,他提到最强大的功能之一是您可以编写可以编写其他程序的程序。 我无法在他的网站或其他
我知道 functional programming 有几个定义。 .我认为这是一个模糊的类别。我个人的定义是接近' referential transparency '。 这个问题不是“函数式编程的
我注意到许多顶尖大学都开设了类(class),在这些类(class)中,学生将学习与计算机图形学相关的 CS 专业科目。可悲的是,这是我的大学没有提供的东西,我真的很想在 future 几年的某个时候
我正在安装100%托管代码的.NET(C#)应用程序。安装程序(InnoSetup)始终希望将应用程序安装到Vista x64中的“Program Files(x86)”文件夹中,我认为这是因为安装程
假设在 C 中,我们有以下结构: struct MyData { char key1[20]; long key2; ... /* some data */ }; 本质上,除
这个问题已经有答案了: When should I use ampersand with scanf() (3 个回答) 已关闭 6 年前。 所以我在python3中有这个“程序”,它添加了3个字符串
我编写了一个包含 self 更新程序的 Java 应用程序。自更新程序从 Web 服务器加载新的程序版本并替换应用程序文件。如果安装了应用程序,这将完美地工作,例如在用户主目录中,如果它安装在 C:\
注意:标记为社区维基。 是否有一个很好的分析为什么可视化编程语言仍然没有起飞?这些天我们仍在 80x25 文本窗口中“线性”编码;而我们表示的概念(数据结构、算法)似乎可以更直观地表示出来。 最佳答案
我一直在阅读Code Complete 2 .由于我不是以英语为母语的人,因此我需要一些时间才能理解某些陈述。我希望你描述作者在他的书中所做的这两个陈述之间的区别: You should progra
我在为我的 tomcat 设置 CLASSPATH 时遇到了这个问题。我需要在 tomcat 的 CLASSPATH 中引用我的 2 个安装。其中一个位于 C:\Program Files\Postg
这个问题已经有答案了: How can I lock a file using java (if possible) (8 个回答) 已关闭 6 年前。 我有 2-3 个程序可以修改文件,但如果有一个
我 checkout Reading stdout from one program in another program却没有找到我要找的答案 我是 Linux 的新手,我正在使用 Python 中
我有一个程序可以打印出通过或失败。我想检测卡在那里的程序并回显“超时” 我写了这样一个脚本: #!/bin/bash echo -n 'test' && timeout 5 ./mytest | gr
我非常清楚函数式编程技术和命令式编程技术之间的区别。但是现在有一种普遍的趋势是谈论“函数式语言”,这确实让我感到困惑。 当然,像 Haskell 这样的一些语言比 C 等其他语言更欢迎函数式编程。但即
请求:每个进程需要计算自己的组到所有点的距离。我的代码如下: #include stdio.h #include stdlib.h #include math.h #include string.h
我是一名优秀的程序员,十分优秀!