1
$\begingroup$

For example,

Given a list of symbolic expressions:

ClearAll[a,o,t];
myexpr = 
{
1 - t + 2 a[1] + (2 - t + 2 a[1]) (-1 + t + a[1] + o[1]) + (1 + a[1]) (-1 + t + a[1] + o[1])^2,
-1 + t + a[1] + o[1]
}

Given conditions on those symbols:

mycond = (1 <= t <= 3 && a[1] == t && o[1] > 3 - 2 t + a[1]) || (t > 3 && a[1] == t && o[1] >= 0)

And given "transformation" function foo,

How can I "apply" those conditions to the expression and generate examples?

By "apply", I mean:

Every case of conditions separated by || is handled on its own.

In every case, for subcondition of form X == Y, all X get replaced by Y (or the other way around) so only one of the two remains in the expressions.

In case of a >=,> or <=,< subconditions, those set up the relative bounds for symbols, lets call them boundLow,boundHigh. If no bound is set, the extreme bounds are used.

The final bounds depend on given extreme bounds boundMin,boundMax which restrict bounds boundLow,boundHigh by having final bounds for every symbol: Max[boundLow,boundMin],Min[boundHigh,boundMax].

That is, the desired output is in this example: (After == substitutions in myexpr and myconds)

{Table[foo[myexpr],{t,Max[1,boundMin],Min[3,boundMax]},{o[1],Max[4-t,boundMin],boundMax}],
Table[foo[myexpr],{t,Max[4,boundMin],boundMax},{o[1],Max[0,boundMin],boundMax}]}

Where we have tables of examples generated by applying the myconds (which consist of two cases separated by || and thus we have two tables) to myexpr.


In the above example, I manually did the == replacements with /. -> and manually set the bounds - how can I automate this process of "applying" conditions to expressions to generate examples?


Motivation

The above example is an actual output from an algorithm I'm trying to make that solves a problem I'm trying to solve - in this question, I'm wondering how can I generate examples from such outputs, to analyze them.

$\endgroup$
1
$\begingroup$

Not sure but this sounds like a case for FindInstance:

FindInstance[#, Variables[myexpr]] & /@ (List @@ mycond)

{{{t -> 3, a[1] -> 3, o[1] -> 5}}, {{t -> 87, a[1] -> 87, o[1] -> 0}}}

(myexpr /. FindInstance[#, Variables[myexpr]]) & /@ (List @@ mycond)

{{{454, 10}}, {{2649237, 173}}}

({myexpr /. #, #} &@ FindInstance[#, Variables[myexpr]]) & /@ (List @@ mycond)

{{{{454, 10}}, {{t -> 3, a[1] -> 3, o[1] -> 5}}}, {{{2649237, 173}}, {{t -> 87, a[1] -> 87, o[1] -> 0}}}}

If you wish you can use third argument of FindInstance to generate multiple examples:

Grid[(Transpose[{myexpr /. #, #}] & @  FindInstance[#, Variables[myexpr], 3]) & 
 /@ (List @@ mycond), Dividers -> All ]

enter image description here

$\endgroup$
  • $\begingroup$ This is helpful, but I was looking into tabulating all instances in an interval [Max[boundLow,boundMin],Min[boundHigh,boundMax]] shared for all variables (variables being in the Integers) like in my example. I see FindInstance has a domain specification, but I don't see if I can use it to generate all instances where variables are restricted by the given interval (Like in my Table example)? $\endgroup$ – Vepir Jul 24 at 15:08
  • $\begingroup$ Accepted the answer as it helped me figure out how to solve the example problem of tabulating solutions (mentioned in my previous comment) via FindInstance- I can use what you present here after splitting the conditions beforehand into intervals so that each contains one solution. $\endgroup$ – Vepir Jul 28 at 14:11
  • $\begingroup$ @Vepir, thank you for the accept. $\endgroup$ – kglr Jul 28 at 18:13

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.