Giter Site home page Giter Site logo

usage question 2: integrating a child object method once, not every time, to get a new function that can be called about mathematicaclasslessobjects HOT 6 CLOSED

jkuczm avatar jkuczm commented on August 16, 2024
usage question 2: integrating a child object method once, not every time, to get a new function that can be called

from mathematicaclasslessobjects.

Comments (6)

jkuczm avatar jkuczm commented on August 16, 2024

You could add special caching method to your object:

ancestor@cacheVAndR[] :=
    Block[{t},
        $self@v[t_] = ancestor[v[t], $self];
        $self@r[t_] = ancestor[r[t], $self];
    ]

Now let's define a child:

ClearAll[child]
DeclareObject[child, ancestor]

After changing members relevant to v[t] and r[t] you can cache their values:

child@a[t_] := t^2
child@v0 = 5;
child@r0 = 10;
child@cacheVAndR[]

After caching, evaluated v[t] and r[t] are saved as members of child object:

?? child
(*...
child[v[t_],_:child]=5+t^3/3
child[r[t_],_:child]=10+5 t+t^4/12
...*)

It might be useful to re-cache v[t] and r[t] each time r0, v0 or a[t_] changes.
This would be more complicated, but I can think about it, if you need it.

from mathematicaclasslessobjects.

ibeatty avatar ibeatty commented on August 16, 2024

Sweet. I'll have to think about why that works and what Block[...] is doing here.

Thanks again!

from mathematicaclasslessobjects.

ibeatty avatar ibeatty commented on August 16, 2024

How might I make v[t] and r[t] Listable? Can I use the same addAttributes[...] function as you suggested earlier? I thought about using that in the caching function, but that was for pure functions, and the caching function doesn't use pure functions.

from mathematicaclasslessobjects.

jkuczm avatar jkuczm commented on August 16, 2024

Block in cacheVAndR is a safeguard. Right hand sides of assignments inside this method are evaluated. It could happen that when cacheVAndR is called, t symbol has a value assigned, if so, without Block, this value would replace t symbol in definition of v and r.

This is not related to ClasslessObjects, it's a standard technique. See for example: Scoping in assigning a derivative.

As to Listable attribute:

ancestor@cacheVAndR[] :=
 With[
  {
   evaluatedV = ancestor[v[#], $self],
   evaluatedR = ancestor[r[#], $self]
   },
  $self@v[t_] := Function[Null, evaluatedV, Listable][t];
  $self@r[t_] := Function[Null, evaluatedR, Listable][t];
  ]

from mathematicaclasslessobjects.

jkuczm avatar jkuczm commented on August 16, 2024

After rethinking this, solutions with auxiliary pure function are a bit hacky.
Explicit definition for List arguments should be cleaner:

ancestor@v[l_List] := $self@v[#] & /@ l
ancestor@r[l_List] := $self@r[#] & /@ l
ancestor@cacheVAndR[] :=
    Block[{t},
        $self@v[t : Except[_List]] = ancestor[v[t], $self];
        $self@r[t : Except[_List]] = ancestor[r[t], $self];
    ]

from mathematicaclasslessobjects.

ibeatty avatar ibeatty commented on August 16, 2024

And… it works. Thanks again!

from mathematicaclasslessobjects.

Related Issues (6)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.