在std c ++中编写外部函数 [英] writing an external function in std c++
问题描述
我一直在盯着编写一个计算两个双宽四向量内部产品的程序。
拉里认为我很好地开始使用以下来源来填充
向量:
#include< vector>
#include<算法>
#include< iterator>
#include< iostream>
#include< math.h>
int main(){
std :: vector< doublefour_vector;
for(double i = 0.0; i< 4.0 ; i ++)
four_vector.push_back(sqrt(i));
std :: cout.precision(16);
std :: copy(four_vector.begin(),four_vector.end(),
std :: ostream_iterator< double>(std :: cout," \ n") );
返回0;
}
我如何模仿以下fortran来源:
程序vector2
隐含无
整数索引,i
整数,参数:: some_kind_number = selected_real_kind(p = 16)
real(kind = some_kind_number),dimension(4):: vec_a,vec_b
real(kind = some_kind_number):: res
index = 4
我i = 1,索引
vec_a(i)= i **。5
vec_b(i)=( - 1)*(i ** 2)
结束了
res = dot_product(vec_a,vec_b)
写(*,*)vec_a,vec_b
写(*,*)res
结束程序vector2
! gfortran2 -o vector2 vector2.f95
! vector2> text55.txt 2> text56.txt
//结束源继续评论
,但外部计算内部产品除外。我没有机会获得正确的
,所以我会饶恕你。屏幕截图
此处: http://zaxfuuq.net/ c ++ 5.jpg
为了模仿它,我相信适当的c ++内部产品将会是b / b负25美元。
-
Gerry Ford
Er hat sich georgiert。 Der Spiegel,2008,sich auf Chimpy Eins komma null
beziehend。
I''ve been pecking away at writing a program that will calculate the inner
product of two double-width four-vectors.
Larry thinks I''m well started with the following source to populate a
vector:
#include <vector>
#include <algorithm>
#include <iterator>
#include <iostream>
#include <math.h>
int main() {
std::vector<doublefour_vector;
for (double i=0.0; i<4.0; i++)
four_vector.push_back(sqrt(i));
std::cout.precision(16);
std::copy(four_vector.begin(), four_vector.end(),
std::ostream_iterator<double>(std::cout, "\n"));
return 0;
}
How do I imitate the following fortran source:
program vector2
implicit none
integer index, i
integer, parameter :: some_kind_number = selected_real_kind (p=16)
real (kind = some_kind_number), dimension(4):: vec_a, vec_b
real (kind = some_kind_number) :: res
index = 4
do i = 1, index
vec_a(i)= i**.5
vec_b(i)= (-1)*(i**2)
end do
res = dot_product(vec_a, vec_b)
write (*,*) vec_a, vec_b
write (*,*) res
end program vector2
! gfortran2 -o vector2 vector2.f95
! vector2 >text55.txt 2>text56.txt
//end source continue comment
, except making the inner product calculated externally. I have zero chance
of getting it correct, so I''ll spare you the flailing attempt. Screenshot
here: http://zaxfuuq.net/c++5.jpg
To imitate it, I believe the appropriate c++ inner product would be around
negative 25.
--
Gerry Ford
"Er hat sich georgiert." Der Spiegel, 2008, sich auf Chimpy Eins komma null
beziehend.
推荐答案
Gerry Ford写道:
Gerry Ford wrote:
我一直在琢磨编写一个程序来计算两个双宽四向量的内部产品。
Larry认为我很好地开始使用以下来源来填充
向量:
#include< vector>
#include< algorithm>
#include< iterator>
#include< iostream>
#include< ; math.h>
int main(){
std :: vector< doublefour_vector;
for (double i = 0.0; i< 4.0; i ++)
four_vector.push_back(sqrt(i));
std :: cout.precision(16 );
std :: copy(four_vector.begin(),four_vector.end(),
std :: ostream_iterator< double>(std :: cout," \ n"));
返回0 ;
}
我如何模仿以下fortran来源:
program vector2
含义没有
整数索引,i
整数,参数:: some_kind_number = selected_real_kind(p = 16)
real(kind = some_kind_number),维度(4):: vec_a,vec_b
real(kind = some_kind_number):: res
index = 4
do i = 1,index
vec_a(i)= i **。5
vec_b(i)=(-1)*(i ** 2)
结束了
res = dot_product(vec_a,vec_b)
写(*,*)vec_a,vec_b
写(*,*)res
结束程序vector2
! gfortran2 -o vector2 vector2.f95
! vector2> text55.txt 2> text56.txt
//结束源继续评论
,但外部计算内部产品除外。我有零
的机会
得到它是正确的,所以我将免除你的抨击尝试。屏幕截图
此处: http://zaxfuuq.net/ c ++ 5.jpg
为了模仿它,我相信适当的c ++内部产品将会是b / b负25美元。
I''ve been pecking away at writing a program that will calculate the inner
product of two double-width four-vectors.
Larry thinks I''m well started with the following source to populate a
vector:
#include <vector>
#include <algorithm>
#include <iterator>
#include <iostream>
#include <math.h>
int main() {
std::vector<doublefour_vector;
for (double i=0.0; i<4.0; i++)
four_vector.push_back(sqrt(i));
std::cout.precision(16);
std::copy(four_vector.begin(), four_vector.end(),
std::ostream_iterator<double>(std::cout, "\n"));
return 0;
}
How do I imitate the following fortran source:
program vector2
implicit none
integer index, i
integer, parameter :: some_kind_number = selected_real_kind (p=16)
real (kind = some_kind_number), dimension(4):: vec_a, vec_b
real (kind = some_kind_number) :: res
index = 4
do i = 1, index
vec_a(i)= i**.5
vec_b(i)= (-1)*(i**2)
end do
res = dot_product(vec_a, vec_b)
write (*,*) vec_a, vec_b
write (*,*) res
end program vector2
! gfortran2 -o vector2 vector2.f95
! vector2 >text55.txt 2>text56.txt
//end source continue comment
, except making the inner product calculated externally. I have zero
chance
of getting it correct, so I''ll spare you the flailing attempt. Screenshot
here: http://zaxfuuq.net/c++5.jpg
To imitate it, I believe the appropriate c++ inner product would be around
negative 25.
如何使用
std :: inner_product(vec_a.begin(),vec_a.end(),vec_b.begin() ,double());
来自< numericheader?
Best
Kai-Uwe Bux
What about using
std::inner_product( vec_a.begin(), vec_a.end(), vec_b.begin(), double() );
from the <numericheader?
Best
Kai-Uwe Bux
Gerry Ford写道:
Gerry Ford wrote:
我一直在琢磨写一个程序将计算两个双宽四向量的内积。
拉里认为我很好地开始使用以下来源来填充
vector:
#include< vector>
#include< algorithm>
#include< iterator> ;
#include< iostream>
#include< ; math.h>
int main(){
std :: vector< doublefour_vector;
for (双i = 0.0; I< 4.0; i ++)
four_vector.push_back(sqrt(i));
std :: cout.precision(16);
std :: copy(four_vector.begin(),four_vector.end(),
std :: ostream_iterator< double>(std :: cout," \ n")) ;
返回0;
}
如何模仿以下fortran来源:
程序vector2
隐式无
整数索引,i
整数,参数:: some_kind_number = selected_real_kind(p = 16)
real(kind = some_kind_number),dimension(4):: vec_a,vec_b
real(kind = some_kind_number):: res
index = 4
我i = 1,索引
vec_a(i)= i **。5
vec_b (i)=( - 1)*(i ** 2)
结束了
res = dot_product(vec_a,vec_b)
写(*,*)vec_a,vec_b
写(*,*)res
结束程序vector2
! gfortran2 -o vector2 vector2.f95
! vector2> text55.txt 2> text56.txt
//结束源继续评论
,但外部计算内部产品除外。我没有机会让它变得正确,所以我会饶恕你的甩尾尝试。
这里的截图: http://zaxfuuq.net/c++5.jpg
为了模仿它,我相信适当的c ++内部产品将是负25美元左右。
I''ve been pecking away at writing a program that will calculate the
inner product of two double-width four-vectors.
Larry thinks I''m well started with the following source to populate a
vector:
#include <vector>
#include <algorithm>
#include <iterator>
#include <iostream>
#include <math.h>
int main() {
std::vector<doublefour_vector;
for (double i=0.0; i<4.0; i++)
four_vector.push_back(sqrt(i));
std::cout.precision(16);
std::copy(four_vector.begin(), four_vector.end(),
std::ostream_iterator<double>(std::cout, "\n"));
return 0;
}
How do I imitate the following fortran source:
program vector2
implicit none
integer index, i
integer, parameter :: some_kind_number = selected_real_kind (p=16)
real (kind = some_kind_number), dimension(4):: vec_a, vec_b
real (kind = some_kind_number) :: res
index = 4
do i = 1, index
vec_a(i)= i**.5
vec_b(i)= (-1)*(i**2)
end do
res = dot_product(vec_a, vec_b)
write (*,*) vec_a, vec_b
write (*,*) res
end program vector2
! gfortran2 -o vector2 vector2.f95
! vector2 >text55.txt 2>text56.txt
//end source continue comment
, except making the inner product calculated externally. I have zero
chance of getting it correct, so I''ll spare you the flailing attempt.
Screenshot here: http://zaxfuuq.net/c++5.jpg
To imitate it, I believe the appropriate c++ inner product would be
around negative 25.
你实际上并没有展示出fortran函数计算点
产品。 fortran源代码只是填充4个元素的2个数组
然后调用函数dot_product()。这是你想要编码的功能dot_product你想要代码,但是你没有显示它的原因。
在fortran中**是功率符号,在C和C ++中你可以使用pow
函数。虽然有些数字提升到了2的幂,但这个数字本身就是这么简单。将数字提高到0.5的幂是
取其平方根。所以基本上所有的fortran代码都填充了一个4个元素的数组,其平方根为1到4,第二个4
元素数组,方块为1到4 ,然后在它们上面调用函数
dot_product,它返回一个数字。
-
Jim Langston
ta ******* @ rocketmail.com
" Jim Langston" < ta ******* @ rocketmail.com写了留言
新闻:Jw ************** @ newsfe05.lga ...
"Jim Langston" <ta*******@rocketmail.comwrote in message
news:Jw**************@newsfe05.lga...
Gerry Ford写道:
Gerry Ford wrote:
>模仿它,我相信适当的c ++内部产品将是负25左右。
>To imitate it, I believe the appropriate c++ inner product would be
around negative 25.
你实际上并没有显示计算点的fortran函数>
产品。 fortran源代码只是填充4个元素的2个数组
然后调用函数dot_product()。这是你想要编码的功能dot_product你想要代码,但是你没有显示它的原因。
在fortran中**是功率符号,在C和C ++中你可以使用pow
函数。虽然有些数字上升到2的幂,但就像单数乘以数字一样简单。将数字提升到
.5的幂是取平方根。所以基本上所有的fortran代码都是
填充一个4个元素的数组,其平方根为1到4,
第二个4元素数组,方块为1到4 ,然后在它们上面调用
函数dot_product,它返回一个数字。
You are not actually showing the fortran function that calculates the dot
product. That fortran source code simply populates 2 arrays of 4 elements
then calls the function dot_product(). It is the function dot_product you
want to code, but you aren''t showing the souce for that.
In fortran ** is the power symbol, in C and C++ you can use the pow
function. Although some number raised to the power of 2 it''s just as
simple to multiply the number by itself. Raising a number to the power of
.5 is taking the square root of it. So basically all that fortran code is
filling one array of 4 elements with the square roots of 1 to 4, the
second 4 element array with the squares of 1 to 4, then calling the
function dot_product on them which returns a single number.
你在这一切上都非常正确。感谢您通过这个
源代码,感谢那些不太熟悉我的常见C扩展
选择的人。
dot_product没有fortran代码,因为它带有食物超过
。当我第一次意识到这一点时,这震惊了我。我知道c ++同样有,但不用担心。有了我们对标题的
,我们可以从头开始编写。
double float c ++ _ dot_product(array vec_a,array vec_b,integer index)
{
//声明本地变量
res = 0;
term = 0;
sum = 0;
for(i = 0; i< index; ++ i)
{
term = vec_a(i)* v_b(i);
sum = sum + term;
}
res = powl(总和,.5);
返回res;
}
如何为此调用者正确编码和外部功能
本身?
-
格里福特
Er hat sich georgiert。 Der Spiegel,2008,sich auf Chimpy Eins komma null
beziehend。
You''re prettymuch right on all this. I appreciate you talking through this
source for the benefit of those less familiar with my common C extension of
choice.
There isn''t fortran code for dot_product, as it comes with the food over
there. That shocked the heck out of me the first time I realized it. I was
given to understand that c++ had likewise, but not to worry. With what we
have for headers, we can write it from scratch.
double float c++_dot_product (array vec_a , array vec_b, integer index)
{
// declare local vars
res = 0;
term=0;
sum=0;
for (i = 0; i < index; ++ i)
{
term=vec_a(i)*v_b(i);
sum = sum + term;
}
res = powl(sum, .5);
return res;
}
How does one correctly code the caller for this and the external function
itself?
--
Gerry Ford
"Er hat sich georgiert." Der Spiegel, 2008, sich auf Chimpy Eins komma null
beziehend.
这篇关于在std c ++中编写外部函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!