hello, i barely know how to use python and i need to write a cubic spline interpolation code in python for my programming assignment . I have C++ code but i know nothing about c++ so can someone help me to convert that code to python code?
this is a very important and i don't have eneugh time to learn how to do it by myself, so i really need someone's help.

the exmple in the attachement is the same example in the c++ code.

 #include<iostream>
#include<cmath>
/*Modele  pour la construction des fonction g(x) de cubiquesSpline */
using namespace std;
#define  n  5

double S[n+1];
double x[]={1,2,3,4,5,6};
double y[]={1.0,3.1,1.5,2.5,3.4,1.98};
int main()
{
int i;
double a[n+1],b[n+1],c[n+1],d[n+1],h[n+1];
double *ai, *bi,*ci,*di;
double m;
ai=new double[n+2];
bi=new double[n+2];
ci=new double[n+2];
di=new double[n+2];
cout.setf(ios::fixed);
cout.setf(ios::showpoint);

for(int i=0;i<=n;i++)
{
h[i]=x[i+1]-x[i];    
}
ai[0]=0;
bi[0]=1.0;
ci[0]=0.0;
di[0]=0.0;
ai[n]=0;
bi[n]=1.0;
ci[n]=0.0;
di[n]=0.0;
cout <<"x[i] \t      y[i]"<<endl;
for(int i=0;i<=n;i++)
{
    cout << x[i]<<"\t"<<y[i]<<endl;
}
for(int i=1;i<=n-1;i++)
{
ai[i]=h[i-1];
bi[i]=2.0*(h[i]+h[i-1]);
ci[i]=h[i];
di[i]=6.0*((y[i+1]-y[i])/h[i]-(y[i]-y[i-1])/h[i-1]);
}

for(int i=1;i<=n;i++)
{
m=ai[i]/bi[i-1];
bi[i]=bi[i]-m*ci[i-1];
di[i]=di[i]-m*di[i-1];
}
S[n]=di[n]/bi[n];
for(int i=n-1;i>=0;i--)
{
S[i]=(di[i]-ci[i]*S[i+1])/bi[i];
}
cout<<"g(i)   \t   a[i] \t   b[i] \t   c[i] \t   d[i]"<<endl;  
for (i=0;i<=4;i++)
{
a[i]=(S[i+1]-S[i])/6.0;
b[i]=S[i]/2;
c[i]=(y[i+1]-y[i])-(2.0*S[i]+S[i+1])/6.0;
d[i]=y[i];
 cout<<i<<"\t"<<a[i]<< "\t"<<b[i]<<"\t"<<c[i]<<"\t"<<d[i]<< endl ;
} 
delete [] ai,bi,ci,di;
 return 0;   
}

Recommended Answers

All 5 Replies

the assignment is about writing cubic slpine interpolation code in python without using Scipy.
i couldn't upload any attachements, if you can help email me so i can send you the equation and the example.
iamabibiliophile@gmail.com

Assignments can be for work which I take it you are IRL and won't assume otherwise.

OK, since it's homework then let's tackle this two ways but first a question. What good is that c++ code since it's not your code?
If you use that, then you won't be able to explain "how does this work?" to your prof.

Moving past that, converting from c++ can't be that hard. Take it line by line and work out the code.

  1. Line 5 could be as simple as writing n = 5 or as complex as https://www.oreilly.com/library/view/python-cookbook/0596001673/ch05s16.html depending on if you want n to be immutable. Only YOU can make that determination. For me n = 5 would get the job done but remember I'm from the real world where we must "get it done" in terms of cost and time.

Now what line of code stops you now?

  1. Let's say you take the equation and you want to create code. That's a fine way to learn that sort of work. Again this is your homework so I can't do that for you. Besides you must understand the math plus Python to get this done which may be the entire purpose of this assignment. Also it's a fine way to test if the students have learned what has been given so far.

1st about the c++ code, my former professor give it to me since i was not taught that much to be able to code a whole method by myself.

2 days ago i didnt know what cubic spline is but now i need to write a 1000 words essay about the method itself and a python code that i barely know how to write a very basic code in it, and for your information i know it is my assignment and i need to do it but i already working on an other assignment "10 pages cheat sheets" the deadline of the both is after tomorrow. also my finals will start this saturday and i have to attending classes the whole week from 8am to 4pm. is it that hard to help someone really need help?
you said " it's a fine way to test if the students have learned what has been given so far" i agree but dont ask me to write a whole code if you barely taught me how to write an array. i know you have nothing to do with that but i just explining my circumstances that force me to ask for a help.
i am really gonna appreciate your help.

If anyone did the conversion for you then any questions by the prof would be unanswerable from what I read above. I think you understand that.

Some students pay others to do their homework or have others complete such on the web, they eventually have problems later as well, they can't pass the final or such.

I don't mind giving up ideas on how to convert or what I would do at work (please note my first reply.) But in the end it's your work you must show and understand.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.