2
$\begingroup$

Often when I'm writing a project in Mathematica, the number of arguments I must provide to functions spins out of control.

I need a sort of object-oriented encapsulation approach. I've already had a go at doing this in Java, and now believe than an object-oriented language is not well suited to my needs.

Example:

Here I fetch data for the geodesic of a test particle in a pseudo-Riemannian manifold. The user must supply the metric in matrix form, the initial positions and velocities, and other miscellaneous things:

NGeodesicData[( {
    {(1 - (1/#2)) &, 0 &, 0 &},
    {0 &, -(1 - 1/#2)^-1 &, 0 &},       (*Matrix of real functions*)
    {0 &, 0 &, (-#2^2) &}
   } ) , 
    {{0, 50, 0.}, {Null, 0., .0008}},   (*Initial position and velocity*)
    {1, True, True},                    (*Miscellaneous*)
  2500, .01, 1]                         (*Numerical integration settings*)

All of these arguments are necessary, and I would like to structure them in a more manageable format.


Specifically, I would like a head called TangentVector which consists of:

  1. An n x n matrix of real functions of n variables each. (The matrix of real functions)
  2. A list of two n-dimensional vectors. (the initial position and velocity; a tangent vector)

I am used to object-oriented languages where I can program a constructor in which I can perform checks, throw exceptions or warnings if I need to, and store a couple of fields based on the input. Basically, I'm looking to do the same thing here.


How can I create such a TangentVector structure, and make a constructor for it?

I believe I have already found a solution to complicated pattern-matching (on the inputs to the constructor) here.

$\endgroup$
5
  • $\begingroup$ So are you after the pattern for matching? Or a OO-implementation? Going with standard Mathematica paradigms might be less hassle (there are several threads on OO around though). $\endgroup$
    – Yves Klett
    Jul 1, 2015 at 6:29
  • $\begingroup$ @YvesKlett - I have solved the pattern-matching problem; the main thing is that I need a constructor which can take input (I know how to do the pattern matching) and create a TangentVector structure based on that. The reason that I want this structure is so that other functions can take in TangentVectors as inputs. The best way I know of to do this is to create a custom head. I will be happy with any encapsulation approach though. $\endgroup$
    – Myridium
    Jul 1, 2015 at 6:32
  • $\begingroup$ Possible duplicates: (4636), (16869), (33884), (73960) $\endgroup$
    – Mr.Wizard
    Jul 1, 2015 at 11:11
  • $\begingroup$ Suggest you look at Association $\endgroup$
    – m_goldberg
    Jul 1, 2015 at 14:00
  • $\begingroup$ You might find some of answers to [this question[(mathematica.stackexchange.com/q/990/3066) of interest, although I think the addition of Association in V10 makes some the answers obsolescent. $\endgroup$
    – m_goldberg
    Jul 1, 2015 at 14:07

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Browse other questions tagged or ask your own question.