Message Boards Message Boards

Maximizing area overlap of regions subject to any affine transform?

Hi! I'm working on using NMaximize to maximize the area overlap between two regions, where NMaximize is maximizing over the space of Affine transformations applied to one of the two regions. I've run into an issue where NMaximize seems to pass the dummy variable names (a,b,c) into the function to be maximized rather than numerical values, which produces an error. Here's my code so far.

j = 3;
regionPreAtOrigin = 
  TransformedRegion[groupedCellRegionListPre[[j]], 
   TranslationTransform[-groupedCellCentroidListPre[[j]]]];
regionPostAtOrigin = 
  TransformedRegion[groupedCellRegionListPost[[j]], 
   TranslationTransform[-groupedCellCentroidListPost[[j]]]];

affineFunction[a_, b_, c_, d_] := 
 Area[RegionIntersection[
    TransformedRegion[regionPreAtOrigin, 
     AffineTransform[{{{a, b}, {c, d}}, {0, 0}}]], 
    regionPostAtOrigin]]/(Area[
     TransformedRegion[regionPreAtOrigin, 
      AffineTransform[{{{a, b}, {c, d}}, {0, 0}}]]] + 
    Area[regionPostAtOrigin]/2)
affineFunction[1, 0, 0, 1]
Plot[affineFunction[x, 0, 0, 1], {x, -5, 5}]
NMaximize[affineFunction[x, 0, 0, 1], {x}]
NMaximize[{affineFunction[t1,t2,t3,t4],t1*t4-t2*t3>0}, 
{t1,t2,t3,t4}] 

groupedCellRegionListPre[[j]] and post are both valid regions, the output of "affineFunction[1,0,0,1]' (the identity transformation) outputs ~0.5, which is what I expect, and I know the function works because the plot works fine. I also know it has maximum, from the afforementioned plot. I also added the restriction to NMaximize such that it won't apply any transformations with non-positive determinants, which would produce errors. Despite all this, here is the output for the code above:Output of the code above.

I aborted the code because if I leave it running it crashes mathematica. So that's odd as well. Thanks for any help y'all can provide!

POSTED BY: Kaden Tro
2 Replies

Hi Kaden,

I cannot verify your example without the data. But there is a general problem calling numerical functions in nested functions: You have to pass numbers to their argument sequence and not expressions.

In each function call of a numerical function , try to wrap the arguments by an Evaluation. The same applies for functions with attribut Hold like Plot or Integrate.

Plot[Evaluate[ affineFunction[x, 0, 0, 1] ] , {x, -5, 5}]
NMaximize[Evaluate[affineFunction[x, 0, 0, 1] , {x}]
NMaximize[Evaluate[{affineFunction[t1,t2,t3,t4],t1*t4-t2*t3>0}], 
{t1,t2,t3,t4}] 

Sometimes even Evaluate does not work properly as intended e.g. in defining functions.

I got used to prepare evaluated functions by evaluating a List and applying the Function head to the result.

f =    Function@@{  D[ JacobiP[3,2,3, #],#]  }
g =  Function@@{ {x},  D[ JacobiP[3,2,3, x], x]  }

Regards Roland

POSTED BY: Roland Franzius

Thank you so much Roland! You're absolutely right, that ended up being the problem. I found a note about it at the bottom of mathematica's documentation for NMaximize (I feel like an idiot for not finding it earlier), and there they offer a solution, which was just checking whether the passed objects into AffineFunction were numbers. So the fixed code just has the following definition for AffineFunction

affineFunction[a_?NumericQ, b_?NumericQ, c_?NumericQ, d_?NumericQ] :=

This seems to have worked.

POSTED BY: Kaden Tro
Reply to this discussion
Community posts can be styled and formatted using the Markdown syntax.
Reply Preview
Attachments
Remove
or Discard

Group Abstract Group Abstract