存储数学函数的值

时间:2018-02-11 19:00:23

标签: c++ multidimensional-array numerical-methods numerical numerical-computing

我想在向量/数组中存储数学函数的值(或者还有其他可能性来避免存储过程)?

x

这是一个示例函数,每个y(x)(没有零)具有理论值。但现在出现了问题:我真的不知道如何在另一个函数中使用func1() - double func2() { double a, x; double integral = a * x * pow(y, -4); return integral; // for the integration i'm going to use the gsl-library. } 的值?

function1

随后我想绘制-2& x。是否有一个库可以创建一个包含yfrom selenium import webdriver from selenium.webdriver.common.by import By from selenium.webdriver.support.ui import WebDriverWait from selenium.webdriver.support import expected_conditions as EC driver = webdriver.Chrome() wait = WebDriverWait(driver, 10) driver.get("http://www.incometaxindia.gov.in/Pages/utilities/exempted-institutions.aspx") while True: for elem in wait.until(EC.presence_of_all_elements_located((By.CSS_SELECTOR,"[id^='arrowex']"))): print(elem.text) try: wait.until(EC.presence_of_element_located((By.ID, "ctl00_SPWebPartManager1_g_d6877ff2_42a8_4804_8802_6d49230dae8a_ctl00_imgbtnNext"))).click() wait.until(EC.staleness_of(elem)) except: break driver.quit() 值表的文件?

2 个答案:

答案 0 :(得分:0)

如果您只想创建一个包含要由其他程序处理的(x,y)输出的文件,您只需使用文件流库:

#include <cmath>
#include <fstream>

//Move the variable x into the function definition an a function argumen
double func1(double x)
{
   double y = 1/std::sqrt(x);
   return y; 
}

//Similar for a and x here
double func2(double a, double x)
{   
   double integral = a * x * std::pow(y, -4);
   return integral;
   // for the integration i'm going to use the gsl-library. 
}


int main() {
 //...
 std::ofstream file1("func1-output.txt"), file2("func2-output.txt");
 // make sure the file is open
 for( int x = 1; x < 100; x++ ) {
    file1 << x << "\t" << func1(x) << std::endl;
    file2 << x << "\t" << func2(1,x) << std::endl;
 }
 //....

 file1.close(); file2.close();
 return 0;
}

答案 1 :(得分:0)

如果您要求为给定的公式处理xy的多个值,则假设您有相同数量的xy元素,您可以使用std::transform使用等式处理每个输入,并将结果存储在第三个向量ans中。

#include <iostream>   ///< std::cout, std::endl
#include <vector>     ///< std::vector, std::back_inserter
#include <algorithm>  ///< std::transform

int main()
{
    std::vector<double> x = { 1,2,3,4,5,6 };
    std::vector<double> y = { 2,4,6,8,10,12 };
    std::vector<double> ans;

    // apply the function for every x and y
    // record each result in ans
    std::transform(x.begin(), x.end(), y.begin(), std::back_inserter(ans),
                   [](double x, double y) { return x*y - ((x+y)/2) + 1; });

    for( const auto& a : ans )
    {
        std::cout << a << std::endl;
    }

    return 0;
}

以下示例输入和等式的输出结果为:

1.5
6
14.5
27
43.5
64