
    pj1                        d dl  d dlZd dlZd dlZej
                  dk\  rd dlZnd dlZeZd dlZ	 ej                  Z
d Zd Zd Zd Zd Zd_d	Zd
 Zd Zd Zd Zd Zd Zd Z G d de      Z G d d      Zd Z G d d      Z e       Zd Z G d d      Z  G d de!      Z" G d d      Z# G d d       Z$ G d! d"e#      Z% G d# d$e#      Z& G d% d&      Z' G d' d(      Z(d) Z)d`d*Z* G d+ d,      Z+ G d- d.      Z, G d/ d0e"e$      Z- G d1 d2e!      Z. G d3 d4e.e$      Z/ G d5 d6e0      Z1 G d7 d8e/e'      Z2 G d9 d:      Z3 G d; d<e/e'e3      Z4 G d= d>e/e'e(      Z5 G d? d@e5      Z6 G dA dBe5      Z7 G dC dDe5e3      Z8 G dE dFe/e'      Z9e2Z:e4Z;e7Z<e8Z=e9Z>e/Z?e Z@ G dG dHe       ZAdI Z@ G dJ dK      ZB G dL dM      ZC G dN dOeC      ZD G dP dQeC      ZE G dR dS      ZF eFeD      ZG eFeE      ZH G dT dU      ZI G dV dWeI      ZJ G dX dYeI      ZK G dZ d[eI      ZL G d\ d]eI      ZMd^ ZNy#  ej                  j                  Z
Y xY w)a    )*N)   r   c                 Z    t        | t              xs t        | t        j                        S N)
isinstanceintnpintegeras    D/DATA/.local/lib/python3.12/site-packages/casadi/tools/structure3.py
is_integerr   +   s    	Ac		6j2::66    c                 "    t        | t              S r   )r   strr   s    r   	is_stringr   .   s    	Ac	r   c                 F    t        | t              xs t        | t              S r   )r   listtupler   s    r   
isIterabler   1   s    	Ad		2z!E22r   c                 .    | D cg c]  }|g c}S c c}w r    )Lxs     r   lpackr   ;   s    1%1aqc1%%%s   
c            	          t        |       dk(  rg gS | d   D cg c]  }t        | dd   D ]  }||z   	  c}}S c c}}w Nr      )lencombine)argsr   bs      r   r    r    =   sH    Y\4K7?7aGT!"X,>qAE,>E7???s   :c           
          t        |       dk(  r	|rg ggS g gS t        | dd        }|r(t        | d         D cg c]  }t        |gg|       c}S t        t	        t        t        | d                     |      S c c}w r   )r   listindicesranger    r   r   )dimsnesttailis       r   r$   r$   C   s    Y\dVmTktABx D+0a>:>agseD!>::U4d1g/066 ;s   A:c               '      K   t        t        t        |             }dgt        |       z  }d}t	        |      r/	 t        ||          |dz   t        |       z  }t	        |      r.y y #  d||<   Y (xY ww)NTr   Fr   )r   mapiterr   anynext)r!   	iteratorsactiver)   s       r   intersperseItr1   P   sr     3tD>")6#d)&!F1 
Q#d)A 	FfQis"   6A4A( 	A4&A4(A1/A4c                  $    t        t        |        S r   )r   r1   )r!   s    r   intersperser3   [   s    
}d#
$$r   c                     t        |       dk(  rg S | gt        | d t        t        t        | d d d               j                  d      dz           z   S )Nr   Tr   )r   canonicalIndexAncestorsr   r+   r   index)inds    r   r6   r6   ^   sU    Xq[)
(-]S3tt95M0N0T0TUY0Z[\0\.])^_	__r   c                     | dk  r| |z   S | S )Nr   r   )r8   ss     r   	canonicalr;   b   s    1W7NJr   c                     t        d | D              r-t        j                  t        t	        t
        |             g       S | S )Nc              3   <   K   | ]  }t        |t                y wr   )r   r   ).0r)   s     r   	<genexpr>zvec.<locals>.<genexpr>i   s     'QAd	Q   )r-   builtinssumr   r+   veces    r   rC   rC   h   s1    'Q''<<SQZ(,,Hr   c                 H    t         j                  | |   |j                        S r   )casadireshapeshape)r   r)   s     r   correct_vector_indexingrJ   o   s    	!agg	&&r   c                       fd}|S )zi
    This decorator modifies a __getitem__/__setitem__ method such that it will always receive a tuple
  c                 >    t        |t              s|f} | |g| S r   r   r   )selfmbtr!   fs      r   properzproperGetitem.<locals>.properx   s&    c% FcT#tr   r   )rP   rQ   s   ` r   properGetitemrR   t   s     
-r   c                       e Zd Zd Zy)SafeDictc                     || vr1t        d|dt        t        | j                                           t        j                  | |      S )NzUnknown keyword 'z'. Available entries: )	Exceptionr   r   keysdict__getitem__)rN   ks     r   rY   zSafeDict.__getitem__   s@    }qTRVR[R[R]M^I_`aaD##r   N)__name__
__module____qualname__rY   r   r   r   rT   rT      s    $r   rT   c                       e Zd Zd Zy)Repeaterc                     || _         y r   rD   )rN   rE   s     r   __init__zRepeater.__init__   s    r   Nr[   r\   r]   ra   r   r   r   r_   r_      s    "r   r_   c                     t        |       S )z
    From the arguemnt, constructs something that acts like a 'list' with the argument repeated the 'correct' number of times

    s = struct_symSX([entry("x",repeat=6)])
    s["x"] = repeated(12)

  )r_   rD   s    r   repeatedrd      s     
!r   c                       e Zd ZdZy)NestedDictLiteralzT
    NestedDictLiteral will cause all dictionaries to become explicit recursively
  N)r[   r\   r]   __doc__r   r   r   rf   rf      s    r   rf   c                     t        |      rt        d|z        t        |       r.|t        |       k\  rt        d|dt        |       d      | |   S t	        | t
              r| j                  S | S )Nz$Got string %s where number expected.z!Rhs out of range. Got list index z  but rhs list is only of length .)r   rV   r   r   r   r_   rE   )payloadr)   s     r   payloadUnpackrk      sh    q\
:A=
>>#g,bcdghodpqrr1:'(#99Nr   c                   H    e Zd Zddg fdZd	dZeZd Zd Zd
dZd Z	ddZ
y)StructEntryNc                 .    || _         || _        || _        y r   )namer&   struct)rN   ro   rp   datar&   s        r   ra   zStructEntry.__init__   s    DIDIDKr   c                     d}t        | j                        dk\  r|dt        | j                        z  z  }| j                         r|| j	                         z  }|S || j
                  j                  d      z  }|S )N r   zrepeated(%s): Tcompact)r   r&   r   isPrimitiveprimitiveStringrp   __str__)rN   ru   r:   s      r   rx   zStructEntry.__str__   sx    	QDIIc$))n,,q$


  q X 	$++

d

++qXr   c                      y)Nr   r   rN   s    r   rw   zStructEntry.primitiveString   s    r   c                     | j                   d u S r   rp   rz   s    r   rv   zStructEntry.isPrimitive   s    ;;$r   c                    | j                   |dk(  rg gn| j                   j                  |dz
        }t        | j                  |      }| j                  gg}|r|D cg c]  }t        |||       c}S t        |||      S c c}w )Nr   r   limit)rp   traverseCanonicalIndexr$   r&   ro   r    )rN   r'   r   childrenlinr)   s          r   r   z"StructEntry.traverseCanonicalIndex   s    +uaxtdkk>`>`glmngn>`>oH	TYYt	$B
))A-/0Rga(#R00Qr(## 1s   Bc                 8    | j                   j                  |      S r   )rp   getStructEntryByStructIndex)rN   structIndexs     r   r   z'StructEntry.getStructEntryByStructIndex   s    ;;22;??r   c                 h   	 || j                   }t        |      dk(  rFt        |      dkD  r-| j                  |D cg c]  }t        d        c}||||      S  ||||       S t        |      dk(  rD| j	                         r |||t        |      |       S | j                  j                  ||||      S |d   }|d   }t        |t              r t        t        |j                  |             }t        |      r*| j                  |dd  |dd  |t        ||      fz   ||      S t        |t              rB|D cg c]6  }| j                  |dd  |dd  |t        ||      fz   |t        ||            8 c}S t        |t              rt        d      t        |t               rt        d      t        |t"              rIt        |      D cg c]4  }| j                  |g|dd  |t        ||      fz   |t        ||            6 c}S t        |t$              rC || j                  |dd  |||j'                         |            }	|j)                  ||d d |		      S t        d
t+        |      z        c c}w c c}w c c}w # t        $ r)}
t        dt+        |      dt+        |            |
d }
~
ww xY w)Nr   )r&   canonicalIndex
dispatcherrj   )entry
extraIndexr   r   r   rj   r   z3powerIndex entry {} cannot be used in list context.z>powerIndex entry {"foo","bar"} cannot be used in list context.r   r   inner%I don't know what to do with this: %sz/Error occured in entry context with powerIndex , at canonicalIndex )r&   r   traverseByPowerIndexslicerv   r   rp   r   r   r%   indicesr   r;   rk   rX   rV   setrf   callablecallableInnercallableOuterr   )rN   
powerIndexr&   r   r   rj   r)   pr:   rrE   s              r   r   z StructEntry.traverseByPowerIndex   s2   JP	diit	Z!	t9Q;**)-.AE$K."0(" +   GN>
>	TAGNeJ>OVZ[
[11"0("	 2   qMGa5!))A,'(!a=**ab>QR"0)Aa.1B"B(" +   $  Q -- n8%3Yq^4E%E!+ -ga 8 .    $OP
P#^_
_+,  ($ #Q --c8%3Yq^4E%E!+ -ga 8 .  #$ $ 8$))12-%335 *  ! ))'.DW[bc)d
dACFJK
KE /B$(  Pcfgqcrsv  xF  tG  H  I  OP  PPsm   9I? I0I? 
I? $2I? I? 6A.I? %I? 9;I54I? 7AI? 
9I:I? AI? &I? ?	J1$J,,J1F)F  )Nr   NN)r[   r\   r]   ra   rx   __repr__rw   rv   r   r   r   r   r   r   rm   rm      s6     $$B 
 ($@KPr   rm   c                   H    e Zd Zd
dZd ZddZeZddZd Zd Z	d Z
dd	Zy)	StructureNc           	      |   || _         |$| j                   D cg c]  }|j                   c}n|| _        t        j                  | j                  D cg c])  }t        |t              rt        |      nt        |g      + c}g       | _        t        | j                   D cg c]  }|j                  |f c}      | _
        | j                  D ]^  }t        |      r|| j                  vst        d|z        t        |t              s<|D ]  }|| j                  vst        d|z         ` y c c}w c c}w c c}w )NzOrder '%s' is invalid.)entriesro   orderrA   rB   r   r   r   keyslistrT   rX   r   rV   )rN   r   r   rE   r)   ees         r   ra   zStructure.__init__  s   DL38=$,,/,Q!&&,/eDJLLZ^ZdZd!eZdUVj5.A47tQCy#PZd!efhiDMdll;l166!*l;<DIZZ	1DII2Q67
7aBtyy 4r9::   0!e;s   D/.D4 D9c                     | j                   S r   )r   rz   s    r   rW   zStructure.keys+  s    ==r   c           	         d}|rD|ddj                  d t        | j                  j                               D              z   dz   z  }|S |dt	        | j                        z  z  }|dt        | j                        z  z  }t        | j                  j                               D ]&  \  }}|d|z   d	z   |j                  d
      z   dz   z  }( |S )Nrs   {,c              3   R   K   | ]  \  }}|d z   |j                  d      z    ! yw)z: Trt   N)rx   )r>   rZ   vs      r   r?   z$Structure.__str__.<locals>.<genexpr>1  s+     aI`#!A!d(aiii&==I`s   %'}zStructure holding %d entries.
