我真的不明白这两个程序之间的区别。 Fortran可以正常工作并提供正确的结果
81 88 221 335 778 595 1559 1759
虽然C ++程序失败并给出了这个结果:
81 181 221 335 778 595 1559 1759
正如您所看到的,第二个元素是不同的,正确的值是使用Fortran代码获得的值:
MODULE MSR
IMPLICIT NONE
CONTAINS
subroutine amuxms (n, x, y, a,ja)
real*8 x(*), y(*), a(*)
integer n, ja(*)
integer i, k
do 10 i=1, n
y(i) = a(i)*x(i)
10 continue
do 100 i = 1,n
do 99 k=ja(i), ja(i+1)-1
y(i) = y(i) + a(k) *x(ja(k))
99 continue
100 continue
return
end
END MODULE
PROGRAM MSRtest
USE MSR
IMPLICIT NONE
INTEGER :: i
REAL(KIND(0.D0)), DIMENSION(8) :: y, x= (/3,4,0,1,6,8,1,19/)
REAL(KIND(0.D0)), DIMENSION(19) :: AA = (/11,22,33,44,55,66,77,88,0,12,31,32,41,42,43,56,67,78,87/)
INTEGER , DIMENSION(19) :: JA = (/10,11,11,13,16,17,18,19,20,2,1,2,1,2,3,6,7,8,7/)
CALL amuxms(8,x,y,aa,ja)
WRITE(6,FMT='(8F10.2)') (y(I), I=1,8)
END PROGRAM
为什么这个C ++程序会产生不同的结果?
# include <iomanip>
# include <iostream>
std::vector<double> dot( int size, std::vector<double> x,
std::vector<double> aa, std::vector<int> ja)
{
std::vector<double> y(x.size());
for(auto i = 0; i < size ; i++ )
y.at(i) = aa.at(i) * x.at(i);
std::vector<double> jja(ja.size());
for(auto i=0 ; i < size ; i++ )
{
auto k = ja.at(i)-1;
do
{
y.at(i) += aa.at(k) * x.at(ja.at(k)-1) ;
k++;
}
while(k < ja.at(i+1)-1) ;
}
return y;
}
int main()
{
std::vector<double> x = {3,4,0,1,6,8,1,19};
std::vector<double> aa = {11,22,33,44,55,66,77,88,0,12,31,32,41,42,43,56,67,78,87};
std::vector<int> ja = {10,11,11,13,16,17,18,19,20,2,1,2,1,2,3,6,7,8,7};//{6,7,7,8,10,3,1,1,3};
std::vector<double> y = dot(x.size(), x , aa , ja);
for(auto& i : y)
std::cout << i << ' ' ;
std::cout << std::endl;
}
奇怪的是,如果我给这个载体:
AA = {1.01,4.07,6.08,9.9,0.,2.34,3.12,1.06,2.2};
JA = {6,7,7,8,10,3,1,1,3};
X = {0.,1.3,4.2,0.8}
两个程序都给出了正确的结果:
9.828 5.291 25.536 17.16
答案 0 :(得分:1)
改为:
# include <iomanip>
# include <iostream>
std::vector<double> dot( int size, std::vector<double> x,
std::vector<double> aa, std::vector<int> ja)
{
std::vector<double> y(x.size());
for(auto i = 0; i < size ; i++ )
y.at(i) = aa.at(i) * x.at(i);
std::vector<double> jja(ja.size());
// for(auto j=0 ; j< jja.size() ; j++)
// jja.at(j) = ja.at(j)-1 ;
for(auto i=0 ; i < size ; i++ )
{
//for(auto k=ja.at(i) ; k< ja.at(i+1)-1 ; k++ )
//auto k = ja.at(i)-1;
//std::cout << ja.at(i) <<" " << i << std::endl ;
// do
for(auto k=ja.at(i)-1 ; k< ja.at(i+1)-1 ; k++ )
{
y.at(i) += aa.at(k) * x.at(ja.at(k)-1) ;
}
// while(k < ja.at(i+1)-1) ;
}
return y;
}
int main()
{
std::vector<double> x = {3,4,0,1,6,8,1,19};
std::vector<double> aa = {11,22,33,44,55,66,77,88,0,12,31,32,41,42,43,56,67,78,87};
std::vector<int> ja = {10,11,11,13,16,17,18,19,20,2,1,2,1,2,3,6,7,8,7};//{6,7,7,8,10,3,1,1,3};
std::vector<double> y = dot(x.size(), x , aa , ja);
for(auto& i : y)
std::cout << i << ' ' ;
std::cout << std::endl;
}
给出:81 88 221 335 778 595 1559 1759
使用vector作为数组索引时请注意!