## Introduction

This is really a parsing question. But the context is that given the right side of first order ode, I want to automatically determine the class of the RHS. It can be either separable, or linear. If it is none of these, then I can do something else.

For learning, I am writing a step-by-step ODE solver in Mathematica.

Currently the correct solver is explicitly called based on type of RHS, by first visually looking at it and seeing which form the RHS is.

So I have solver for linear RHS, one for separable RHS, one for exact RHS, etc…

But I want to automate this part, just like one calls DSolve and have the program figure which solver to call based on the type of the RHS.

But the problem it is very hard to parse the RHS and determine the form. I do not know how `DSolve`

does all of this.

## description of problem

Given a first order ODE in $ x$ and $ y(x)$ , the general form is $ y'(x)=f(x,y(x))$

So need a function, which takes in $ f(x,y(x))$ and return if it is linear, or separable or neither and also return back these parts.

$ f(x,y(x))$ is separable if it can be written as product of two functions, where one function depends only on $ x$ and the other function depends only on $ y(x)$ .

As in $ f(x,y(x)) = p(x) q(y(x))$ where $ p(x)$ depends on $ x$ only and $ q$ depends on $ y(x)$ only. Examples of separable are

$ f=x^2+x^2 y(x) = x^2 (1+y(x))$ . Here $ p=x^2$ and $ q=1+y(x)$

$ f=1+\cos(x)$ Here $ p=1+\cos(x)$ and $ q=1$

$ f=y(x)$ Here $ p=1$ and $ q=y(x)$

There are some which are tricky, such as

$ f=1−x+y^2(x)−x y^2(x)$ this is separable since it can be written as $ (1−x)(1+y^2(x))$ and in this case $ p=(1−x)$ and $ q=1+y^2(x)$

$ f(x,y(x))$ is linear if it can be written as sum of two functions as follows $ f(x,y(x))=p(x)+y(x) q(x)$ where $ p(x)$ and $ q(x)$ here depend on $ x$ only.

Examples of a linear $ f$ is $ f=x^2+x^3 y(x)$ , here $ p=x^2$ and $ q=x^3$ . This can’t be written as separable.

But $ f$ sometimes can be both linear and separable at same time. In this case, I want to classify it as separable. For example $ f=x^2+x^2 y(x)$ can be viewed as $ x^2(1+y(x))$ which is separable, or as linear where $ f=p(x)+y(x)q(x)$ with $ p=q=x^2$

Some RHS is not separable nor linear. These I will just flag as neither and worry about them later. For example $ f=\frac{1}{x+y}$ as this can’t be written in the form $ p(x)+y(x)q(x)$ and it also can’t be written as $ f=p(x) q(y(x))$

## Input for testing

Here are test inputs, and expected output to make it easier to answer this question.

`input={ x^2+x^2 y[x], 2 x+1, Cos[2 x], y[x], (x-1)/y[x], Log[1+y[x]^2], 1-x+y[x]^2-x y[x]^2, x^2+x^3 y[x], 1/(x+y[x]), (1+Sqrt[x])/(1+Sqrt[y[x]]), (1-x^2+y[x]^2-x^2 y[x]^2)/x^2, 2 x y[x]^2+3 x^2 y[x]^2, 3 Exp[2 x]+2 y[x], (5 Sqrt[x]-y)/ x, 2 x y[x]+3 x^2 Exp[x^2], Sqrt[1+x+y[x]^2] } `

And expected output for each one these inputs is

`out={ {x^2,1+y[x],"separable"}, {2x+1,1,"separable"}, {1,y[x],"separable"}, {Cos[2 x],1,"separable"}, {(x-1),1/y[x],"separable"}, {1,Log[1+y[x]^2],"separable"}, {(1-x),1+y[x]^2,"separable"}, {x^2,x^3,"linear"}, {"","","neither"}, {(1+Sqrt[x]),1/(1+Sqrt[y[x]]),"separable"}, {(1-x^2)/x^2,1+y[x]^2,"separable"}, {2 x+3 x^2,y[x]^2,"separable"}, {3 Exp[2 x],2,"linear"}, {5 Sqrt[x]/ x,-1/x,"linear"}, {3 x^2 Exp[x^2],2 x,"linear"}, {"","","neither"} } `

This is summarized in

For example, let the parsing function be called `parse`

then the call using the first example is

`parse[ x^2+x^2 y[x], x, y[x]] `

And this should return

` {x^2, 1 + y[x], "separable"} `

as in

`parse[f_, x_, y_] := Module[{p, q, class}, If[FreeQ[f, y[x]], p = f; q = 1; class = "seperable"; Return[{p, q, class}, Module] ]; {p, q} = f /. Times[p_, q_] :> {p, q}; (*etc....not working*) ] `

Basic parsing methods do not really work. For example doing something like

`expr=x^2+x^2 y[x] {p,q}=Simplify[expr]/.Times[p_,q_]:>{p,q} `

Works, but not here

`expr=1-t+y[t]^2-t y[t]^2 expr/.Times[p_,q_]:>{p,q} `

Using `Simplify`

sometimes help and sometimes now. So this approach is not robust to do what I want.

How does `DSolve`

manages to parse such complicated input and it figures which part of the expression is which? I am sure it does something much more advanced than basic pattern matching.

What is the general approach to write such a parser? Can just using pattern matching function work? Or it needs special parsing methods? May be a hint towards a general approach to solve this will be enough. Since trying to do this case by case is not getting to anywhere.