z  Order: %s
z  z = Trt   
)joinr   rX   itemsr   r   r   rx   )rN   ru   r:   rZ   r   s        r   rx   zStructure.__str__.  s    	Q3adiiooN_I`aaadgggq X	 	,s499~==q/C

O
++qtyy()31Q
dQh!))D)"99D@
@! *Xr   c                 $   g }| j                   D ]y  }t        |t              rBt        |D cg c]%  }| j                  |   j                  d|dz
        ' c} D ]  }||z  }	 U|| j                  |   j                  |dz
        z  }{ |S c c}w )NTr   r~   )r   r   r   r3   rX   r   )rN   r   retdder   s         r   r   z Structure.traverseCanonicalIndex;  s    
CZZ	Ae	bcdbc\^tyy}CCDuUVwCWbcdeA
(# f 	tyy|22q2AA  J	 es   *B
c                 n    | j                   |d      }t        |      dkD  r|j                  |dd        S |S r   )rX   r   r   )rN   r   rE   s      r   r   z%Structure.getStructEntryByStructIndexE  s=    		+a.!A
;**;qr?;;hr   c                 b    | j                  |D cg c]  }t        |      s| c}      S c c}w r   )r   r   )rN   r   r   s      r   getStructEntryByCanonicalIndexz(Structure.getStructEntryByCanonicalIndexL  s+    ++,P19Q<Q,PQQ,Ps   ,,c           
         || j                   j                  vr9t        dt        |      t        t	        |j                                     fz        | j                   j                  |   j                   }|t        d|z        |S )Nz-Cannot find entry with key '%s'. Candidates: zEntry '%s' has no structure.)rp   rX   rV   r   r   rW   )rN   ro   r   s      r   	getStructzStructure.getStructO  sy    4;;###ETSVW[\`\e\e\gWhSiHjjkk
++

4
 
'
'C
{4=>>jr   c                 ^   	 t        |      dk(  r	 |||      S |d   }t        |      r)| j                  |   j                  |dd  ||fz   ||      S t	        |t
              rt        d      t	        |t        t                    r^	 t        | j                               D cg c]8  \  }}| j                  |   j                  |dd  ||fz   |t        ||            : c}}S t	        |t              st	        |t              rt	        |t              rht        |j                         D cg c]C  \  }}|| j                  |   j                  t	        |t              r|dd  n|g||fz   ||      fE c}}      S t        | j                  j                         D cg c]6  \  }}||j                  t	        |t              r|dd  n|g||fz   ||      f8 c}}      S t	        |t              rt	        |t              rYt        |j                         D cg c]4  \  }}||v r+|| j                  |   j                  |dd  ||fz   ||      f6 c}}      S t        |D cg c]-  }|| j                  |   j                  |dd  ||fz   ||      f/ c}      S t	        |t              rBt        |      D 	cg c]+  \  }}	| j                  |dd  ||	fz   |t        ||            - c}	}S t	        |t               rB || j                  |dd  ||j#                         |            }
|j%                  ||d d |
      S t        dt'        |      z        c c}}w c c}}w c c}}w c c}}w c c}w c c}	}w # t        $ r)}t        dt'        |      dt'        |            |d }~ww xY w)	Nr   r   r   z,slice not allowed here, did you mean '...' ?r   r   z0Error occured in struct context with powerIndex r   )r   r   rX   r   r   r   rV   typeEllipsis	enumeraterW   rk   rf   r   r   r   r   r   r   r   )rN   r   r   r   rj   r   r)   rZ   r   r:   r   rE   s               r   r   zStructure.traverseByPowerIndexX  s^   ZSz?Aj&HHqMQ<122qrN)1$.!	 3   % HI
I$x.) 'tyy{35 4ca YYq\66!!"~&4aT&9",,WQ7	 7  45 5 $:a0A#B% ")	 "1#!A iil77*4Qt*<
121#'5qd':#- !	 8  "1	 	 	  "&!2	 "3#!A ,,*4Qt*<
121#'5qd':#- '	 -  "3	 	 	 #% ")	 "1#!AAF iil77"12'5qd':#- !	 8  "1	 	 	   !	  !! iil77"12'5qd':#- '	 8   !	 	 	 $ &aL* )SQq ..!!"~&4aT&9",,WQ7	 /  )* * 8$))12-%335	 *  ! ))'.DW[bc)d
dACFJK
KQ5				*$  Sfijtfuvy  {I  wJ  K  L  RS  	SSs   M: 8M: AM: #=M M: $AM: ,AM
4M: <"M: ;M#
M: !8M: 9M)
M: 	M: #2M/M: M: :0M4*M: .AM:  :M: :	N,$N''N,r   r   )r   )r   NN)r[   r\   r]   ra   rW   rx   r   r   r   r   r   r   r   r   r   r   r     s4    ;"	 (R[Sr   r   c                        e Zd Zd Zd ZddZy)
Dispatcherc                 `    t        |j                               D ]  \  }}t        | ||        y r   )r   r   setattr)rN   r!   rZ   r   s       r   ra   zDispatcher.__init__  s'    DJJL!!d1Q "r   c                     | S r   r   rz   s    r   r   zDispatcher.callableInner  s    Kr   Nc                     |S r   r   )rN   rj   r   r   r   r   s         r   r   zDispatcher.callableOuter  s    Lr   NNN)r[   r\   r]   ra   r   r   r   r   r   r   r     s    r   r   c                   :    e Zd Zd ZddZddZddZddZddZy)	CasadiStructureDerivablec                    d }t        |t              r	|}t        }n2t        |t              s"t        |t              s	 t        |      }t        }|+t        |t              r	|}t        }n	 t        |      }t        }|2t        |t              r|}t        }||fS 	 t        |      }t        }||fS |fS #  Y ixY w#  Y CxY w#  t	        d      xY w)NzSCall to Structure has weird argument: expecting DM-like or MX-like or SXMatrix-like)r   DMMXSXrV   )rN   argmtyper   s       r   argtypez CasadiStructureDerivable.argtype  s    E#b
aeB
3r(:sG }	C		g!% }	C	 e9	qg!% e9Ae93	
	qop
