下三角矩阵-矢量积 [英] Lower triangular matrix-vector product

查看:116
本文介绍了下三角矩阵-矢量积的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

为了进行编程练习,我得到了保存为数组的对称3x3矩阵的下三角元素

For a programming exercise, I was given the lower triangular elements of a symmetric 3x3 matrix saved as an array

|1 * *|

|2 4 *| => [1,2,3,4,5,6]

|3 5 6|

我需要乘积C(i)= C(i)+ M(i,j)V(j),其中M是对称矩阵,V是向量.

I need to make the product C(i)=C(i)+M(i,j)V(j) where M is the symmetric matrix and V is a vector.

V =>[A,B,C]
C(1)=1*A + 2*B + 3*C
C(2)=2*A + 4*B + 5*C
C(3)=3*A + 5*B + 6*C

我正在尝试制定一种可以执行此产品的高效算法

I am trying to make an efficient algorithm that can perform this product

我可以轻松生成C(3)所需的所有乘积,但是,当我尝试生成值C(1),C(2)时遇到问题,我不知道该如何解决无需使用额外的内存.

I can easily generate all the product I need for C(3) However, I have a problem when I try to generate the values C(1), C(2) and I don't know how to get around this without using extra memory.

这就是我所做的

k=6
n=3

  DO 1 j = n,1,-1
    l= k
    DO 2 i = n,j + 1,-1
       C(i) = C(i) + V(j)*M(l)
       l = l - 1           
2   enddo             
    C(j) = V(j)*M(k-n+j)
    k = k - (n-j+1)
1 enddo

我遇到的问题是我无法生成并为C(1)添加2 * B,为C(2)添加5 * C.该练习的目标是使用尽可能少的步骤和尽可能少的数组空间.

The problem I have is that I can no generate and add the 2*B for C(1) and the 5*C for C(2). The goal of the exercise is to use as few steps and as little array space as possible.

有什么建议吗?

推荐答案

您的代码存在多个问题:

There are multiple issues with your code:

  • 在外循环中,您分配了 C(n)(可能是对角线条目),因此根本不使用内循环的计算
  • 您正在从左到右循环遍历左下三角形,如果要反转它,则矢量化矩阵内部的索引会简单得多
  • 矩阵内部位置( k l )的计算错误
  • 您不计算镜像元素的乘积
  • In the outer loop, you assign C(n) (probably the diagonal entries), so the computation of inner loop is not used at all
  • You are looping over the lower left triangle from back to front, if you would reverse that, the indexing inside the vectorized matrix would be much simpler
  • The calculation of the position inside the matrix (k and l) is wrong
  • You do not calculate the products of the mirrored elements

这是我的解决方案,可以胜过上述几点:

Here is my solution that honors above points:

  ! Loop over all elements in the lower left triangle
  k = 0
  do j=1,n
    ! Increment the position inside the unrolled matrix
    k = k+1
    ! diagonal entries, i = j
    c(j) = c(j) + v(j)*M(k)

    ! off-diagonal entries
    do i=j+1,n
      ! Increment the position inside the unrolled matrix
      k = k+1
      ! Original entry
      c(i) = c(i) + v(j)*M(k)
      ! Mirrored one
      c(j) = c(j) + v(i)*M(k)
    enddo !i
  enddo !j

这篇关于下三角矩阵-矢量积的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