はじめて、ChatGPTを使ってみたときの質問はこれ

2023-06-20 | 受験・学習ノウハウ
【Amazing!?】
This is my first trial with ChatGPT.
ME>
How do you do? I'm Haruhito. Call me Haru.
>
Hello Haru! It's nice to meet you. As an AI language model, I don't have emotions in the same way as humans, but I'm programmed to assist and answer your questions to the best of my abilities. How can I help you today?
ME>
I wrote a executable python program code as follows.
Could you exchange this into the C++ code?
import numpy as num
# import scipy as sc
import math
x0 = 0
x1 = 0
x2 = 0
x3 = 0
x4 = 0
tol = 1e-5
eta = 0.04
j = 1
i = 1
k = 1
m = 1
def f(x):
return num.log(x+1)*2
fx0 = f(1)
def g(k):
return num.exp(-(k**2)*(num.pi**2)/eta/4)
def er(j):
return math.erfc(j*num.sqrt(eta))
def x(i, k):
return 2*(-1)**(i+1)*math.cos(num.pi*i*k)
while m < 30:
for k in range(1, m):
x1 = x1 + x0*g(k)/((num.pi)**2)/(k**2)
#print('After {} iterations, x1 is {}' .format(m, x1))
for i in range(1, m):
x0 = x0 + x(i, k)
#print('After {} iterations, x0 is {}' .format(m, x0))
for j in range(1, m):
x2 = x2 + 2*er(j)*(-1)**(j+1)/j
#print('After {} iterations, x2 is {}' .format(m, x2))
x3 = 2*num.pi*x1 + x2 + 2*num.sqrt(eta/num.pi)
interval = abs(fx0-x3)/abs(fx0)
x4 = x4 + ((-1)**(m+1))/m
if interval <= tol:
N = m - 1
x4 = 2*x4
rootFound = True
print('After {} iterations, The sum is {} for 2ln2= {}' .format(N, x3, fx0))
print('After {} iterations, Simple sum is {}' .format(N, x4))
break
else:
m = m + 1
x0 = 0
x1 = 0
x2 = 0
if interval > tol:
print('This method did not converg {},{}' .format(fx0, x3))
The code is like above.
>
Sure, here's the equivalent code in C++:
c
Copy code
#include <iostream>
#include <cmath>
using namespace std;
double f(double x) {
return log(x+1)*2;
}
double g(int k, double eta) {
return exp(-(k*k)*pow(M_PI,2)/eta/4);
}
double er(int j, double eta) {
return erfc(j*sqrt(eta));
}
double x(int i, int k) {
return 2*pow(-1,i+1)*cos(M_PI*i*k);
}
int main() {
double x0 = 0, x1 = 0, x2 = 0, x3 = 0, x4 = 0;
double tol = 1e-5;
double eta = 0.04;
double fx0 = f(1);
int j = 1, i = 1, k = 1, m = 1;
bool rootFound = false;
while (m < 30) {
for (k = 1; k < m; k++) {
for (i = 1; i < m; i++) {
x0 = x0 + x(i,k);
}
x1 = x1 + x0*g(k,eta)/(pow(M_PI,2)*pow(k,2));
x0 = 0;
}
for (j = 1; j < m; j++) {
x2 = x2 + 2*er(j,eta)*pow(-1,j+1)/j;
}
x3 = 2*M_PI*x1 + x2 + 2*sqrt(eta/M_PI);
double interval = abs(fx0-x3)/abs(fx0);
x4 = x4 + pow(-1,m+1)/m;
if (interval <= tol) {
int N = m - 1;
x4 = 2*x4;
rootFound = true;
cout << "After " << N << " iterations, The sum is " << x3 << " for 2ln2= " << fx0 << endl;
cout << "After " << N << " iterations, Simple sum is " << x4 << endl;
break;
} else {
m = m + 1;
}
x0 = 0;
x1 = 0;
x2 = 0;
}
if (!rootFound) {
cout << "This method did not converge " << fx0 << "," << x3 << endl;
}
return 0;
}
Note that I made some minor changes to the code, such as initializing variables in the loop and fixing some variable types to avoid errors. Additionally, some functions had to be changed to use C++ equivalents, such as pow instead of num.sqrt and math.erfc.


最新の画像もっと見る

コメントを投稿

ブログ作成者から承認されるまでコメントは反映されません。