\unprotect
\def\dostartmathmatrix[#1][#2]%
  {\begingroup
   \edef\currentmathmatrix{#1}%
   \doifassignmentelse{#2}{\setupmathmatrix[#1][#2]}\donothing
   \null
    \executeifdefined{\??mx:\mathmatrixparameter\c!location}{\getvalue{\??mx:\v!lohi}}%
   \mathmatrixleft
   \mathmatrixbox\bgroup
   \pushmacro\domatrixNC
   \let\endmath\relax
   \def\NC{\domatrixNC}%
   \def\MC{\domatrixNC\ifmmode\else$\def\endmath{$}\fi}%
   \global\let\domatrixNC\dodomatrixNC
   \def\NR{\endmath\global\let\domatrixNC\dodomatrixNC\crcr}%
   \normalbaselines
   \mathsurround\zeropoint
   \everycr\emptytoks
   \tabskip\zeropoint
   \eqaligncolumn\zerocount
   \processcommacommand[\mathmatrixparameter\c!align]{\advance\eqaligncolumn\plusone\dosetmatrixcolumn}%
   \scratchcounter=\ifnum\eqaligncolumn>\zerocount \eqaligncolumn \else \plusone \fi
   \global\eqaligncolumn\plusone
   \preparemathmatrix } % uses scratchcounter
\protect

\definetypeface[mainface] [rm]   [serif]  [times]     [default]  [encoding=texnansi]
\definetypeface[mainface] [mm] [math] [modern] [modern] [encoding=texnansi]
\setupbodyfont[mainface,11pt]

\definemathmatrix[mycases][left={\left\{\,},right={\,\right.}]
\definemathmatrix[aligned][n=3,align={right,left,left},distance=4pt]
\definemathmatrix[alignedfour][n=4,align={left,middle,left,right}]

\startbuffer
\startformula
\startalignedfour[left={\left\{},right={\right\}}]
\NC left \NC middle \NC  left \NC right \NR
\NC aaa \NC = \NC 0 \NC |x| > 0 \NR
\NC aaaa \NC = \NC 1 \NC |x| < 0 \NR
\NC bbb \NC = \NC 2 \NC bb \NR
\NC bb \NC = \NC 3 \NC ccc \NR
\stopalignedfour
\stopformula

\startformula
\startmycases[n=4,align={left,middle,middle,right},distance=1em]
\NC left \NC middle \NC middle \NC right \NR
\NC aaaaaa \NC baaaaaaaaa \NC cccc \NC ddddddddddddd  \NR
\NC aaaaaaaaa \NC bbb \NC c \NC ddd \NR
\stopmycases
\stopformula

\startformula
\startaligned[left={\left.},right={\right\}}]
\NC (\lambda + \mu ) \cdot a \NC = \NC \lambda \cdot a + \mu \cdot a \NR
\NC \lambda ( a + b ) \NC = \NC \lambda \cdot a + \lambda \cdot b \NR
\stopaligned
\quad
\text{(distributieve eigenschappen)}
\stopformula
\stopbuffer

\starttext
Test user defined math matrices without a section (with Aditya's fix)
\blank
Math matrix alignment works.
\getbuffer
\section{Test user defined math matrices but after a section (with Aditya's fix)}
Math matrix alignment broken
\getbuffer

\page
\section{Default math characters}
\showmathcharacters

\stoptext

