Gradient of a function at many points, referencing a struct

Click For Summary
The discussion centers on computing the gradient of a smooth function using a struct that contains a single member variable `double x`. The main challenge is how to evaluate the function at two different points, x(i) and x(i+1), without modifying the existing function definition that accepts the struct. Participants note that since the struct only holds one value, two separate instances of the struct are necessary to access both x values. Suggestions include using an array of structs to store multiple x values, allowing the function to be called with different instances for each evaluation. However, the original poster expresses reluctance to change the function's definition, which complicates the solution. Clarifications on the programming language and function declaration are requested to provide more targeted assistance. Overall, the consensus is that without altering the struct or function signature, the task cannot be accomplished as intended.
CAF123
Gold Member
Messages
2,918
Reaction score
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 :))
 
Technology news on Phys.org
CAF123 said:
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:
  • Like
Likes CAF123
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.
 
jedishrfu said:
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.
 
Perhaps the array approach would work then. I’ve seen that done when return geographic coordinates like lat=latlon[0] and lon=latlon[1].
 
jedishrfu said:
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.
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.
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.
 
I guess I am 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.
 
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?
 
@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.
 
  • Like
Likes Mark44
  • #10
jedishrfu said:
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.
jedishrfu said:
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.
RPinPA said:
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.
 
  • Like
Likes jedishrfu
  • #11
Mark44 said:
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:
CAF123 said:
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.
 
  • #12
RPinPA said:
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:

Similar threads

  • · Replies 11 ·
Replies
11
Views
2K
Replies
6
Views
2K
  • · Replies 5 ·
Replies
5
Views
2K
  • · Replies 31 ·
2
Replies
31
Views
3K
  • · Replies 15 ·
Replies
15
Views
3K
  • · Replies 8 ·
Replies
8
Views
2K
  • · Replies 2 ·
Replies
2
Views
3K
  • · Replies 0 ·
Replies
0
Views
623
Replies
2
Views
1K
  • · Replies 6 ·
Replies
6
Views
7K