Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add parse_free method to Problem class, add test fubction #303

Merged
merged 4 commits into from
Jan 21, 2025

Conversation

Peter230655
Copy link
Contributor

@Peter230655 Peter230655 commented Jan 21, 2025

I added parse_free(solution) to the Problem class, and added a test function comparing the new method to the one existing in utils. This should take care of issue #203

Comment on lines 636 to 654
len_states = n * N
len_specified = q * N

free_states = free[:len_states].reshape((n, N))

if q == 0:
free_specified = None
else:
free_specified = free[len_states:len_states + len_specified]
if q > 1:
free_specified = free_specified.reshape((q, N))

if variable_duration:
free_time_interval = free[-1]
free_constants = free[len_states + len_specified:-1]
return free_states, free_specified, free_constants, free_time_interval
else:
free_constants = free[len_states + len_specified:]
return free_states, free_specified, free_constants
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
len_states = n * N
len_specified = q * N
free_states = free[:len_states].reshape((n, N))
if q == 0:
free_specified = None
else:
free_specified = free[len_states:len_states + len_specified]
if q > 1:
free_specified = free_specified.reshape((q, N))
if variable_duration:
free_time_interval = free[-1]
free_constants = free[len_states + len_specified:-1]
return free_states, free_specified, free_constants, free_time_interval
else:
free_constants = free[len_states + len_specified:]
return free_states, free_specified, free_constants
return parse_free(n, q, N, variable_duration)

This is indeed a nice feature. You can remove some code duplicaton by just calling the parse_free function.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks!
I thought of this but I was not sure util's parse_free is automatically available.
Does it not have to be 'imported'?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, it needs to be imported, which you can see is done already at the top of this module.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Now I noticed.
So I am 'allowed' to use util's parse_free?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, it is even preferable as it reduces code duplication, i.e. same code in multiple locations.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I already changed as per your suggestion.
Somehow, I feel 'more comfortable' with my clumpsy test function. I can't really say why. I know yours is correct.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, it is even preferable as it reduces code duplication, i.e. same code in multiple locations.

Thanks!
This really helped my understanding on such packages!

@@ -1566,3 +1566,125 @@ def test_for_algebraic_eoms():
)

assert excinfo.type is ValueError


def test_prob_parse_free():
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This test can be made way simpler. You can take some inspiration from test_parse_free. You should be able to relatively easily just create some random equations which give a certain n, q, r, N.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

To create a simple equation of n symbols, one can use something like sum(sm.symbols(f"p:{n}")).

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If you'd like it to have a physical meaning. You can just imagine a set of n independent point masses that can only move in one direction by example.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is VERY clever indeed!

@Peter230655
Copy link
Contributor Author

Simplified completely as per Timo's suggestion!
I left my test function, somehow I feel more comfortable with it - likely dumb feeling.

states, controls, constants = prob.parse_free(initial_guess)
assert(np.all(states == statesu))
assert(np.all(controls == controlsu))
assert(np.all(constants == constantsu))
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The better testing for floating point comparison is np.testing.test_allclose().

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry, it is np.testing.assert_allclose().

@Peter230655
Copy link
Contributor Author

I used assert(np.allclose(a, b))
Would np.assert_allclose(a, b) still be better?

@Peter230655
Copy link
Contributor Author

Question:
is np.testing.assert_allclose(a, b) better than assert(np.allclose(a, b)) because it is all numpy and hence faster?
Thanks!

@moorepants
Copy link
Member

This looks good. Thanks!

@moorepants moorepants merged commit 8134983 into csu-hmc:master Jan 21, 2025
21 checks passed
@Peter230655
Copy link
Contributor Author

Peter230655 commented Jan 21, 2025

This looks good. Thanks!

Thanks! :-)

@Peter230655 Peter230655 deleted the prob-parse-free branch January 21, 2025 20:38
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants