The Origin Forum
File Exchange
Try Origin for Free
The Origin Forum
Home | Profile | Register | Active Topics | Members | Search | FAQ | Send File to Tech support
 All Forums
 Origin Forum for Programming
 Forum for Origin C
 apply user defined function on dataset

Note: You must be registered in order to post a reply.
To register, click here. Registration is FREE!

Screensize:
UserName:
Password:
Anti-Spam Code:
Format Mode:
Format: BoldItalicizedUnderlineStrikethrough Align LeftCenteredAlign Right Horizontal Rule Insert HyperlinkUpload FileInsert Image Insert CodeInsert QuoteInsert List
   
Message:

* HTML is OFF
* Forum Code is ON
Smilies
Smile [:)] Big Smile [:D] Cool [8D] Blush [:I]
Tongue [:P] Evil [):] Wink [;)] Clown [:o)]
Black Eye [B)] Eight Ball [8] Frown [:(] Shy [8)]
Shocked [:0] Angry [:(!] Dead [xx(] Sleepy [|)]
Kisses [:X] Approve [^] Disapprove [V] Question [?]

 
Check here to subscribe to this topic.
   

T O P I C    R E V I E W
CRTBT-UBT 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
4   L A T E S T    R E P L I E S    (Newest First)
CRTBT-UBT 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
Mike Buess 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 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 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

The Origin Forum © 2020 Originlab Corporation Go To Top Of Page
Snitz Forums 2000