ps#   B3 *B: C 3B7:B>Cc                    | j                  |      \  }}t        |t              rc|j                  d   dk(  rD|j                  d   dk(  r2| j                  dk7  r#t        j                  | j                  d      |z  }t        | |      S t        |t              rt        | |      S t        |t              rt        | |      S y )Nr   r   rq   )r   r   r   rI   sizeonesDMStructr   MXStructr   SXStruct)rN   r   r   r   s       r   __call__z!CasadiStructureDerivable.__call__  s    S!IQu!B	
qQWWQZ1_AGGDIIa "d""!Bd""!Bd"" r   c           	         | j                  |      \  }}|j                  d   | j                  k(  s(t        d| j                  |j	                         fz        t        t        d| |j                  d         g      }t        t        ft        t        ft        t        ffD ]3  \  }}t        ||      s ||t        ||j                  d               }5 j                  d   }d|_        |S )Nr   zExpecting %d x n DM. Got %str   )rp   repeatr   T)r   rI   r   rV   dimrp   r   r   r   r   r   r   r   r   DataReferenceRepeatedprefix
castmaster	rN   r   r   r   r:   r   cnumbersr   s	            r   rd   z!CasadiStructureDerivable.repeated  s    S!IQu771:"4		!%%'7JJKKcQWWQZ89:Ah-"h"h@1	AaA01771:>? A 	sAALHr   c           	         | j                  |      \  }}|j                  d   dk(  rN|j                  d   dk(  r<| j                  dk7  r-t        j	                  | j                  | j                        |z  }|j                  d   |j                  d   k(  r|j                  d   | j                  k(  s)t        d| j                  d|j                               t        t        d| | f      g      }t        t        ft        t        ft        t        ffD ]3  \  }}t        ||      s ||t        ||j                  d               }5 j                   d   }d|_        |S )	Nr   r   zExpecting square DM of size z. Got r   shapestructr   T)r   rI   r   r   r   rV   r   rp   r   r   r   r   r   r   r   DataReferenceSquaredr   r   r   s	            r   squaredz CasadiStructureDerivable.squared  s   S!IQuwwqzQ1771:?tyy!|77499TYY')q771:#
DII(=499QUUWUVVctDk234Ah-"h"h@1	AaA/!''!*=> A 	sAALHr   c           
         | j                  |      \  }}|j                  d   dk(  rN|j                  d   dk(  r<| j                  dk7  r-t        j	                  | j                  |j                        |z  }|j                  d   |j                  k(  r|j                  d   | j                  k(  s6t        d| j                  d|j                  d|j                               t        t        d| |f      g      }t        t        ft        t        ft        t        ffD ]A  \  }}t        ||      s ||t        ||j                  d   |j                  d               }C j                   d   }	d	|	_        |	S )
