Gradient of a function at many points, referencing a struct

In summary: he's using a function that takes an arbitrary struct type as its parameter and just wants to evaluate func at the x member of that type, in which case he could do something like this:double slope = (func@x1 - func@x)/(x1-x) ;
  • #1
CAF123
Gold Member
2,948
88
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
  • #2
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
  • #3
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.
 
  • #4
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.
 
  • #5
Perhaps the array approach would work then. I’ve seen that done when return geographic coordinates like lat=latlon[0] and lon=latlon[1].
 
  • #6
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.
 
  • #7
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.
 
  • #8
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?
 
  • #9
@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:

1. What is the gradient of a function?

The gradient of a function is a mathematical concept that represents the direction and magnitude of the steepest increase or decrease of the function at a given point.

2. How is the gradient of a function calculated?

The gradient of a function can be calculated by taking the partial derivatives of the function with respect to each of its variables and arranging them in a vector.

3. What does it mean to calculate the gradient of a function at many points?

Calculating the gradient of a function at many points involves finding the gradient at multiple points within the function's domain. This allows for a more comprehensive understanding of the function's behavior and can provide insights into its overall shape and trends.

4. What is a struct in reference to calculating the gradient of a function?

A struct, short for "structure", is a data type in programming that allows for the grouping of multiple variables under one name. In the context of calculating the gradient of a function at many points, a struct can be used to store the coordinates of these points and their corresponding gradient values.

5. Why is calculating the gradient of a function at many points important?

Calculating the gradient of a function at many points is important because it provides a more detailed understanding of the function's behavior. This information can be used to optimize the function, identify critical points, and make predictions about its behavior in different scenarios.

Similar threads

  • Programming and Computer Science
Replies
11
Views
1K
  • Programming and Computer Science
Replies
6
Views
915
  • Programming and Computer Science
Replies
5
Views
1K
  • Programming and Computer Science
Replies
31
Views
2K
  • Programming and Computer Science
Replies
8
Views
1K
  • Programming and Computer Science
Replies
15
Views
2K
  • Programming and Computer Science
Replies
5
Views
2K
  • Programming and Computer Science
Replies
12
Views
1K
  • Programming and Computer Science
Replies
1
Views
749
  • Programming and Computer Science
Replies
2
Views
911
Back
Top