C/++/# Gradient of a function at many points, referencing a struct

  • Thread starter CAF123
  • Start date

CAF123

Gold Member
2,887
87
I want to compute the gradient of some smooth function at many points by taking the value of the function at point x(i) subtracted from the value of the function at point x(i+1) and then divide the result by ( x(i+1)-x(i) ). My function has a struct as an argument and within that struct I have a member variable `double x;' How can I pass the value x(i+1) to the function without introducing another struct? i.e how to evaluate

Code:
for (int i = 0; i<=100; i++) {
x=(100-10)*double(i)/100+10;
x1=(100-10)*double(i+1)/100+10;

double slope = (func@x1 - func@x)/(x1-x) ; }
if my function func has a struct `struct1` as an argument where struct1 is defined as follows:

struct struct1 {
...
double x;
...
}

ie. func = func(struct1). How to let the function to know to take the value x1 and not just x?
(I am using someone else's code to perform this task so I do not wish to reformat the whole code, i.e I could just trivially do (func(x1)-func(x))/(x1-x) but I would have to reformat the definition of func which is used many times for different purposes throughout the program. It has the struct instead as an argument, func(struct1) which has member x and I'd like to evaluate func not at x but at x1 so I can use it in the gradient definition. Hope my question makes sense :))
 
32,448
4,195
I want to compute the gradient of some smooth function at many points by taking the value of the function at point x(i) subtracted from the value of the function at point x(i+1) and then divide the result by ( x(i+1)-x(i) ). My function has a struct as an argument and within that struct I have a member variable `double x;' How can I pass the value x(i+1) to the function without introducing another struct?
You can't. From what you show, the structure has only a single x value in it, so to evaluate f at x[ i ] and at x[ i + 1], you need two of these structures.
CAF123 said:
i.e how to evaluate

C:
for (int i = 0; i<=100; i++) {
x=(100-10)*double(i)/100+10;
x1=(100-10)*double(i+1)/100+10;

double slope = (func@x1 - func@x)/(x1-x) ; }
I get what you're trying to say just above, but this last line isn't C code.
CAF123 said:
if my function func has a struct `struct1` as an argument where struct1 is defined as follows:

struct struct1 {
...
double x;
...
}

ie. func = func(struct1). How to let the function to know to take the value x1 and not just x?
Assuming one struct instance is named S1, then you can access its x member as S1.x .
If you want to access a different x value, you'll need another struct, but you would access it the same way, with the dot member access operator.
CAF123 said:
(I am using someone else's code to perform this task so I do not wish to reformat the whole code, i.e I could just trivially do (func(x1)-func(x))/(x1-x) but I would have to reformat the definition of func which is used many times for different purposes throughout the program. It has the struct instead as an argument, func(struct1) which has member x and I'd like to evaluate func not at x but at x1 so I can use it in the gradient definition. Hope my question makes sense :))
 
Last edited:
10,447
3,967
If this were java code you could use a Point2D object or subclass it to make your own PairXY class or just write a PairXY class with two float values in it.

Another approach is to just return a two element array of floats as is done in C and other languages.

For python, there are tuples or lists your choice.
 
32,448
4,195
If this were java code you could use a Point2D object or subclass it to make your own PairXY class or just write a PairXY class with two float values in it. Another approach is to just return a two element array of floats as is done in C and other languages.
It could be done in C++ with the pair template class, but I don't think this is an option here. I believe CAF123 is stuck with using the struct type that contains a single double member. I could be wrong on that, though.
 
10,447
3,967
Perhaps the array approach would work then. I’ve seen that done when return geographic coordinates like lat=latlon[0] and lon=latlon[1].
 
32,448
4,195
Perhaps the array approach would work then. I’ve seen that done when return geographic coordinates like lat=latlon[0] and lon=latlon[1].
I don't see that this is an option.
I am using someone else's code to perform this task so I do not wish to reformat the whole code, i.e I could just trivially do (func(x1)-func(x))/(x1-x)
but I would have to reformat the definition of func which is used many times for different purposes throughout the program.
The function he's using takes a specific struct type as its parameter, and that struct type has just a single value called x. Unless he rewrites the definition of func, he's stuck with the parameter list as it currently exists.
 
10,447
3,967
I guess im giving general advice. I suppose he could add the extra value to the struct and rebuild things unless the struct is used to data from a file.

What language is it? I don't recognize the @ as a reference operator.
 

RPinPA

Science Advisor
Homework Helper
427
230
Coming into this late, but I have a question. Your struct1 contains one x value. So do you have 100 instances of struct1, each of which with a different x value? Perhaps in an array of structs (I'm a little rusty in C but I'm pretty sure that's a thing you can do, perhaps involving a typedef).

If so, then why not call func(array_of_structs[n]) - func(array_of_structs[n-1]) or something like that? Reference the two struct instances that have the values of x that you are interested in?
 

strangerep

Science Advisor
2,934
708
@CAF123 :
This should be trivial, but... if it's still unresolved for you, then please tell me:

1) Which language are you using? C or C++ or .... ?

2) Show me the declaration of func(), (and also the implementation if you have it).

3) Show me the declaration of the struct.
 
32,448
4,195
I suppose he could add the extra value to the struct and rebuild things unless the struct is used to data from a file.
That's a possibility if he is able to modify the struct template.
What language is it? I don't recognize the @ as a reference operator.
The OP tagged the thread as C/C++/C#. I mentioned earlier that the line with @ in it wasn't syntactically correct in those languages. I think he wrote it as a sort of pseudocode.
If so, then why not call func(array_of_structs[n]) - func(array_of_structs[n-1]) or something like that?
He said in the OP that he didn't want to provide a new definition for func, so changing the argument list seems like it's out of question.
 

RPinPA

Science Advisor
Homework Helper
427
230
He said in the OP that he didn't want to provide a new definition for func, so changing the argument list seems like it's out of question.
But the OP said this:
if my function func has a struct `struct1` as an argument where struct1 is defined as follows:
So it seems that func is already defined to take a struct1 as an argument.
 
32,448
4,195
So it seems that func is already defined to take a struct1 as an argument.
That's why I believe that changing func's signature to use a different structure isn't a solution here, at least given the minimal information that the OP has provided.

Let's hold off further speculation until he clears things up.

@CAF123, please respond to @strangerep's questions in post #9, especially items 2 and 3.
strangerep said:
2) Show me the declaration of func(), (and also the implementation if you have it).
3) Show me the declaration of the struct.
His question 1 was which language is this, which I assume is either C or C++.

We are 12 posts into this thread, and still aren't sure about what you're asking.
 
Last edited:

Want to reply to this thread?

"Gradient of a function at many points, referencing a struct" You must log in or register to reply here.

Physics Forums Values

We Value Quality
• Topics based on mainstream science
• Proper English grammar and spelling
We Value Civility
• Positive and compassionate attitudes
• Patience while debating
We Value Productivity
• Disciplined to remain on-topic
• Recognition of own weaknesses
• Solo and co-op problem solving
Top