Re: How to remove unneeded constraints
- To: mathgroup at smc.vnet.net
- Subject: [mg87977] Re: How to remove unneeded constraints
- From: Albert Retey <awnl at arcor.net>
- Date: Mon, 21 Apr 2008 06:39:33 -0400 (EDT)
- References: <fueeo7$b82$1@smc.vnet.net>
Hi, > The problem I am working on is a pretty large problem, which I am solving by dividing it into a lot of subproblems. > > As such, I have a large list of constraints that apply to the entire problem, but are not relevant for each individual subproblem. > > This becomes a problem when I use Refine, as it takes a very long time when you have a lot of conditions, even though the conditions don't pertain to the problem. Example: > In[25]:= Table[Timing@Refine[p>q,Map[Subscript[x,#]>0&,Range[i]]],{i,1000,5000,1000}] > Out[25]= {{0.547,p>q},{1.797,p>q},{3.875,p>q},{8.594,p>q},{10.468,p>q}} > > And it only gets worse. > > However, for each individual call to Refine I make, I only need a small subset of the total constraints. > > So what I want to do is something like this: > expr = some expression of n different variables; > cond = DeleteCases[totalConstraints, all cases which do not contain a variable from expr]; > result = Refine[expr,cond]; > > I have no idea how to construct a pattern powerful enough to do what is required for the DeleteCases call, though. > > All of the variables are of the form Subscript[s,_,_,_] or Subscript[b,_,_] and the conditions can also contain expressions of several variables. > > I'd be most grateful for any help. Thank you. here is a toy problem I used to test the code below, hope it isn't to simple: expr = Total@ Table[Random[]*ToExpression["x" <> ToString[i]]^i, {i, 1, 100, 5}] conditions = Table[ToExpression["x" <> ToString[i - 1]] < Random[] < ToExpression["x" <> ToString[i]], {i, 1, 100}] This will construct a pattern that matches all variables in expr (maybe you need some fine tuning to extract only those symbols which represent variables in your problem): varpattern = Alternatives @@ Cases[expr, _Symbol,Infinity] Now you can use that pattern to extract the relevant conditions: DeleteCases[conditions, _?(FreeQ[#, varpattern] &)] If you haven't seen this kind of pattern yet, search for PatternTest in the documentation. In cases where a pattern where such a PatternTest using a pure function is necessary, I usually prefere to use Select instead of DeleteCases/Cases, but in this case it isn't much clearer or shorter: Select[conditions, MemberQ[#, varpattern, Infinity] &] hth, albert