I would like some opinions reguarding a practice that I have been using.
In developing an object-oriented framework for successive quadratic
programming (SQP) in C++ I have migrated toward much weaker typing and
runtime checks using dynamic_cast. Let me give you an example of its use
and my defense for it.
I have and optimization algorithm that works with a matrix abstraction for
the gradients a(i) of a set of nonlinear constraints which I will call the
matrix A (abstract class MatrixWithOp). The optimization algorithm
performs BLAS like (i.e. y = op(A)*x) operations with this matrix A but is
must also perform a decomposition of the matrix (Matlab notation):
A' = [ C, N ]
where: C is square and nonsingular
The optimization algorithm has no idea what the internal structure of A is
so to perform this decomposition it uses a BasisSystem object through an
abstract interface. Now here is the weak typing problem. Both the
MatrixWithOp and BasisSystem interfaces are abstract classes. When the
optimization algorithm needs to perform the above decomposition and solve
for systems with C it goes through the BasisSystem interface passing a
reference to A (which is of type MatrixWithOp). In order for the concrete
BasisSystem subclass to perform these tasks efficiently it must know the
concrete type (or at least a lower level subclass) of the matrix A. Before
the optimization algorithm is run, an external entity configures it with
concrete types of the matrix A and its BasisSystem that are (hopefully)
compatible. However the concrete BasisSystem object must perform a cast to
convert from the abstract type to the concrete type (or at least a more
specific type). For some added runtime safety I use dynamic_cast rather
than static_cast because the external configuration object might have
gotten it wrong and dynamic_cast can also handle multiple inheritance.
This use of dynamic_cast is not a performance problem and any
configuration mistakes are therefore caught right away at runtime.
The above example is a case where an Abstract Factory (Gamma, 1994) is
used to create a set of related objects that must interact with each other
but must also know about each others concrete subclasses to an extent in
order to function.
Using this type of weak typing with dynamic_cast has made my framework
much easier to develop and extent but I am afraid that it might upset more
diehard C++ developers. The approach that I have taken has allowed my
client optimization algorithm to be completely ambiguous as to the
specifics of sparse matrix data structures and solvers but at the same
time has allowed for only constant time overheads which makes it
competitive with any Fortran implementation (efficiency in this respect
is critical). There are some Fortran library compatibility problems that
make this coupling between the matrix and BasisSystem subclasses
necessary.
What do you think about this use of weak typing in C++?
Also, I have passed my Ph.D. proposal and descriptions of this
object-oriented framework with be forth coming is the next couple of
months but the source code may have to wait.
--------------------------------------
| Roscoe Bartlett |
| Ph.D. student |
| Department of Chemical Engineering |
| Carnegie Mellon University |
| email: roscoe@andrew.cmu.edu |
--------------------------------------
--------------------- Object Oriented Numerics List --------------------------
* To subscribe/unsubscribe: use the handy web form at
http://oonumerics.org/oon/
* If this doesn't work, please send a note to owner-oon-list@oonumerics.org
This archive was generated by hypermail 2b29 : Wed Feb 20 2002 - 03:20:08 EST