views:

132

answers:

4

I'm helping a professor for some lectures in OO and numerical algorithm in particular problem like to find the root of a function, minimization, interpolation, ... the problem is that he wants to teach at the same time OO with C++. I think that usual algorithms like these are implemented using procedural programming, at least you can use functional or generic programming but it is a forcing to use OO, it is unnatural. What do you think? Please give a constructive answer!

The course is designed for second year physics student. At the first year they have an introduction to C language.

+2  A: 

It strikes me as contrived.

That being said, the obvious thing to do is represent a function as a Function class with methods to, e.g. find roots.

At least, these methods could cache the results so that they only get run once for a given function and the client code doesn't have to explicitly store the return values. I guess there is some value to it.

aaronasterling
yes, I had thought about functors, but I think it is not enought to teach OO. It's only a little example.
wiso
+4  A: 

Professors are like functions. The best ones only have one thing they try to do. Yours is trying to do two things: teach OOP and teach non-trivial mathematical methods.

IMO your prof should forget about trying to cram a root-finding algorithm in to an OO box, and instead focus on the classics. Just teach OO, not OO and math.

John Dibling
"Professors are like functions. The best ones only have one thing they try to do." You made me laugh out loud!
sbi
@sbi: I hope you weren't drinking a Coke at the time.
John Dibling
@John: My glass of Port was already empty when that happened. Would have been a waste...
sbi
A: 

It might be a bit more advanced than what you're looking for (it's from a first-year graduate course), but the assignment here could be a good starting point.

The idea is to implement the conjugate gradient method, but in a more abstract way than is usually presented. Instead of explicitly forming the matrix for the equation that you're trying to solve, you can consider the matrix multiplication A*b as a linear operator acting on an object. The same algorithm can be reused for different data types (e.g. 2D or 3D grids) as long as them implement the same basic interface.

Later on in the course, the code is extended to deal with more complicated numerical methods (GMRES); the final assignment is reusing the conjugate gradient code with a preconditioner to speed up the convergence of the method.

celion
A: 

It strikes me as bizarre that anyone would think that OOP should not be considered for programming the type of applications that computational physicists might be concerned with. There is extensive literature on OO for numerical methods Object-Oriented Implementation of Numerical Methods is just one of the books on the topic. Scientific and Engineering C++ remains one of the best introductions to the language and its application in a specific domain despite its age

Physics, and the kind of maths that physicists use, is replete with objects -- or at least it is entirely sensible to analyse the physical world into objects. Have a look at how the Smalltalk class hierarchy deals with Magnitudes (of which Numbers are a sub-class).

I think your professor is to be applauded for trying to teach both an approach to programming and a particular language within the context of their application to a specific domain.

Finally, look at the code which implements methods in your favourite OO application -- you will see that a lot of it looks procedural. And I don't even want to get into the topic of how you can implement functional-programming with C++.

EDIT: I completely forgot to mention BOOST which implements a variety of scientific/engineering type programs in C++.

High Performance Mark