| Author |
Topic  |
|
|
CRTBT-UBT
France
Posts |
Posted - 03/19/2007 : 11:45:31 AM
|
Origin Version (Select Help-->About Origin): 7.5 Operating System: WinXP
Hello,
I suppose that this is a quite easy question, but as I couldn't find the answer I will ask it anyway.
I defined the following function:
float polynome(float paras[6], float x) { float sum = 0.0; for(int ii=0; ii<=5; ii++) { sum += paras[ii] * pow(x, ii); }; return sum }
Now I have two problems:
a) I don't succeed to call it from the script window, as I don't know how to pass an array as argument.
b) I'd like to apply this function on a dataset in a C-function, so I tried to add these lines to another C-function (wks, fColNum, toColNum , ds1 and ds2 are of course properly initialised:
float Tcparas[6] = {9.294e-1 , 1.387e-1 , -6.930e-3 , 2.569e-4, -5.725e-6, 5.301e-8}; ds1.Attach(wks,fColNum); ds2.Attach(wks,toColNum); ds2 = polynome(Tcparas, ds1);
Now this won't compile, and I suppose this is because my polynome function does expect a double, and not a dataset. What can I change for this to work? Of course I could just loop over the dataset, but I suppose that there is a more elegant solution.
Thanks for help, Johannes |
|
|
Mike Buess
USA
3037 Posts |
Posted - 03/19/2007 : 1:50:02 PM
|
Hi Johannes,
a) There are no arrays in LabTalk so use a dataset instead.
paras = {9.294e-1 , 1.387e-1 , -6.930e-3 , 2.569e-4, -5.725e-6, 5.301e-8}; // create the dataset paras in script window
You can then pass the dataset name to your Origin C function...
float polynome(string dsName, float x) { Dataset ds(dsName); if( !ds ) return 0; float sum = 0.0; for(int ii=0; ii<ds.GetSize(); ii++) { sum += ds[ii] * pow(x, ii); } return sum; }
b) I see no elegant way to do this in Origin C. Ironically, this works fine in LabTalk...
col(B)=poynome("paras",col(A));
Mike Buess Origin WebRing Member |
 |
|
|
CRTBT-UBT
France
Posts |
Posted - 03/19/2007 : 2:07:49 PM
|
First of all thanks for your reply.
But in the second part you misunderstood me:
I don't want to pass the dataset as the first argument, but as the second one. In fact in spite of passing a float as second argument and getting a float in return, I'd like to pass a dataset and getting a dataset in return. If I'm correct this is perfectly possible with the built-in functions, but I didn't succeed to do it with my custom functions. Is it possible to code functions in a "polymorphic" way, so that it accepts both, floats and datasets/arrays?
Greets from France, Johannes |
 |
|
|
Mike Buess
USA
3037 Posts |
Posted - 03/19/2007 : 2:21:24 PM
|
quote: In fact in spite of passing a float as second argument and getting a float in return, I'd like to pass a dataset and getting a dataset in return.
That's exactly what the LabTalk expression col(B)=polynome("paras",col(A)) does. It applies polynome() to all rows in col(A) and stores the results in col(B). The same thing cannot be done in Origin C as far as I know. You must explicitly apply it to all elements of the "x" dataset.
quote: Is it possible to code functions in a "polymorphic" way, so that it accepts both, floats and datasets/arrays?
Yes, that is possible. Both of the following versions of your function can be defined at the same time (called overloading). // This can be used both in LabTalk and OriginC float polynome(string strparas, float x) { Dataset ds(strparas); if( !ds ) return 0; float sum = 0.0; for(int ii=0; ii<ds.GetSize(); ii++) { sum += ds[ii] * pow(x, ii); } return sum; } // This can only be used in Origin C void polynome(vector paras, Dataset dsx, Dataset& sum) { sum.SetSize(dsx.GetSize()); for(int jj=0; jj<dsx.GetSize(); jj++) { sum[jj] = 0.0; for(int ii=0; ii<paras.GetSize(); ii++) { sum[jj] += paras[ii]*pow(dsx[jj],ii); } } } // Example of the second usage void test() { Worksheet wks = Project.ActiveLayer(); Dataset dsx(wks,0); Dataset sum(wks,1); vector paras = {9.294e-1 , 1.387e-1 , -6.930e-3 , 2.569e-4, -5.725e-6, 5.301e-8}; polynome(paras,dsx,sum); }
Mike Buess Origin WebRing Member
Edited by - Mike Buess on 03/19/2007 2:24:10 PM
Edited by - Mike Buess on 03/19/2007 4:07:10 PM |
 |
|
|
CRTBT-UBT
France
Posts |
Posted - 03/20/2007 : 04:53:44 AM
|
thanks, this works fine.
I'm probably to spoilt from programming in python, where a lot of things are easier and more elegant... by the way, some adapted version of python could be a good substitute for your LabTalk language (which I personally would class far from being elegant and readable...)
Greets, Johannes |
 |
|
| |
Topic  |
|
|
|