Differentiate an infix formal language functions - algorithm

I have a source file like (without loss of generality (only to image a possible syntax)):
function a()
return g // global variable without any internal structure exactly
end
function b(x, y)
local z = x * y
return z + 1
end
function c(z, t)
return b(z * z, a())
end
// ...etc
I want to defferentiate any function WRT to some variable.
All the formal parametres we can treat as a functions with unknown at derive time internal structure.
If I stand correct further, then the following is truth (for depending symbols ' is part of symbol, for global variables is operator during substitute time stage (def: g{g} is one, but g{y} is zero)):
function a'()
return g';
end
function b'(x, y, x', y')
local z' = x' * y + x * y'
return z' + 0
end
But what to do with last function? Namely, with actual parameters in substitution of function b?
Is there any ready to use implementations of general algorithm to work with the above? What to do with higher order derivatives (especially interesting, how to handle the formal parameters)? Are there any other possible unclear cases?

I would suggest having your parameters be symbolic expressions that know how to respond to derivatives, and having all operations take functions and return functions. Then you will get a final expression that knows how to be represented as a derivative. Furthermore you can do things like partial derivatives at a later point because you have the symbolic expression.
For a real example of what I mean, see http://www.elem.com/~btilly/kelly-criterion/js/advanced-math.js for a library that I wrote to solve a calculus problem in JavaScript, and search for "Optimize if requested" in the source for http://www.elem.com/~btilly/kelly-criterion/betting-returns2.html to see how I used it. See http://www.elem.com/~btilly/kelly-criterion/ for an explanation of why I was writing that code.
In that example I, of course, was not working from infix notation. But that is a standard parsing problem that I think you know how to solve.

Related

How to result invalid LogicalExpression in Dafny?

Consider the following dafny function below:
function method unpair(n: nat): (nat, nat)
{
var x,y :| n == (x+y)*(x+y+1)/2 + y;
return (x,y)
}
Given some natural number n, I would like to identify 2 natural numbers x and y which satisfy the equation (x+y)*(x+y+1)/2 + y. This is possible using Cantor's Pairing Function, but not sure I have the correct syntax for it, as dafny is throwing an error: "invalid LogicalExpression" on the return line. How can I resolve this error?
A function method is (perhaps confusingly) a function, with the only difference being that it is allowed to be called from non-ghost contexts. In any function (including function methods), we don't need to say return in Dafny. Instead, the body of the function is just the expression we want to return. So you should write
function method unpair(n: nat): (nat, nat)
{
var x,y :| n == (x+y)*(x+y+1)/2 + y;
(x,y)
}
At this point, you have a syntactically valid function.
Dafny then complains about several semantic issues. First, there are a few errors about "not satisfying the constraints of type nat". You can fix those by explicitly declaring x and y to have type nat, like this:
function method unpair(n: nat): (nat, nat)
{
var x:nat,y:nat :| n == (x+y)*(x+y+1)/2 + y;
(x,y)
}
At this point, Dafny reports one additional error, which is that it cannot prove that there always exist such an x and y. This is a more fundamental problem. You will need to convince Dafny (probably using a separate lemma) that such numbers always exist.

How to overload arithmetic operators (+, *, -, /) for functions in c++?

I would like to implement a numerical integral whose integrand is evaluated at quadrature points. Therefore something like: integral(domain, f), where domain is indeed the domain where I want to integrate and f is the function to integrate. f is only a function of the Point p (quadrature points) inside the domain and can have vector values (scalar is a particular case).
Since the function f can be, in general, a combination of different functions, I wonder how to overload arithmetic operators for functions.
I already found this Implementing multiplication operator for mathematical functions C++
but it does not cover my question, because the Function returns only x, while In my case I would like to have different Functions which can return a more complex function of x.
So, let f_1,...f_N be different functions which have the same return type, for example a std::array<double,M> with given length M, and which receive the same input Point p, i.e for I=1,...,N:
std::array<double,M> f_i(Point p)
{ std::array<double,M> x;
\\ compute x somehow depending on i
return x;}
Then I would like to create f as a combination of the previous f_1,...f_N, e.g. f=f_1 *f_2+(f_3*f_4)*f_5... (here the operations are meant to be component wise).
In this way I could evaluate f(p) inside integral(domain, f), obtaining for each quadrature point exactly:
f_1(p) *f_2(p)+(f_3(p)*f_4(p))*f_5(p)...
Edit:
I know I have to use functors and not simple functions (which I used just to state the problem), but I am not able to figure out how for this purpose.
Any hint?
Thank you

Mathematica transformation rules with trig functions

I'm an occasional Mathematica user and I am trying to transform an expression from spherical to Cartesian coordinates.
My function is defined as:
g[theta_, phi_] := Cos[phi](Sin[theta])^2 Sin[phi]
I'm hoping to transform that function using the following rules:
Sin[theta]Sin[phi] -> x
Cos[theta]-> y
Sin[theta]Cos[phi]-> z
in order to get the result:
zx
Here is the code I'm using to do that:
g[theta, phi] //. {Sin[theta]Sin[phi] -> x, Cos[theta] -> y, Sin[theta] Cos[phi] -> z}
And the result I get is:
Cos[phi] Sin[phi] Sin[theta]^2
So no transformation occurred.
Is there a function or an option I could add to help Mathematica figure out that the transformation is possible?
Thanks!
Perhaps this will be sufficient
Assuming[Sin[theta]Sin[phi]==x&&Cos[theta]==y&&Sin[theta]Cos[phi]==z,
Simplify[Cos[phi]Sin[theta]^2 Sin[phi]]]
which instantly returns
x z
That doesn't show you the steps or rules it used to arrive at that result, but because it considered x z to be "simpler" than your trig expression the evaluation process went in that direction.
There is a slightly more compact way of doing the same thing, if that matters. Simplify can accept a second argument which are the things which are assumed to be true during the process of simplification. Thus
Simplify[Cos[phi]Sin[theta]^2 Sin[phi],
Sin[theta]Sin[phi]==x&&Cos[theta]==y&&Sin[theta]Cos[phi]==z]
will give you exactly the same result

Using AND or OR logic gates to construct a 4-variable function

I'm a software developer, and I'm trying to model a function using only AND or OR gates. I remember having similar subjects in my undergraduates, but I don't recall it. f(x,y,z,w) is a function of four variables, and gets True when AT LEAST two of the variables gets true. How can I visually construct it using only AND or OR gates?
UPDATE: I think f= xy+xz+xw+yz+yw+zw if I'm correct!
The following expression is logically what you want:
(xy) + (xz) + (xw) + (yz) + (yw) + (zw)
x (y + z + w) + y (z + w) + (zw)
Note that you don't need to check for cases of three or four TRUE values, because they are already included in the check for two TRUE values.
I represent AND gates with scalar multiplication, and OR gates using the addition operator (+). Note that when you wire up the actual circuit you might even be able to simplify even more than I have by reusing pieces (e.g. z + w).

How to rearrange a function y = f[x] into x = g[y]

I have a differential equation A*dx/dt + B(y-y0) = 0
Where x is a very complicated function of y.
How can I use Mathematica to rearrange y to get a function x in order to solve this?
Thanks
There are two or three different problems here that you might be asking:
Option 1: The subject line
First, if you really do have a function f[x] defined and you want to rearrange it, you would be doing something like this:
f[x_]=2+x+x^2;
Solve[y==f[x],x]
However, even here you should notice that inverse functions are not necessarily unique. There are two functions given, and the domain of each is only for y>=7/4.
Option 2: Solving a DE
Now, the equation you give is a differential equation. That is not the same as "rearranging a function y=f[x] into x=g[y]" because there are derivatives involved.
Mathematica has a built-in differential-equation solver:
DSolve[a y'[t] + b (y[t] - y0) == 0, y[t], t]
That will give you a function (in terms of constants $a,b,y_0$) that is the answer, and it will include the unspecified constant of integration.
Your system seems to refer to two functions, x(t) and y(t). You cannot solve one equation for two variables, so it is impossible to solve this (Mathematica or otherwise) without more information.
Option 3: Rearranging an expression
As a third alternative, if you are trying to rearrange this equation without solving the differential equation, you can do that:
Solve[a x'[t] + b(y[t]-y0)==0,x'[t]]
This will give you $x'(t)$ in terms of the other constants and the function $y(t)$, but in order to integrate this (i.e. to solve the differential equation) you will need to know more about y[t].

Resources