tags:

views:

317

answers:

5

Hi dudes, I have the next code in C++:

for (long i=0; i < num_iter ; i++)
{
    bp->bpgt(data[i%8], &data[i%8][3]);

    if( bp->mse(&data[i%8][3]) < thresh) 
        break;
}

where bpgt is a procedure, and mse is a function, thresh is a Double type, data is a bi-dimensional matrix of Double types.

void bpgt(double *in,double *tgt);
double mse(double *tgt);
double data[][4]={
     0,0,0,0,
     0,0,1,1,
 1,1,1,1 };

I've tried to pass it to Delphi code:

for i := 0 to FNum_Iter - 1 do begin
    FBPN.bpgt(FData[i mod 8], ^FData[i mod 8,3]);

    if FBPN.mse(@FData[i mod 8, 3]) < FThresh then
      Break;
end;

but I've failed, because I'm a newbie in C++ and I dont know to translate the "&" operator. May Someone help me?

Thanks in advance.

A: 

The & allows you to send the address of the variable into the function, rather than it's value. This call by reference allows the function to alter the value of the variable. I don't know the equivalent Delphi mechanism, but you want to pass the reference of your variable there, or change the procedure to a function, return the value and store it in the original variable.

Traveling Tech Guy
Gerry
+2  A: 

I would translate

void bpgt(double *in,double *tgt);

as

procedure bpgt(var in:double; var tgt: double)

Well, something like that, my Delphi is a bit rusty....

That way, in bpgt, you can change the value of tgt (and in).

Your call of bpgt will be

FBPN.bpgt(FData[i mod 8], FData[i mod 8,3]);

In fact Delphi's var (call by reference) is quite often usable as an exact functional equivalent of C/C++'s passing of pointers.

fvu
Yeah, you've got the semantics right. You can simplify the function header a little bit by saying "var in, tgt: double". That doesn't work in C++, but it will in Delphi. The one caveat is that a C * parameter (pass-by-pointer) can set the pointer to NULL, whereas with a Delphi **var** parameter (pass-by-reference), the pointer can never be **nil** on a value type, such as numbers. You have to be aware of this when translating a C header that you're going to call from Delphi, because it could break a program invariant if the C routine sets the pointer to NULL.
Mason Wheeler
Billiardo Aragorn
Billiardo, please read Mason's and my answer again. You don't need the @ or whatever anymore. If your final goal is to learn something about neural networks it's probably simpler to get some Delphi NN code and play around with that. Like this one, found on Torry's http://www.torry.net/authorsmore.php?id=6870
fvu
A: 

You are close, try this:

for i := 0 to FNum_Iter - 1 do begin
    FBPN.bpgt(FData[i mod 8], FData[i mod 8,3]);

    if FBPN.mse(FData[i mod 8, 3]) < FThresh then
      Break;
end;

Sorry, but I don't have delphi here to compile and try it by myself.

eKek0
+2  A: 

This is based on your reply to Fvu's answer. If they're arrays, that can complicate things, since C doesn't have real arrays, just pointers sprinkled with a light coating of powdered syntactic sugar. The solution depends on whether you're translating this completely into Delphi, or trying to write a Delphi routine that will work with C code.

If you're doing pure Delphi, declare an array type, like so

type
   TDoubleArray = array[0..length] of double; //or "array of double" for a dynamic array

Then declare the parameter of the function as var Data: TDoubleArray. If this is a dynamic array, have your for loop go from 0 to high(Data);

If this needs to work with C:

Because C doesn't have real arrays, you need to do some extra work. Declare your type like this:

type
   TCDoubleArray = array[0..65535] of double;
   PCDoubleArray = ^TCDoubleArray;

Any sufficiently large value will work for the upper bound of the array. We're not going to use all of it anyway. It just has to be bigger than you'll ever use. Make the parameter type a PCDoubleArray, which corresponds to double *. You'll need to pass an extra parameter, which tells you where the end point of the array is. Have your for loop go from 0 to the end point, and access it by saying something like FData^[i mod 8].

Mason Wheeler
A: 
Mick