Comments (6)
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.
Sweet. I'll have to think about why that works and what Block[...]
is doing here.
Thanks again!
from mathematicaclasslessobjects.
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.
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.
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.
And… it works. Thanks again!
from mathematicaclasslessobjects.
Related Issues (6)
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from mathematicaclasslessobjects.