Nr   r   zExpecting DM of shape (r   z). Got r   r   r   T)r   rI   r   r   r   rV   r   rp   r   r   r   r   r   r   r   DataReferenceProductr   r   )
rN   otherstructr   r   r   r:   r   r   r   r   s
             r   productz CasadiStructureDerivable.product  s/   S!IQuwwqzQ1771:?tyy!|77499[--.q0q771:{'''AGGAJ		,A$))KL\L\]^]b]b]deffctK&89:;Ah-"h"h@1	AaA/!''!*QWWQZHI A 	sAALHr   c                    | j                  |      \  }}|j                  d   | j                  k(  r|j                  d   | j                  z  dk(  s(t        d| j                  |j	                         fz        t        t        d| | ft        |j                  d   | j                  z              g      }t        t        ft        t        ft        t        ffD ]T  \  }}t        ||      s ||t        || j                  t        |j                  d   | j                  z                    }V j                   d   }d|_        |S )Nr   r   z%Expecting square (%d) DM by N. Got %sr   )r   r   r   T)r   rI   r   rV   r   rp   r   r   r   r   r   r   r   r   r   DataReferenceSquaredRepeatedr   r   r   s	            r   squared_repeatedz)CasadiStructureDerivable.squared_repeated$  s   S!IQu771:tyy QWWQZ$))%;q%@>$))AEEGATTUUctDkQWWQZ$))=S9TUVWAh-"h"h@1	AaA7$))CPQ
UYU^U^H^D_`a A 	sAALHr   N)r   )	r[   r\   r]   r   r   rd   r   r   r   r   r   r   r   r     s!    "H#r   r   c                       e Zd ZddZy)GetterDispatcherNc                 j   |d n|j                   }|| j                  j                  v r|| j                  v rK|t	        |      dk(  r;| j                  |   }||S |dk(  rt        |      S t        d|j                   z        t        | j                  j                  |   ||      }	 |t        | j                  |      S |dk(  rt        | j                  |         S t        d|j                   z        t        dt        |      z        # t        $ r}t        dt        |      z        |d }~ww xY w)Nr   symmCannot handle type '%s'.r   1Error in powerIndex slicing for canonicalIndex %s"Canonical index %s does not exist.)r   rp   r+   priority_object_mapr   	triu2symmrV   performExtraIndexrJ   masterr   )	rN   rj   r   r   r   r   r   r)   rE   s	            r   r   zGetterDispatcher.__call__4  s.   =4ejjD(	433	39KsS]`aOa$$^4<(6\1
4uzzAB
B
DKKOON;zX]
^al<(a8
86\4;;q>*
*4uzzAB
B :S=PPQQ  lKsSaObcdjkkls$   (D  D D 	D2D--D2NNr[   r\   r]   r   r   r   r   r   r   3  s    Rr   r   c                   "    e Zd ZddZd ZddZy)SetterDispatcherNc                 p   | j                  |      }|d n|j                  }|| j                  j                  v ryt	        | j                  j                  |   ||      }	 ||| j
                  |<   n?|dk(  rt	        | j                  j                  |   ||d      }|j                         r|| j
                  |<   || j
                  |<   nt	        t        j                  |j                        ||      }	|	j                         |j                         k7  r3t        d|j                         z   dz   |	j                         z   dz         |j                  |j                            | j
                  |<   ||j                            | j
                  |<   nt        d|j                  z        y y y t        dt!        |      z        # t        $ r}
t        d	t!        |      d
t!        | j
                        dt!        t#        j                  | j
                              dt!        |      dt!        t#        j                  |            dt!        |      dt!        t#        j                  |            dt!        |
            d }
~
wt        $ r}
t        dt!        |      z        |
d }
~
ww xY w)Nr   r   Tr   r   flipzPayload sparsity z does not match lhs sparsity ri   r   $Error in canonicalIndex slicing for &: Incompatible types in a[i]=b with a  (z) and b z) and i z
). Error: r   r   )r   r   rp   r+   r   r   	is_scalarr   r   originalsparsitysparsityrV   r   TNotImplementedErrorCompatibilityExceptionr   rA   )rN   rj   r   r   r   payload_r   r)   iflipoirE   s              r   r   zSetterDispatcher.__call__P  s   zz'"H=4ejjD(
DKKOON;zX]
^al<#$++a.6\#DKKOON$Cz`ekop%!%DKKN!)DKK"2775+A+A#Bj_deB{{}h//111HLLNBFeehjhnhnhppsvvxx!)ENN,<!=DKK%ajjl3DKKN4uzzAB
B   & :S=PPQQ ! y$  fi  jx  fy  z}  ~B  ~I  ~I  zJ  KN  OW  O\  O\  ]a  ]h  ]h  Oi  Kj  kn  ov  kw  x{  |D  |I  |I  JQ  |R  xS  TW  XY  TZ  [^  _g  _l  _l  mn  _o  [p  qt  uv  qw  &x  y  	y lKsSaObcdjkkls&   !EG 	J5B8JJ5J00J5c                 B    t         j                  | j                        S )Nr|   )CasadiStructureIMDispatcherrp   rz   s    r   r   zSetterDispatcher.callableInnerl  s    ''t{{';;r   c                     	 || j                   |<   y # t        $ r; t        dt        |      dt        | j                         dt        |      d      t        $ r}t	        dt        |      z        |d }~ww xY w)Nr   r   z and b ri   r   )r   r  r  r   rV   )rN   rj   r   r   r   r   rE   s          r   r   zSetterDispatcher.callableOutero  s    j"dkk% u"  BE  FT  BU  VY  Z^  Ze  Ze  Vf  gj  kr  gs  $t  u  u jISQ_M`abhiijs    AA:A55A:r   r   )r[   r\   r]   r   r   r   r   r   r   r   r   O  s    R8<jr   r   c                       e Zd Zed        Zy)MasterGettablec                     | j                   j                  |t        | j                   | j                  | j                              S )N)rp   r   r   r   )rp   r   r   r   r   rN   r   s     r   rY   zMasterGettable.__getitem__x  sQ    ;;++JBRZ^ZeZemqmxmx  NR  Nf  Nf  Cg+  h  hr   Nr[   r\   r]   rR   rY   r   r   r   r  r  w  s    h hr   r  c                       e Zd Zed        Zy)MasterSettablec                     | j                   j                  |t        | j                   | j                  | j                        |      S )N)rp   r   r   r   rj   )rp   r   r   r   r   )rN   r   values      r   __setitem__zMasterSettable.__setitem__}  s=    ;;++JDKKt{{LUZ , \ \r   N)r[   r\   r]   rR   r  r   r   r   r  r  |  s    \ \r   r  c                    t        |       s@t        | t              r;t        |       dkD  r-t	        | D cg c]  }t        |       c}      rt        |       } t        | t              ra|t        d      |j                  t        d      t        |j                  |   t              st        d       | |j                  |         S | S c c}w )Nr   z0Cannot use delayed index without supplied entry.z6Cannot use delayed index without supplied shapestruct.z=Cannot use delayed index with a integer shapestruct argument.)
r   r   r   r   allFlatIndexDelegater	DelegaterrV   r   r   )r   r   r)   rE   s       r   
delegationr    s    zz*T:s:q?PUXpzY{pzklZcdeZfpzY{U|#J/J
9%}I&XYY 	2j(k"ke''*95y  JI  @J  :Je''*++ Z|s   C
c                    |t        |      dk(  r| S t        |d   t              r.t        |d   t              s |d   t	        | |dd  ||            S t        |d   t
              st        |      dkD  st        |      dk(  rt        dt        |      z        	 t        |      dk(  r#|d   }t        ||d      }| j                  |      S |\  }}t        ||d      }t        ||d      }| j                  |r||f      S ||f      S | S # t        $ r. t        dt        |      dt        t        |             d      w xY w)	Nr   r   r      z[Powerindex exhausted. Remaining %s is interpreted as extraIndex, but length must be 1 or 2.z!Powerindex exhausted. Passing on z to z(, but it doesn't know what to do with it)r   r   r   r  r   rf   rV   r   r  rY   r  r   )r)   r   r   r   r   r"   s         r   r   r     s   3z?A-H
1x(JqM)1T:a=*1
12UX\]^^	JqM"3	4
:qC
OQ.svy  {E  wF  F  G  GM	Z!	qMqq!}}Q!qq!qq!}}daU66166 H  Mnqr|n}  B  CG  HI  CJ  K  L  M  MMs   0D
 3D
  D
 
7Ec                   \    e Zd ZddZd Zd Zd Zd Zd ZeZ	d Z
ed        Zed	        Zy
)Prefixerc                     || _         || _        || _        g d}|D ]0  }t        | j                   |      st	        | || j
                         2 y )N)__DM____SX____MX__)rp   r   r   hasattrr   cast)rN   rp   r   r   methodsms         r   ra   zPrefixer.__init__  sD    DKDK DO,G	Q	Qtyy! r   c                 <    | j                  |d   |d   |d          y )Nrp   r   r   ra   rN   states     r   __setstate__zPrefixer.__setstate__  s    MM%/%/%2EFr   c                 J    | j                   | j                  | j                  dS )Nrp   r   r   r0  rz   s    r   __getstate__zPrefixer.__getstate__  s    kkT[[tWWr   c                 6   t        | j                  j                  t              r!| j                  j                  j                  }n| j                  j                  }t        |t
              s.t        |t              st        |t              st        ||      S y y y r   )	r   rp   r   DataReferencer   r   rX   r   getattr)rN   ro   r   s      r   __getattr__zPrefixer.__getattr__  sp     $++$$]3
++



a
++

aaAd!3z!E7JQt_ 8K!3r   c                     | j                   rZt        | j                  j                  t              r | j                  j                  j
                  S | j                  j                  S  |        S r   )r   r   rp   r   r3  r   rz   s    r   r'  zPrefixer.cast  sJ    	DKK&&}	5{{!!###{{!!!Vmr   c                 x    dt        | j                        z   dz   | j                  j                  d      z   dz   S )Nzprefix( r   Trt   ))r   r   rp   rx   rz   s    r   rx   zPrefixer.__str__  s8    DKK((3.1D1DT1D1RRUXXXr   c                 L    | j                   j                  | j                        S r   rp   rY   r   rz   s    r   r   zPrefixer.__call__  s    ;;""4;;//r   c                 R    | j                   j                  | j                  |z         S r   r:  r  s     r   rY   zPrefixer.__getitem__  s     ;;""4;;#;<<r   c                 T    | j                   j                  | j                  |z   |      S r   )rp   r  r   )rN   r   rq   s      r   r  zPrefixer.__setitem__  s"    ;;""4;;#;DAAr   Nr   )r[   r\   r]   ra   r.  r1  r5  r'  rx   r   r   rR   rY   r  r   r   r   r!  r!    sY    "GXY (0 = = B Br   r!  c                   .    e Zd Zd ZeZddZed        Zy)PrefixConstructorc                 F    d| j                   j                  d      z   dz   S )NzprefixConstructor(Trt   r8  )rp   rx   rz   s    r   rx   zPrefixConstructor.__str__  s$    $++"5"5d"5"CCcIIr   c                      || _         || _        y r   )rp   r   )rN   rp   r   s      r   ra   zPrefixConstructor.__init__  s    DKDOr   c                 F    t        | j                  || j                        S )N)r   )r!  rp   r   )rN   r   s     r   rY   zPrefixConstructor.__getitem__  s    DKK$//BBr   Nr   )r[   r\   r]   rx   r   ra   rR   rY   r   r   r   r>  r>    s*    J ( C Cr   r>  c                   |    e Zd ZdZd Z G d de      Z G d de      Zd Zd Z	d	 Z
dd
ZddZddZddZddZy)r
  z
    size
    map
  c                 F    dd l }|j                  | t        |d      d       y Nr   wbr  pickledumpfilerN   filenamerG  s      r   savezCasadiStructure.save      
KKT(4(+r   c                       e Zd ZddZy)#CasadiStructure.FlatIndexDispatcherNc           	      d   || j                   j                  v rt        | j                   j                  |   ||      }t        |t              rB|j                         sJ t        t        t        t        |j                                           S t        |      S t        dt        |      z        Nr   zCanonical index %s not found.)rp   r+   r   r   r   is_denser   r   nonzerosrV   r   )rN   rj   r   r   r   ress         r   r   z,CasadiStructure.FlatIndexDispatcher.__call__  s    	4;;??	* ?:\abc"
c#d3<<>234
4Cy7#n:MMNNr   r   r   r   r   r   FlatIndexDispatcherrO    s    Or   rU  c                       e Zd ZddZy)CasadiStructure.IMDispatcherNc                     || j                   j                  v r%t        | j                   j                  |   ||      S t        dt	        |      z        rQ  )rp   r+   r   rV   r   )rN   rj   r   r   r   s        r   r   z%CasadiStructure.IMDispatcher.__call__  sE    	4;;??	* !@J]bcc7#n:MMNNr   r   r   r   r   r   r  rW    s    Or   r  c                 4     | j                   |d   i |d    y )Nr!   kwargsr+  r,  s     r   r.  zCasadiStructure.__setstate__  s    DMM5=35?3r   c                 ,    t        | j                        S r   )rX   initializerrz   s    r   r1  zCasadiStructure.__getstate__  s      !!r   c                 D   ||d| _         t        j                  | g|i | i | _        g | _        i }d}| j                         D ]+  }| j                  |      }|j                  t        j                  dd      n|j                  }t        |t        t        |||j                         z                     }||j                         z  }t        |      }	|| j                  |	<   | xj                  t        t!        |j#                         |j%                                     D 
cg c]
  \  }
}|	|
|f c}}
z  c_        t'        |	      dd  D ]!  }||v r||   j)                  |       |g||<   # . || _        |j-                         D ]+  \  }}t/        |D cg c]  }|j0                  d d   c} ||<   - | j                  j3                  |        G d d      } G d d|      } G d d	|      } ||       | _         ||       | _        | | _        y c c}}
w c c}w )
Nr!   rZ  r   r   c                       e Zd Zd Zy)1CasadiStructure.__init__.<locals>.StructureGetterc                     || _         y r   r|   rN   rp   s     r   ra   z:CasadiStructure.__init__.<locals>.StructureGetter.__init__&  s	    r   Nrb   r   r   r   StructureGetterr`  %  s    r   rc  c                       e Zd Zed        Zy)*CasadiStructure.__init__.<locals>.IMGetterc                 x    | j                   j                  |t        j                  | j                               S Nr|   r  )rp   r   r
  r  r  s     r   rY   z6CasadiStructure.__init__.<locals>.IMGetter.__getitem__*  s1    {{//
oFbFbjnjujuFbFv/wwr   Nr  r   r   r   IMGetterre  )  s    x xr   rh  c                       e Zd Zed        Zy)1CasadiStructure.__init__.<locals>.FlatIndexGetterc                     t        | j                  j                  |t        j	                  | j                                    S rg  )rC   rp   r   r
  rU  r  s     r   rY   z=CasadiStructure.__init__.<locals>.FlatIndexGetter.__getitem__/  sK    4;;33J/JmJmuy  vA  vAJm  KB3  C  D  	Dr   Nr  r   r   r   FlatIndexGetterrj  .  s    D Dr   rl  )r\  r   ra   r+   lookuptabler   r   r  Sparsitydenser   r   r%   nnzr   r   zipget_colrowr6   appendr   r   vertcatnzupdater)   rP   rp   )rN   r!   rZ  hmaprZ   r)   rE   spr)  itkkr   r   r   rc  rh  rl  s                    r   ra   zCasadiStructure.__init__
  s    $7Dt+T+F+DHDD	A((*

-
-a
0a"#**"48>>!A!**b
RU1QrvvxZ()
*a2668ma8bdhhrl
yRZZ\"&&(9S/TU/Ttr!"R/TUU&r*12.!9
q'..
C$q'	 / + DIzz|!1-1a!$$q'1-.d1g HHOOD x? x
D/ D
 d^DFT"DFDK7 V .s   &HH
c                 X    |rdnd| j                   z  t        j                  | |      z   S )Nrs   zStructure with total size %d.
rt   )r   r   rx   rN   ru   s     r   rx   zCasadiStructure.__str__7  s-    B@499LyO`O`aenuO`Ovvvr   c                     |dk  s|| j                   k\  rt        d|| j                   fz        | j                  |   \  }}}|dk(  r|S |dk(  r||fz   S ||z   S )z
      Returns the canonicalIndex of the entry with a given flatIndex
      extraMode influences wether nothing (0), [i] (1) or [i,j] (2) will be returned as extra index
    r   z>Lookup index out of range. Got %d, but structure is of size %dr   )r   rV   rm  )rN   r)   	extraModecanrZ   r   s         r   getCanonicalIndexz!CasadiStructure.getCanonicalIndex:  sp    
 	salVZ[\`\e\eYffggq!GC!!|j	A!XoUlr   c                 t    t        | j                        D cg c]  }| j                  ||       c}S c c}w Nr  )r%   r   r  rN   r  r)   s      r   canonicalIndicesz CasadiStructure.canonicalIndicesI  s5    CHCSTCSaD""1y"9CSTTT   5c                 r    | j                  ||      }ddj                  t        t        |            z   dz   S )Nr  [r   ])r  r   r+   r   )rN   r)   r  r   s       r   getLabelzCasadiStructure.getLabelL  s6    q95AS$$s**r   c                 t    t        | j                        D cg c]  }| j                  ||       c}S c c}w r  )r%   r   r  r  s      r   labelszCasadiStructure.labelsP  s2    :?		:JK:JQDMM!iM0:JKKKr  Nr   )r   )r[   r\   r]   rg   rL  r   rU  r  r.  r1  ra   rx   r  r  r  r  r   r   r   r
  r
    sT    
,	OJ 	OOZ O4"+ZwU+Lr   r
  c                   D    e Zd ZdZd Zed        Zed        ZddZd Z	y)	
StructuredGeneric Structured objectc                     |j                   | _         | j                   j                  | _        | j                   j                  | _        | j                   j                  | _        t	        |       | _        y r   )rp   r)   rP   r   r>  r   )rN   	structures     r   ra   zStructured.__init__V  sJ    ""DK[[]]DF[[]]DF[[**DN#D)DKr   c                 .    | j                   j                  S r   )rp   r   rz   s    r   r   zStructured.size]  s    ;;r   c                 z    t        | j                  t              r| j                  j                  S | j                  S r   )r   r   r3  r   rz   s    r   catzStructured.cata  s(    $++m,[[]][[r   c                     |du r*| j                   dz   | j                  j                         z   S | j                   dz   | j                  j                  d      z   dz   S )NFz with following structure:
r   Trt   r8  )descriptionrp   rx   r}  s     r   rx   zStructured.__str__h  s[    % >>ATATAVVV$t{{':':4':'HH3NNr   c                 H    t        | j                  j                               S r   )r   rp   rW   rz   s    r   rW   zStructured.keysn  s      "##r   Nr   )
r[   r\   r]   r  ra   propertyr   r  rx   rW   r   r   r   r  r  S  s?    ++*    O$r   r  c                   @    e Zd ZdZd Zd Zd	dZed        Zd Z	d Z
y)
CasadiStructuredr  c                     t         j                  t               }|j                  |d   |d   d       | j                  ||d          y )Nr!   rZ  r^  r   r   r
  __new__r.  ra   rN   r-  css      r   r.  zCasadiStructured.__setstate__t  s?    		 	 	1BOOU6]U8_EFMM"5>M*r   c                 X    | j                   j                         }| j                  |d<   |S )Nr   )rp   r1  r   rN   r   s     r   r1  zCasadiStructured.__getstate__y  s&      "AAgJHr   Nc                    || _         t        |d      r(t        j                  | |j                         g | _        nSt        ||      }|j
                  | _        t        j                  | t        | j
                  |j                                | j                  j                  | _        | j                  j                  | _	        | j                  j                  | _
        | j                  j                  | _        i | _        y )Nrp   r  )r   r&  r  ra   rp   r   	EntryListr
  r  r  r  r  r   )rN   rp   r   	entrylists       r   ra   zCasadiStructured.__init__~  s    DJvh$v}}-dlF/i&&dl$t||9??ST![[::D KK88DKK((DM++$$DK!Dr   c                     | j                   dfS Nr   )r   rz   s    r   rI   zCasadiStructured.shape  s    IIa=r   c                 B    t         j                  | j                  d      S r  )rn  ro  r   rz   s    r   r  zCasadiStructured.sparsity  s    >>$))A&&r   c                 :     | j                   j                  |i |S r   )rp   lookup)rN   r!   rZ  s      r   r  z"CasadiStructured.getCanonicalIndex  s    4;;t-f--r   r   )r[   r\   r]   r  r.  r1  ra   r  rI   r  r  r   r   r   r  r  q  s4    +++

"   '.r   r  c                       e Zd Zy)r  N)r[   r\   r]   r   r   r   r  r    s    r   r  c                       e Zd ZdZddZd Zy)
ssymStructzsymbolic SXNc                    t         j                  | ||       t        d | j                  D              rt	        d      g }| j
                  j                         D ]w  }| j
                  j                  |      }|j                  t        j                  dj                  t        t        |            |j                  j                                      y t!        |D cg c]  }|j"                  d d   c} | _        | j                  D ]?  }|j                  |j                  | j$                  | j&                  |j(                     <   A y c c}w )Nr  c              3   8   K   | ]  }|j                   d u  y wr   exprr>   rE   s     r   r?   z&ssymStruct.__init__.<locals>.<genexpr>       
4|!166|   zmstruct_symSX does not accept entries with an 'expr' argument, because such an element is not purely symbolic._)r  ra   r-   r   rV   rp   r   r   rt  r   symr   r+   r   r  rp  ru  rv  r   r)   ro   )rN   rp   r   r:   r)   rE   s         r   ra   zssymStruct.__init__  s    d66

4t||
44  F  G  G
A[[//1
++
4
4Q
7ahhrvvchhs3qz*1::>>+;<= 2 Q/QADDGQ/0DK\\	
	&'eeDFF166N#  0s   Ec                     | j                   S r   r  rz   s    r   r$  zssymStruct.__SX__      88Or   r   )r[   r\   r]   r  ra   r$  r   r   r   r  r    s    +,"r   r  c                       e Zd ZddZy)VertsplitStructureNc                    || j                   }|| j                  }t        |t              r|j                  }g }g }g }g }d}|j                  d      D ]  }	|j                  |	      }
d }|
j                         r/|
j                  t        j                  dd      n|
j                  }n*t        j                  |
j                   j                  d      }|j                  |       t        |	      }|j                  |
       |j                  |       |j                  |       ||j                         z  } |j                  |j                                t!        |t#        ||      ||      D ]i  \  }}}}
|
j                         s!| j%                  |
j                   ||z   |       |j                         |k(  r|nt'        ||      | j(                  ||z   <   k y )Nr   r   r~   )rp   parentIndexparent)rp   r   r   r3  r   r   r   rv   r  rn  ro  r   rt  r   rp  size1rq  	vertsplitbuildMapr   r   )rN   rp   r  r  ksitsspsesrZ   r)   rE   ry  rz  s                r   r  zVertsplitStructure.buildMap  s   ~~&'xxf
B
C
C
B	A***3

/
/
2ab	
$%JJ$6X^^Aa AJJ^^AHHMM!,iil8biil	jjn	jjn2668ma 4 IIflln3y3C;Ar!]]_QXXK"4DAN67jjlB6FBrRSHd{2~. <r   )Nr   N)r[   r\   r]   r  r   r   r   r  r    s    Ur   r  c                       e Zd ZdZddZd Zy)
msymStructzMX.symNc                 <   t         j                  | ||       t        d | j                  D              rt	        d      t        d | j                  D              rt	        d      t
        j                  d| j                  d      | _        | j                          y )Nr  c              3   8   K   | ]  }|j                   d u  y wr   r  r  s     r   r?   z&msymStruct.__init__.<locals>.<genexpr>  r  r  zmstruct_symMX does not accept entries with an 'expr' argument, because such an element is not purely symbolic.c              3   8   K   | ]  }|j                   d u  y wr   )r  r  s     r   r?   z&msymStruct.__init__.<locals>.<genexpr>  s     
3l155lr  z<struct_symMX does not accept entries with an 'sym' argument.Vr   )
r  ra   r-   r   rV   r   r  r   r   r  )rN   rp   r   s      r   ra   zmsymStruct.__init__  s}    d66

4t||
44  F  G  G

3dll
33TUU&&TYYq)DKMMOr   c                     | j                   S r   r  rz   s    r   r%  zmsymStruct.__MX__  r  r   r   )r[   r\   r]   r  ra   r%  r   r   r   r  r    s    +
r   r  c                   $    e Zd Zed        ZddZy)MatrixStructc                 4    d| j                   j                  z   S )NzMutable )r   r[   rz   s    r   r  zMatrixStruct.description  s    

++++r   Nc                    t         j                  | |d        t        d | j                  D              rt	        d      || _        t        ||      st        |t              r|| _        n1|"|j                  | j                  d      | _        n ||      | _        | j                  j                  d   | j                  k7  r2t	        d| j                  | j                  j                         fz        | j                  j                  d   dk7  rN| j                  j                  d   dkD  r2t	        d| j                  | j                  j                         fz        | j                  D ]  }|j                  | |j                  <    y )Nr  c              3   8   K   | ]  }|j                   d u   y wr   r  r  s     r   r?   z(MatrixStruct.__init__.<locals>.<genexpr>       
0<a166T><r  z;struct_SX does only accept entries with an 'expr' argument.r   r   z<MatrixStruct: dimension error. Expecting %d-by-1, but got %s)r  ra   r-   r   rV   r   r   r3  r   nanr   rI   r   r  ro   )rN   rp   r   rq   r   rE   s         r   ra   zMatrixStruct.__init__  s=   d65

04<<
00STTDJ$uD!?dk	IIdii*dk$Kdk{{TYY&TX\XaXabfbmbmbqbqbsWttuu{{Q4;;#4#4Q#7#9TX\XaXabfbmbmbqbqbsWttuu\\VVd166l r   r   )r[   r\   r]   r  r  ra   r   r   r   r  r    s    , ,r   r  c                   ,    e Zd Zd Zd Zd ZddZd Zy)r   c                 F    dd l }|j                  | t        |d      d       y rD  rF  rJ  s      r   rL  zDMStruct.save  rM  r   c                     t         j                  t               }|j                  |d   |d   d       | j                  ||d          y )Nr!   rZ  r^  r   r   r  r  s      r   r.  zDMStruct.__setstate__  s?    		 	 	1BOOU6]U8_EFMM"%/M*r   c                 X    | j                   j                         }| j                  |d<   |S )Nr   )rp   r1  r   r  s     r   r1  zDMStruct.__getstate__  s&      "A++AhKHr   Nc                 >    t         j                  | |t        |       y )Nr   )r  ra   r   )rN   rp   rq   s      r   ra   zDMStruct.__init__  s    $vbd3r   c                     | j                   S r   r  rz   s    r   r#  zDMStruct.__DM__  r  r   r   )r[   r\   r]   rL  r.  r1  ra   r#  r   r   r   r   r     s    ,+

4r   r   c                       e Zd ZddZd Zy)r   Nc                 @    t         j                  | |t        ||       y N)rq   r   )r  ra   r   rN   rp   rq   r   s       r   ra   zSXStruct.__init__  s    $vbd?r   c                     | j                   S r   r  rz   s    r   r$  zSXStruct.__SX__  r  r   r   )r[   r\   r]   ra   r$  r   r   r   r   r     s    @r   r   c                       e Zd ZddZd Zy)r   Nc                 `    t         j                  | |t        ||       | j                          y r  )r  ra   r   r  r  s       r   ra   zMXStruct.__init__  s$    $vbd?MMOr   c                     | j                   S r   r  rz   s    r   r%  zMXStruct.__MX__$  r  r   r   )r[   r\   r]   ra   r%  r   r   r   r   r     s    
r   r   c                   4    e Zd ZdZddZd Zd Zed        Zy)MXVeccatStructzPartially mutable MXNc                    t         j                  | ||       t        d | j                  D              rt	        d      g | _        i | _        t        | j                  j                  d            D ]8  \  }}| j
                  j                  d        || j                  t        |      <   : | j                  D ]  }|j                  | |j                  <    d| _        y )Nr  c              3   8   K   | ]  }|j                   d u   y wr   r  r  s     r   r?   z*MXVeccatStruct.__init__.<locals>.<genexpr>+  r  r  z;struct_MX does only accept entries with an 'expr' argument.r   r~   T)r  ra   r-   r   rV   storagemappingr   rp   r   rt  r   r  ro   dirty)rN   r   r   rZ   r)   rE   s         r   ra   zMXVeccatStruct.__init__)  s    d3U3

04<<
00STTDLDL;;!;DE!
ll$ dll58 F \\VVd166l  DJr   c                 v     t        t              sfd fd	} j                  j                  ||      S )Nc                 @   |t        d      t        d      st        d      |j                  v rՉj                  j                  |   j                         | j                         k7  ryt        dt              dt        |      dj                  j                  |   j                         j                         d| j                         j                         d	      | j                  j                  |   <   d
_	        y t        d	t        |      z        )NzLAn MX veccat structure does not accept indexing on MX level for __setitem__.r  zKAn MX veccat structure __setitem__ accepts only objects that have sparsity.zError in powerIndex slicing z for canonicalIndex z: Shape mismatch. lhs is z	, rhs is ri   zNot found: %s T)
rV   r&  r  rp   r+   r  r   r   r  r  )rj   r   r   r   r   rN   s       r   injectz*MXVeccatStruct.__setitem__.<locals>.inject=  s]   		fggT*%eff	4<<	';;??>*335w7G7G7II{~  @J  |K  LO  P^  L_  `d  `k  `k  `o  `o  p~  `  `H  `H  `J  `N  `N  `P  QX  Qa  Qa  Qc  Qg  Qg  Qi  j  k  k5<T\\.12 dj (3~+>>??r   r  r   )r   r   rp   r   )rN   r   r  r  s   ``  r   r  zMXVeccatStruct.__setitem__9  s8    j'=j ;;++J&QV+WWr   c                     | j                   S r   r  rz   s    r   r%  zMXVeccatStruct.__MX__L  r  r   c           
         t        d | j                  D              rdt        | j                  j	                               D cg c]!  }| j                  | j                  |       |# }}t        dt        |      z        | j                  rWt        | j                  D cg c]6  }t        j                  |j                         r|n|j                  d d        8 c} | _        | j                  S c c}w c c}w )Nc              3   $   K   | ]  }|d u  
 y wr   r   r  s     r   r?   z(MXVeccatStruct.master.<locals>.<genexpr>Q  s     
+l19ls   z`Problem in MX vecNZcat structure cat: missing expressions. The following entries are missing: %s)r-   r  r   r  rW   rV   r   r  ru  rG   rC   rR  rv  master_cached)rN   rZ   missingr)   s       r   r   zMXVeccatStruct.masterO  s    

+dll
++ !2!2!45_5qdllSTo9V9^5g_x{~  @G  |H  H  I  Izz"Y]YeYe$fYeTUVZZQZZ\qttAw%OYe$fgd ` %gs   !C0$C0 ;C5r   )	r[   r\   r]   r  ra   r  r%  r  r   r   r   r   r  r  '  s+    &+ X&  r   r  c                       e Zd Zd Zd Zd Zy)CasadiStructEntryc           	         t        |      dk(  rt        d      |d   | _        || _        t        |      dkD  rt        dt	        |dd        z        t        |j                               }g d}|D ]!  }||vst        d|dt	        |      d       d	d
gfd
d	dgfdd
gfdg dfdg dffD ]'  \  }}||v s|D ]  }||v st        d|d|d       ) g | _        d|v r#t        |d   t
              r|d   n|d   g| _        t        | j                  D 	cg c]  }	t        |	       c}	      s!t        dt	        | j                        z        d | _        d
|v r>|d
   }
t        |
t              r|
| _        n!t        |
t              r|
j                  | _        d | _        d	|v r|d	   }t        |      rt        j!                  |d      | _        nt        |t
              st        |t"              r?t        |      dk(  st        |      dkD  rt        d      t        j                   | | _        nJt        |t              r|| _        n2t        dt	        |      z        t        j!                  dd      | _        d | _        d|v rA|d   }t        |t              st        |t              r|j                  df| _        nt        |t"              rt        |D cg c]1  }t        |t              xs t        |t              xs t        |      3 c}      rt        |      dk(  st        |      dkD  rt        d      t#        |D cg c]  }t        |      r|n|j                   c}      | _        nt        d      d	|vrFt        j                   | j$                  D cg c]  }t        |      r|n|j&                   c} | _        d | _        d|v r|d   }t        |t*              r|j-                         r|| _        nTt        |t              r#|j                  | _        |j.                  | _        n!t        dt	        | j(                        z        | j(                  j                         | _        d | _        d|v r|d   | _        g ffd	 | j0                        \  }}|$dg| _        t        j!                  dd      | _        n@|| _        t3        |d      r|j                         | _        nt        dt	        |      z        d | _        d|v r|d   | _        dg}| j4                  |vr+t        d t	        | j4                  t	        |            z        | j4                  dk(  r| j                  j7                         | j                  j9                         k7  r)t        d!| j                  j;                         z  dz         | j                  | _        | j                  t        j?                  | j                  j7                               z  | _        t@        jC                  | | j                  | j                  | j                  | j                  "       y c c}	w c c}w c c}w c c}w )#Nr   z/Missing name argument (first argument of Entry)r   z/Don't know what to do with unnamed arguments %s)r   rI   r  r  rp   r   r   zUnknown keyword argument 'z'. Please use one of ri   rI   rp   r   r  )rI   r   r  r  )rI   r   r  zYou supplied keyword argument 'z4', but it cannot be combined with keyword argument 'z'.r   zIThe 'repeat' argument, if present, must be a list of integers, but got %sr  zeThe 'shape' argument, if present, must be an integer, a tuple of 1 or 2 integers, a sparsity pattern.zpThe 'shape' argument, if present, must be an integer, a tuple of 1 or 2 integers, or a sparsity pattern. Got %s z_The 'shapestruct' argument, if present, must be a structure or a tuple of structures or numbersz\The 'shapestruct' argument, if present, must be a structure or a tuple of at most structureszYThe 'sym' argument must be a purely symbolic SX or a structured symbolic. Got %s instead.c                     t        | t              r0t        |       dk(  rd |dgz   fS  | d   |t        |       gz         S | |fS )Nr   )r   )r   r   r   )rE   r   getPrimitives     r   r  z0CasadiStructEntry.__init__.<locals>.getPrimitive  sL    aVQYs
?"!FCF8O<<6/r   r  zeThe 'expr' argument must be a matrix expression or nested list of matrix expressions. Got %s instead.r   r   zIYou supplied a type argument '%s' but it is not recognised. Use one of %sz:You supplied a type 'symm', but matrix is not square. Got )rp   r&   rq   )"r   rV   ro   rX   r   r   rW   r   r   r  r   rp   r   r  r  rn  ro  r   r   r   r  r   is_valid_inputr  r  r&  r   r  size2r   r  upperrm   ra   )rN   r!   rZ  kwkwsrZ   kcfkfkir   rp   rI   r   rE   r  r   r   allowedclassr  s                     @r   ra   zCasadiStructEntry.__init__g  sZ   
4y!|GHHq'diDI
4y{G#dSTSUh-WXX 
fkkm	B
GC	
#RSTWX[T\]^^  H:
W]+
,(
$*
+*
+
B <cf}}  AD  E  F  F 
 DK6(26(3CD(IF8$PVW_P`Oadkt{{3{!
1{34adghlhshsdttuu DK6hf	F9	%fZ(mm DM&Woe	E	 uQ/eD!Ze%<u:q=CJqL  B  C  C"..%0$-eH%  K  NQ  RW  NX  X  Y  	YnnQq)dmD=)k	K
	+z+i/P'..q1k%(bmnbm]^Jq,X
1Y0GX:VW=Xbmnotw  yD  uE  GH  uH  LO  P[  L\  ]^  L^{|
| K!XKqz!}!!(("BK!XYvww		 QUQaQa(bQaAjm)GQa(bc DH5Mc	C	 2 2 4c*%jj77svyz~  {C  {C  wD  D  E  	Ehh'')dm DI.di "  #ca	
c q+1Z **,$-  B  EH  IJ  EK  K  L  LDIdiXl	,	&cgjkoktktux  zF  vG  hH  I  J  	J	F	== DMM$7$7$99VY]YfYfYjYjYllorrs
s $hnnT]]5H5H5J&KK diit{{QUQ^Q^_Q 4N o!X
 )cs   Y6Y$	 Y)" Y.c                     | j                   | j                  j                         S | j                   dk(  r d| j                  j                         z   dz   S y )Nr   zsymm(r8  )r   r  r   rz   s    r   rw   z!CasadiStructEntry.primitiveString  sM    yy]]  	F	))++c11 
r   c                 ,     t         fddD              S )Nc              3   :   K   | ]  }|t        |      f  y wr   )r4  )r>   rZ   rN   s     r   r?   z1CasadiStructEntry.__getstate__.<locals>.<genexpr>  s     q-p74?#-ps   )ro   rp   r  r   r   r   r&   )rX   rz   s   `r   r1  zCasadiStructEntry.__getstate__  s    q-pqqqr   N)r[   r\   r]   ra   rw   r1  r   r   r   r  r  f  s    N``2rr   r  c                  d    t        |       dk(  rt        | d   t              r| d   S t        | i |S )Nr   r   )r   r   r  r^  s     r   r   r     s4    Y\ja):;7N	D	*6	**r   c                       e Zd ZddZy)r  Nc           
         g | _         g | _        t        |t              st	        dt        |      z        |D ]  }t        |t              rZt        t        t        |            }| j                  j                  t        d |D                     | xj                   |z  c_         mt        |      }| j                  j                  |j                         | j                   j                  |        |.t        d | j                  D              rt	        d      || _        | j                   D cg c]  }|j                   c}| _        t        | j                        t        t        | j                              k7  rg }t        | j                        D ]<  \  }}|| j                  d | v s|| j                  |dz   d  v s,|j                  |       > t	        dt        t        t        |                  z        y c c}w )NzHExpecting list of entries, with possible tuples for grouping, but got %sc              3   4   K   | ]  }|j                     y wr   )ro   )r>   r   s     r   r?   z%EntryList.__init__.<locals>.<genexpr>  s     81s   c              3   <   K   | ]  }t        |t                y wr   rM   r  s     r   r?   z%EntryList.__init__.<locals>.<genexpr>  s     5*QZ% *r@   zYou supplied an order by using tuple syntax on entries %s, but you overwrite it with the 'order' keyword. Use one or the other, not both.r   z,Your list of entries contains duplicates: %s)r   r   r   r   rV   r   r   r+   r   rt  ro   r-   namesr   r   r   )	rN   r   r   rE   r   r   r   
duplicatesr)   s	            r   ra   zEntryList.__init__  s   DLDJc$`cfgjckkll	Ae	s5|$

%8889g1X

"''"B  	5$**5	5  d  e  	edj"&,,/,Q!&&,/DJ
4::C

O,,j4::&#!A

2A!tzz!A#$'7"7


A
 ' Ds4PST^P_K`Gaabb - 0s   0G?r   rb   r   r   r   r  r    s    cr   r  c                       e Zd Zd Zd ZeZy)r  c                     || _         y r   )r   rN   r   s     r   ra   zDelegater.__init__'  s	    DHr   c                 `    | j                   j                  dt        | j                        dS )Nr  r  )	__class__r[   r   r   rz   s    r   rx   zDelegater.__str__*  s    ~~..s488}==r   N)r[   r\   r]   ra   rx   r   r   r   r   r  r  &  s    > (r   r  c                       e Zd Zd Zy)IndexDelegaterc                 L    |j                   j                  | j                        S r   )r)   rY   r   rb  s     r   r   zIndexDelegater.__call__1      88))r   Nr   r   r   r   r  r  0      *r   r  c                       e Zd Zd Zy)r  c                 L    |j                   j                  | j                        S r   )rP   rY   r   rb  s     r   r   zFlatIndexDelegater.__call__5  r  r   Nr   r   r   r   r  r  4  r  r   r  c                   (    e Zd ZdZddZed        Zy)DelegaterConstructorz
    Creates an object that delegates a slicing operation.

    Example usage:
      s = struct_symSX([])
      x = struct_symSX(entry("x",Sparsity.diag(4)))
      x["x",0,index[:]]

  c                      || _         || _        y r   )prepend	delegater)rN   r  r  s      r   ra   zDelegaterConstructor.__init__C  s    DLDNr   c                 >    | j                  | j                  |z         S r   )r  r  r  s     r   rY   z DelegaterConstructor.__getitem__G  s    >>$,,,--r   N)r   )r[   r\   r]   rg   ra   rR   rY   r   r   r   r  r  9  s      . .r   r  c                   "    e Zd Zed        Zd Zy)r3  c                 .    | j                   j                  S r   )r   rI   rz   s    r   rI   zDataReference.shapeO  s    66<<r   c                 6    | j                   j                         S r   )r   r   rz   s    r   r   zDataReference.dimS  s    66::<r   N)r[   r\   r]   r  rI   r   r   r   r   r3  r3  N  s     r   r3  c                       e Zd Zd Zd Zd Zy)r   c                     |j                         sJ || _        || _        |j                  ||j	                         z  df      | _        y r  )rR  r   r   rH   r  r   rN   r   r   s      r   ra   zDataReferenceRepeated.__init__X  s<    ::<<DFDFYY!'')A'DFr   c                     || j                   j                  |<   | j                  j                         j	                         }| j                   j                  |   | j                  j                  |<   y r   r   rv  r   r  findrN   r   r"   Is       r   r  z!DataReferenceRepeated.__setitem__^  I    DFFIIaL A6699Q<DFFIIaLr   c                 4    | j                   j                  |   S r   r   rv  rN   r   s     r   rY   z!DataReferenceRepeated.__getitem__c      6699Q<r   Nr[   r\   r]   ra   r  rY   r   r   r   r   r   W  s    ( 
r   r   c                   .    e Zd Zd Zd Zd Zed        Zy)r   c                 R    |j                         sJ || _        || _        || _        y r   )rR  r   r   r   r!  s      r   ra   zDataReferenceSquared.__init__g  s%    ::<<DFDFDFr   c                 6    || j                   j                  |<   y r   r   rv  rN   r   r"   s      r   r  z DataReferenceSquared.__setitem__m      DFFIIaLr   c                 4    | j                   j                  |   S r   r0  r*  s     r   rY   z DataReferenceSquared.__getitem__p  r+  r   c                 8    | j                   | j                   z  dfS r  r   rz   s    r   rI   zDataReferenceSquared.shapes      FF466M!r   N)r[   r\   r]   ra   r  rY   r  rI   r   r   r   r   r   f  s%      r   r   c                   :    e Zd Zd Zd Zd Zed        Zd Zd Z	y)r   c                 `    |j                         sJ || _        || _        || _        || _        y r   )rR  r   r   r   r)  )rN   r   r   r)  s       r   ra   zDataReferenceProduct.__init__x  s,    ::<<DFDFDFDFr   c                 6    || j                   j                  |<   y r   r0  r1  s      r   r  z DataReferenceProduct.__setitem__  r2  r   c                 4    | j                   j                  |   S r   r0  r*  s     r   rY   z DataReferenceProduct.__getitem__  r+  r   c                 8    | j                   | j                  z  dfS r  )r   r)  rz   s    r   rI   zDataReferenceProduct.shape  r6  r   c                     | j                   S r   r5  rz   s    r   r  zDataReferenceProduct.size1      66Mr   c                     | j                   S r   )r)  rz   s    r   r  zDataReferenceProduct.size2  r=  r   N)
r[   r\   r]   ra   r  rY   r  rI   r  r  r   r   r   r   r   w  s/      r   r   c                       e Zd Zd Zd Zd Zy)r   c                     |j                         sJ || _        || _        || _        |j	                  ||z  |z  df      | _        y r  )rR  r   r   NrH   r   )rN   r   r   rA  s       r   ra   z%DataReferenceSquaredRepeated.__init__  sA    ::<<DFDFDFYY!Aay!DFr   c                     || j                   j                  |<   | j                  j                         j	                         }| j                   j                  |   | j                  j                  |<   y r   r#  r%  s       r   r  z(DataReferenceSquaredRepeated.__setitem__  r'  r   c                 4    | j                   j                  |   S r   r)  r*  s     r   rY   z(DataReferenceSquaredRepeated.__getitem__  r+  r   Nr,  r   r   r   r   r     s    " 
r   r   c                 @    dd l }|j                  t        | d            S )Nr   rb)rG  loadrI  )rK  rG  s     r   struct_loadrG    s    ;;tHT*++r   r   )NNF)OrG   numpyr	   operatorsysversion_inforA   __builtin__collectionsCallabler   abcr   r   r   r   r    r$   r1   r3   r6   r;   rC   rJ   rR   rX   rT   r_   rd   rf   
nesteddictrk   rm   objectr   r   r   r   r   r  r  r  r   r!  r>  r
  r  r  rV   r  r  r  r  r  r   r   r   r  struct_symSXstruct_symMX	struct_SXstruct_MX_mutable	struct_MXrp   r   r  r  r  r  r  r  r7   indexfr3  r   r   r   r   rG  r   r   r   <module>rX     s  0    
v( &!!(73 &@7	%`'
$t $# # 
  

oP oPbZS ZSz	 	n n`Rz R8&jz &jPh h
\ \	02B 2BhC CiLi 8 iLV$ $<%.z": %.NY !. ,U UB!.1C (#N> 6| ,| |. 1%n 1h 	 		 	Xr Xrv+
c c@ *Y ** *
. .$ 
n	-	0	1 M = "= 0=  ,o+&__%%(s   H% %I 