\section{Geometric Image Transformations}

The functions in this section perform various geometrical transformations of 2D images. That is, they do not change the image content, but deform the pixel grid, and map this deformed grid to the destination image. In fact, to avoid sampling artifacts, the mapping is done in the reverse order, from destination to the source. That is, for each pixel $(x, y)$ of the destination image, the functions compute coordinates of the corresponding "donor" pixel in the source image and copy the pixel value, that is:

\[\texttt{dst}(x,y)=\texttt{src}(f_x(x,y), f_y(x,y))\]

In the case when the user specifies the forward mapping: $\left<g_x, g_y\right>: \texttt{src} \rightarrow \texttt{dst}$, the OpenCV functions first compute the corresponding inverse mapping: $\left<f_x, f_y\right>: \texttt{dst} \rightarrow \texttt{src}$ and then use the above formula.

The actual implementations of the geometrical transformations, from the most generic \cvCross{Remap}{remap} and to the simplest and the fastest \cvCross{Resize}{resize}, need to solve the 2 main problems with the above formula:
\begin{enumerate}
    \item extrapolation of non-existing pixels. Similarly to the filtering functions, described in the previous section, for some $(x,y)$ one of $f_x(x,y)$ or $f_y(x,y)$, or they both, may fall outside of the image, in which case some extrapolation method needs to be used. OpenCV provides the same selection of the extrapolation methods as in the filtering functions, but also an additional method \texttt{BORDER\_TRANSPARENT}, which means that the corresponding pixels in the destination image will not be modified at all.
    \item interpolation of pixel values. Usually $f_x(x,y)$ and $f_y(x,y)$ are floating-point numbers (i.e. $\left<f_x, f_y\right>$ can be an affine or perspective transformation, or radial lens distortion correction etc.), so a pixel values at fractional coordinates needs to be retrieved. In the simplest case the coordinates can be just rounded to the nearest integer coordinates and the corresponding pixel used, which is called nearest-neighbor interpolation. However, a better result can be achieved by using more sophisticated \href{http://en.wikipedia.org/wiki/Multivariate_interpolation}{interpolation methods}, where a polynomial function is fit into some neighborhood of the computed pixel $(f_x(x,y), f_y(x,y))$ and then the value of the polynomial at $(f_x(x,y), f_y(x,y))$ is taken as the interpolated pixel value. In OpenCV you can choose between several interpolation methods, see \cvCross{Resize}{resize}.
\end{enumerate}

\ifCPy

\cvCPyFunc{GetRotationMatrix2D}
Calculates the affine matrix of 2d rotation.

\cvdefC{
CvMat* cv2DRotationMatrix(
\par CvPoint2D32f center,
\par double angle,
\par double scale,
\par CvMat* mapMatrix );
}\cvdefPy{GetRotationMatrix2D(center,angle,scale,mapMatrix)-> None}

\begin{description}
\cvarg{center}{Center of the rotation in the source image}
\cvarg{angle}{The rotation angle in degrees. Positive values mean counter-clockwise rotation (the coordinate origin is assumed to be the top-left corner)}
\cvarg{scale}{Isotropic scale factor}
\cvarg{mapMatrix}{Pointer to the destination $2\times 3$ matrix}
\end{description}

The function \texttt{cv2DRotationMatrix} calculates the following matrix:

\[
\begin{bmatrix}
\alpha & \beta & (1-\alpha) \cdot \texttt{center.x} - \beta \cdot \texttt{center.y} \\
-\beta & \alpha & \beta \cdot \texttt{center.x} - (1-\alpha) \cdot \texttt{center.y}
\end{bmatrix}
\]

where

\[
\alpha = \texttt{scale} \cdot cos(\texttt{angle}), \beta = \texttt{scale} \cdot sin(\texttt{angle})
\]

The transformation maps the rotation center to itself. If this is not the purpose, the shift should be adjusted.

\cvCPyFunc{GetAffineTransform}
Calculates the affine transform from 3 corresponding points.

\cvdefC{
CvMat* cvGetAffineTransform(
\par const CvPoint2D32f* src,
\par const CvPoint2D32f* dst,
\par CvMat* mapMatrix );
}\cvdefPy{GetAffineTransform(src,dst,mapMatrix)-> None}

\begin{description}
\cvarg{src}{ Coordinates of 3 triangle vertices in the source image}
\cvarg{dst}{ Coordinates of the 3 corresponding triangle vertices in the destination image}
\cvarg{mapMatrix}{ Pointer to the destination $2 \times 3$ matrix}
\end{description}

The function cvGetAffineTransform calculates the matrix of an affine transform such that:

\[
\begin{bmatrix}
x'_i\\
y'_i
\end{bmatrix}
=
\texttt{mapMatrix}
\cdot
\begin{bmatrix}
x_i\\
y_i\\
1
\end{bmatrix}
\]

where

\[
dst(i)=(x'_i,y'_i),
src(i)=(x_i, y_i),
i=0,1,2
\]

\cvCPyFunc{GetPerspectiveTransform}
Calculates the perspective transform from 4 corresponding points.

\cvdefC{
CvMat* cvGetPerspectiveTransform(
\par const CvPoint2D32f* src,
\par const CvPoint2D32f* dst,
\par CvMat* mapMatrix );
}\cvdefPy{GetPerspectiveTransform(src,dst,mapMatrix)-> None}

\begin{description}
\cvarg{src}{Coordinates of 4 quadrangle vertices in the source image}
\cvarg{dst}{Coordinates of the 4 corresponding quadrangle vertices in the destination image}
\cvarg{mapMatrix}{Pointer to the destination $3\times 3$ matrix}
\end{description}

The function \texttt{cvGetPerspectiveTransform} calculates a matrix of perspective transforms such that:

\[
\begin{bmatrix}
x'_i\\
y'_i
\end{bmatrix}
=
\texttt{mapMatrix}
\cdot
\begin{bmatrix}
x_i\\
y_i\\
1
\end{bmatrix}
\]

where

\[
dst(i)=(x'_i,y'_i),
src(i)=(x_i, y_i),
i=0,1,2,3
\]

\cvCPyFunc{GetQuadrangleSubPix}
Retrieves the pixel quadrangle from an image with sub-pixel accuracy.

\cvdefC{
void cvGetQuadrangleSubPix(
\par const CvArr* src,
\par CvArr* dst,
\par const CvMat* mapMatrix );
}\cvdefPy{GetQuadrangleSubPix(src,dst,mapMatrix)-> None}

\begin{description}
\cvarg{src}{Source image}
\cvarg{dst}{Extracted quadrangle}
\cvarg{mapMatrix}{The transformation $2 \times 3$ matrix $[A|b]$ (see the discussion)}
\end{description}

The function \texttt{cvGetQuadrangleSubPix} extracts pixels from \texttt{src} at sub-pixel accuracy and stores them to \texttt{dst} as follows:

\[
dst(x, y)= src( A_{11} x' + A_{12} y' + b_1, A_{21} x' + A_{22} y' + b_2)
\]

where

\[
x'=x-\frac{(width(dst)-1)}{2},
y'=y-\frac{(height(dst)-1)}{2}
\]

and

\[
\texttt{mapMatrix} = \begin{bmatrix}
A_{11} & A_{12} & b_1\\
A_{21} & A_{22} & b_2
\end{bmatrix}
\]

The values of pixels at non-integer coordinates are retrieved using bilinear interpolation. When the function needs pixels outside of the image, it uses replication border mode to reconstruct the values. Every channel of multiple-channel images is processed independently.

\cvCPyFunc{GetRectSubPix}
Retrieves the pixel rectangle from an image with sub-pixel accuracy.

\cvdefC{void cvGetRectSubPix(
\par const CvArr* src,
\par CvArr* dst,
\par CvPoint2D32f center );
}\cvdefPy{GetRectSubPix(src,dst,center)-> None}

\begin{description}
\cvarg{src}{Source image}
\cvarg{dst}{Extracted rectangle}
\cvarg{center}{Floating point coordinates of the extracted rectangle center within the source image. The center must be inside the image}
\end{description}

The function \texttt{cvGetRectSubPix} extracts pixels from \texttt{src}:

\[
dst(x, y) = src(x + \texttt{center.x} - (width(\texttt{dst})-1)*0.5, y + \texttt{center.y} - (height(\texttt{dst} )-1)*0.5)
\]

where the values of the pixels at non-integer coordinates are retrieved
using bilinear interpolation. Every channel of multiple-channel
images is processed independently. While the rectangle center
must be inside the image, parts of the rectangle may be
outside. In this case, the replication border mode is used to get
pixel values beyond the image boundaries.

\cvCPyFunc{LogPolar}
Remaps an image to log-polar space.

\cvdefC{
void cvLogPolar(
\par const CvArr* src,
\par CvArr* dst,
\par CvPoint2D32f center,
\par double M,
\par int flags=CV\_INTER\_LINEAR+CV\_WARP\_FILL\_OUTLIERS );}
\cvdefPy{LogPolar(src,dst,center,M,flags=CV\_INNER\_LINEAR+CV\_WARP\_FILL\_OUTLIERS)-> None}

\begin{description}
\cvarg{src}{Source image}
\cvarg{dst}{Destination image}
\cvarg{center}{The transformation center; where the output precision is maximal}
\cvarg{M}{Magnitude scale parameter. See below}
\cvarg{flags}{A combination of interpolation methods and the following optional flags:
\begin{description}
  \cvarg{CV\_WARP\_FILL\_OUTLIERS}{fills all of the destination image pixels. If some of them correspond to outliers in the source image, they are set to zero}
  \cvarg{CV\_WARP\_INVERSE\_MAP}{See below}
\end{description}}
\end{description}

The function \texttt{cvLogPolar} transforms the source image using the following transformation:

Forward transformation (\texttt{CV\_WARP\_INVERSE\_MAP} is not set):

\[
dst(\phi,\rho) = src(x,y)
\]

Inverse transformation (\texttt{CV\_WARP\_INVERSE\_MAP} is set):

\[
dst(x,y) = src(\phi,\rho)
\]

where

\[
\rho = M \cdot \log{\sqrt{x^2 + y^2}},
\phi=atan(y/x)
\]

The function emulates the human "foveal" vision and can be used for fast scale and rotation-invariant template matching, for object tracking and so forth.
The function can not operate in-place.

\ifC
% Example: Log-polar transformation
\begin{lstlisting}
#include <cv.h>
#include <highgui.h>

int main(int argc, char** argv)
{
    IplImage* src;

    if( argc == 2 && (src=cvLoadImage(argv[1],1) != 0 )
    {
        IplImage* dst = cvCreateImage( cvSize(256,256), 8, 3 );
        IplImage* src2 = cvCreateImage( cvGetSize(src), 8, 3 );
        cvLogPolar( src, dst, cvPoint2D32f(src->width/2,src->height/2), 40,
        CV_INTER_LINEAR+CV_WARP_FILL_OUTLIERS );
        cvLogPolar( dst, src2, cvPoint2D32f(src->width/2,src->height/2), 40,
        CV_INTER_LINEAR+CV_WARP_FILL_OUTLIERS+CV_WARP_INVERSE_MAP );
        cvNamedWindow( "log-polar", 1 );
        cvShowImage( "log-polar", dst );
        cvNamedWindow( "inverse log-polar", 1 );
        cvShowImage( "inverse log-polar", src2 );
        cvWaitKey();
    }
    return 0;
}
\end{lstlisting}

And this is what the program displays when \texttt{opencv/samples/c/fruits.jpg} is passed to it
\includegraphics[width=0.4\textwidth]{pics/logpolar.jpg}
\includegraphics[width=0.4\textwidth]{pics/inv_logpolar.jpg}
\fi

\cvCPyFunc{Remap}
Applies a generic geometrical transformation to the image.

\cvdefC{
void cvRemap(
\par const CvArr* src,
\par CvArr* dst,
\par const CvArr* mapx,
\par const CvArr* mapy,
\par int flags=CV\_INTER\_LINEAR+CV\_WARP\_FILL\_OUTLIERS,
\par CvScalar fillval=cvScalarAll(0) );}
\cvdefPy{Remap(src,dst,mapx,mapy,flags=CV\_INNER\_LINEAR+CV\_WARP\_FILL\_OUTLIERS,fillval=(0,0,0,0))-> None}

\begin{description}
\cvarg{src}{Source image}
\cvarg{dst}{Destination image}
\cvarg{mapx}{The map of x-coordinates (CV\_32FC1 image)}
\cvarg{mapy}{The map of y-coordinates (CV\_32FC1 image)}
\cvarg{flags}{A combination of interpolation method and the following optional flag(s):
\begin{description}
  \cvarg{CV\_WARP\_FILL\_OUTLIERS}{fills all of the destination image pixels. If some of them correspond to outliers in the source image, they are set to \texttt{fillval}}
\end{description}}
\cvarg{fillval}{A value used to fill outliers}
\end{description}

The function \texttt{cvRemap} transforms the source image using the specified map:

\[
\texttt{dst}(x,y) = \texttt{src}(\texttt{mapx}(x,y),\texttt{mapy}(x,y))
\]

Similar to other geometrical transformations, some interpolation method (specified by user) is used to extract pixels with non-integer coordinates.
Note that the function can not operate in-place.

\cvCPyFunc{Resize}
Resizes an image.

\cvdefC{
void cvResize(
\par const CvArr* src,
\par CvArr* dst,
\par int interpolation=CV\_INTER\_LINEAR );}
\cvdefPy{Resize(src,dst,interpolation=CV\_INTER\_LINEAR)-> None}

\begin{description}
\cvarg{src}{Source image}
\cvarg{dst}{Destination image}
\cvarg{interpolation}{Interpolation method:
\begin{description}
\cvarg{CV\_INTER\_NN}{nearest-neigbor interpolation}
\cvarg{CV\_INTER\_LINEAR}{bilinear interpolation (used by default)}
\cvarg{CV\_INTER\_AREA}{resampling using pixel area relation. It is the preferred method for image decimation that gives moire-free results. In terms of zooming it is similar to the \texttt{CV\_INTER\_NN} method}
\cvarg{CV\_INTER\_CUBIC}{bicubic interpolation}
\end{description}}
\end{description}

The function \texttt{cvResize} resizes an image \texttt{src} so that it fits exactly into \texttt{dst}. If ROI is set, the function considers the ROI as supported.

\cvCPyFunc{WarpAffine}
Applies an affine transformation to an image.

\cvdefC{
void cvWarpAffine(
\par const CvArr* src,
\par CvArr* dst,
\par const CvMat* mapMatrix,
\par int flags=CV\_INTER\_LINEAR+CV\_WARP\_FILL\_OUTLIERS,
\par CvScalar fillval=cvScalarAll(0) );
}
\cvdefPy{WarpAffine(src,dst,mapMatrix,flags=CV\_INTER\_LINEAR+CV\_WARP\_FILL\_OUTLIERS,fillval=(0,0,0,0))-> None}

\begin{description}
\cvarg{src}{Source image}
\cvarg{dst}{Destination image}
\cvarg{mapMatrix}{$2\times 3$ transformation matrix}
\cvarg{flags}{A combination of interpolation methods and the following optional flags:
\begin{description}
  \cvarg{CV\_WARP\_FILL\_OUTLIERS}{fills all of the destination image pixels; if some of them correspond to outliers in the source image, they are set to \texttt{fillval}}
  \cvarg{CV\_WARP\_INVERSE\_MAP}{indicates that \texttt{matrix} is inversely
  transformed from the destination image to the source and, thus, can be used
  directly for pixel interpolation. Otherwise, the function finds
  the inverse transform from \texttt{mapMatrix}}}
\end{description}
\cvarg{fillval}{A value used to fill outliers}
\end{description}

The function \texttt{cvWarpAffine} transforms the source image using the specified matrix:

\[
dst(x',y') = src(x,y)
\]

where

\[
\begin{matrix}
\begin{bmatrix}
x'\\
y'
\end{bmatrix} = \texttt{mapMatrix} \cdot \begin{bmatrix}
x\\
y\\
1
\end{bmatrix} & \mbox{if CV\_WARP\_INVERSE\_MAP is not set}\\
\begin{bmatrix}
x\\
y
\end{bmatrix} = \texttt{mapMatrix} \cdot \begin{bmatrix}
x'\\
y'\\
1
\end{bmatrix}& \mbox{otherwise}
\end{matrix}
\]

The function is similar to \cvCPyCross{GetQuadrangleSubPix} but they are not exactly the same. \cvCPyCross{WarpAffine} requires input and output image have the same data type, has larger overhead (so it is not quite suitable for small images) and can leave part of destination image unchanged. While \cvCPyCross{GetQuadrangleSubPix} may extract quadrangles from 8-bit images into floating-point buffer, has smaller overhead and always changes the whole destination image content.
Note that the function can not operate in-place.

To transform a sparse set of points, use the \cvCPyCross{Transform} function from cxcore.

\cvCPyFunc{WarpPerspective}
Applies a perspective transformation to an image.

\cvdefC{
void cvWarpPerspective(
\par const CvArr* src,
\par CvArr* dst,
\par const CvMat* mapMatrix,
\par int flags=CV\_INTER\_LINEAR+CV\_WARP\_FILL\_OUTLIERS,
\par CvScalar fillval=cvScalarAll(0) );
}
\cvdefPy{WarpPerspective(src,dst,mapMatrix,flags=CV\_INNER\_LINEAR+CV\_WARP\_FILL\_OUTLIERS,fillval=(0,0,0,0))-> None}

\begin{description}
\cvarg{src}{Source image}
\cvarg{dst}{Destination image}
\cvarg{mapMatrix}{$3\times 3$ transformation matrix}
\cvarg{flags}{A combination of interpolation methods and the following optional flags:
\begin{description}
  \cvarg{CV\_WARP\_FILL\_OUTLIERS}{fills all of the destination image pixels; if some of them correspond to outliers in the source image, they are set to \texttt{fillval}}
  \cvarg{CV\_WARP\_INVERSE\_MAP}{indicates that \texttt{matrix} is inversely transformed from the destination image to the source and, thus, can be used directly for pixel interpolation. Otherwise, the function finds the inverse transform from \texttt{mapMatrix}}
\end{description}}
\cvarg{fillval}{A value used to fill outliers}
\end{description}

The function \texttt{cvWarpPerspective} transforms the source image using the specified matrix:

\[
\begin{matrix}
\begin{bmatrix}
x'\\
y'
\end{bmatrix} = \texttt{mapMatrix} \cdot \begin{bmatrix}
x\\
y\\
1
\end{bmatrix} & \mbox{if CV\_WARP\_INVERSE\_MAP is not set}\\
\begin{bmatrix}
x\\
y
\end{bmatrix} = \texttt{mapMatrix} \cdot \begin{bmatrix}
x'\\
y'\\
1
\end{bmatrix}& \mbox{otherwise}
\end{matrix}
\]

Note that the function can not operate in-place.
For a sparse set of points use the \cvCPyCross{PerspectiveTransform} function from CxCore.

\fi

\ifCpp

\cvCppFunc{convertMaps}
Converts image transformation maps from one representation to another

\cvdefCpp{void convertMaps( const Mat\& map1, const Mat\& map2,\par
                  Mat\& dstmap1, Mat\& dstmap2,\par
                  int dstmap1type, bool nninterpolation=false );}
\begin{description}
\cvarg{map1}{The first input map of type \texttt{CV\_16SC2} or \texttt{CV\_32FC1} or \texttt{CV\_32FC2}}
\cvarg{map2}{The second input map of type \texttt{CV\_16UC1} or \texttt{CV\_32FC1} or none (empty matrix), respectively}
\cvarg{dstmap1}{The first output map; will have type \texttt{dstmap1type} and the same size as \texttt{src}}
\cvarg{dstmap2}{The second output map}
\cvarg{dstmap1type}{The type of the first output map; should be \texttt{CV\_16SC2}, \texttt{CV\_32FC1} or \texttt{CV\_32FC2}}
\cvarg{nninterpolation}{Indicates whether the fixed-point maps will be used for nearest-neighbor or for more complex interpolation}
\end{description}

The function converts a pair of maps for \cvCppCross{remap} from one representation to another. The following options (\texttt{(map1.type(), map2.type())} $\rightarrow$ \texttt{(dstmap1.type(), dstmap2.type())}) are supported:
\begin{enumerate}
    \item $\texttt{(CV\_32FC1, CV\_32FC1)} \rightarrow \texttt{(CV\_16SC2, CV\_16UC1)}$. This is the most frequently used conversion operation, in which the original floating-point maps (see \cvCppCross{remap}) are converted to more compact and much faster fixed-point representation. The first output array will contain the rounded coordinates and the second array (created only when \texttt{nninterpolation=false}) will contain indices in the interpolation tables.
    \item $\texttt{(CV\_32FC2)} \rightarrow \texttt{(CV\_16SC2, CV\_16UC1)}$. The same as above, but the original maps are stored in one 2-channel matrix.
    \item the reverse conversion. Obviously, the reconstructed floating-point maps will not be exactly the same as the originals.
\end{enumerate}

See also: \cvCppCross{remap}, \cvCppCross{undisort}, \cvCppCross{initUndistortRectifyMap}

\cvCppFunc{getAffineTransform}
Calculates the affine transform from 3 pairs of the corresponding points

\cvdefCpp{Mat getAffineTransform( const Point2f src[], const Point2f dst[] );}

\begin{description}
\cvarg{src}{Coordinates of a triangle vertices in the source image}
\cvarg{dst}{Coordinates of the corresponding triangle vertices in the destination image}
\end{description}

The function calculates the $2 \times 3$ matrix of an affine transform such that:

\[
\begin{bmatrix}
x'_i\\
y'_i
\end{bmatrix}
=
\texttt{map\_matrix}
\cdot
\begin{bmatrix}
x_i\\
y_i\\
1
\end{bmatrix}
\]

where

\[
dst(i)=(x'_i,y'_i),
src(i)=(x_i, y_i),
i=0,1,2
\]

See also: \cvCppCross{warpAffine}, \cvCppCross{transform}

\cvCppFunc{getPerspectiveTransform}
Calculates the perspective transform from 4 pairs of the corresponding points

\cvdefCpp{Mat getPerspectiveTransform( const Point2f src[], \par const Point2f dst[] );}

\begin{description}
\cvarg{src}{Coordinates of a quadrange vertices in the source image}
\cvarg{dst}{Coordinates of the corresponding quadrangle vertices in the destination image}
\end{description}

The function calculates the $3 \times 3$ matrix of a perspective transform such that:

\[
\begin{bmatrix}
t_i x'_i\\
t_i y'_i\\
t_i
\end{bmatrix}
=
\texttt{map\_matrix}
\cdot
\begin{bmatrix}
x_i\\
y_i\\
1
\end{bmatrix}
\]

where

\[
dst(i)=(x'_i,y'_i),
src(i)=(x_i, y_i),
i=0,1,2
\]

See also: \cvCppCross{findHomography}, \cvCppCross{warpPerspective}, \cvCppCross{perspectiveTransform}

\cvCppFunc{getRectSubPix}
Retrieves the pixel rectangle from an image with sub-pixel accuracy

\cvdefCpp{void getRectSubPix( const Mat\& image, Size patchSize,\par
                    Point2f center, Mat\& dst, int patchType=-1 );}
\begin{description}
\cvarg{src}{Source image}
\cvarg{patchSize}{Size of the extracted patch}
\cvarg{center}{Floating point coordinates of the extracted rectangle center within the source image. The center must be inside the image}
\cvarg{dst}{The extracted patch; will have the size \texttt{patchSize} and the same number of channels as \texttt{src}}
\cvarg{patchType}{The depth of the extracted pixels. By default they will have the same depth as \texttt{src}}
\end{description}

The function \texttt{getRectSubPix} extracts pixels from \texttt{src}:

\[
dst(x, y) = src(x + \texttt{center.x} - (\texttt{dst.cols}-1)*0.5, y + \texttt{center.y} - (\texttt{dst.rows}-1)*0.5)
\]

where the values of the pixels at non-integer coordinates are retrieved
using bilinear interpolation. Every channel of multiple-channel
images is processed independently. While the rectangle center
must be inside the image, parts of the rectangle may be
outside. In this case, the replication border mode (see \cvCppCross{borderInterpolate}) is used to extrapolate
the pixel values outside of the image.

See also: \cvCppCross{warpAffine}, \cvCppCross{warpPerspective}

\cvCppFunc{getRotationMatrix2D}
Calculates the affine matrix of 2d rotation.

\cvdefCpp{Mat getRotationMatrix2D( Point2f center, double angle, double scale );}
\begin{description}
\cvarg{center}{Center of the rotation in the source image}
\cvarg{angle}{The rotation angle in degrees. Positive values mean counter-clockwise rotation (the coordinate origin is assumed to be the top-left corner)}
\cvarg{scale}{Isotropic scale factor}
\end{description}

The function calculates the following matrix:

\[
\begin{bmatrix}
\alpha & \beta & (1-\alpha) \cdot \texttt{center.x} - \beta \cdot \texttt{center.y} \\
-\beta & \alpha & \beta \cdot \texttt{center.x} - (1-\alpha) \cdot \texttt{center.y}
\end{bmatrix}
\]

where

\[
\begin{array}{l}
\alpha = \texttt{scale} \cdot \cos \texttt{angle},\\
\beta = \texttt{scale} \cdot \sin \texttt{angle}
\end{array}
\]

The transformation maps the rotation center to itself. If this is not the purpose, the shift should be adjusted.

See also: \cvCppCross{getAffineTransform}, \cvCppCross{warpAffine}, \cvCppCross{transform}

\cvCppFunc{invertAffineTransform}
Inverts an affine transformation

\cvdefCpp{void invertAffineTransform(const Mat\& M, Mat\& iM);}
\begin{description}
\cvarg{M}{The original affine transformation}
\cvarg{iM}{The output reverse affine transformation}
\end{description}

The function computes inverse affine transformation represented by $2 \times 3$ matrix \texttt{M}:

\[\begin{bmatrix}
a_{11} & a_{12} & b_1 \\
a_{21} & a_{22} & b_2
\end{bmatrix}
\]

The result will also be a $2 \times 3$ matrix of the same type as \texttt{M}.

\cvCppFunc{remap}
Applies a generic geometrical transformation to an image.

\cvdefCpp{void remap( const Mat\& src, Mat\& dst, const Mat\& map1, const Mat\& map2,\par
            int interpolation, int borderMode=BORDER\_CONSTANT,\par
            const Scalar\& borderValue=Scalar());}
\begin{description}
\cvarg{src}{Source image}
\cvarg{dst}{Destination image. It will have the same size as \texttt{map1} and the same type as \texttt{src}}
\cvarg{map1}{The first map of either \texttt{(x,y)} points or just \texttt{x} values having type \texttt{CV\_16SC2}, \texttt{CV\_32FC1} or \texttt{CV\_32FC2}. See \cvCppCross{convertMaps} for converting floating point representation to fixed-point for speed.}
\cvarg{map2}{The second map of \texttt{y} values having type \texttt{CV\_16UC1}, \texttt{CV\_32FC1} or none (empty map if map1 is \texttt{(x,y)} points), respectively}
\cvarg{interpolation}{The interpolation method, see \cvCppCross{resize}. The method \texttt{INTER\_AREA} is not supported by this function}
\cvarg{borderMode}{The pixel extrapolation method, see \cvCppCross{borderInterpolate}. When the\\ \texttt{borderMode=BORDER\_TRANSPARENT}, it means that the pixels in the destination image that corresponds to the "outliers" in the source image are not modified by the function}
\cvarg{borderValue}{A value used in the case of a constant border. By default it is 0}
\end{description}

The function \texttt{remap} transforms the source image using the specified map:

\[
\texttt{dst}(x,y) = \texttt{src}(map_x(x,y),map_y(x,y))
\]

Where values of pixels with non-integer coordinates are computed using one of the available interpolation methods. $map_x$ and $map_y$
can be encoded as separate floating-point maps in $map_1$ and $map_2$ respectively, or interleaved floating-point maps of $(x,y)$ in $map_1$, or
fixed-point maps made by using \cvCppCross{convertMaps}. The reason you might want to convert from floating to fixed-point
representations of a map is that they can yield much faster (~2x) remapping operations. In the converted case, $map_1$ contains pairs
\texttt{(cvFloor(x), cvFloor(y))} and $map_2$ contains indices in a table of interpolation coefficients.

This function can not operate in-place.

\cvCppFunc{resize}
Resizes an image

\cvdefCpp{void resize( const Mat\& src, Mat\& dst,\par
             Size dsize, double fx=0, double fy=0,\par
             int interpolation=INTER\_LINEAR );}
\begin{description}
\cvarg{src}{Source image}
\cvarg{dst}{Destination image. It will have size \texttt{dsize} (when it is non-zero) or the size computed from \texttt{src.size()}
and \texttt{fx} and \texttt{fy}. The type of \texttt{dst} will be the same as of \texttt{src}.}
\cvarg{dsize}{The destination image size. If it is zero, then it is computed as:
\[\texttt{dsize = Size(round(fx*src.cols), round(fy*src.rows))}\].
Either \texttt{dsize} or both \texttt{fx} or \texttt{fy} must be non-zero.}
\cvarg{fx}{The scale factor along the horizontal axis. When 0, it is computed as
\[\texttt{(double)dsize.width/src.cols}\]}
\cvarg{fy}{The scale factor along the vertical axis. When 0, it is computed as
\[\texttt{(double)dsize.height/src.rows}\]}
\cvarg{interpolation}{The interpolation method:
\begin{description}
\cvarg{INTER\_NEAREST}{nearest-neighbor interpolation}
\cvarg{INTER\_LINEAR}{bilinear interpolation (used by default)}
\cvarg{INTER\_AREA}{resampling using pixel area relation. It may be the preferred method for image decimation, as it gives moire-free results. But when the image is zoomed, it is similar to the \texttt{INTER\_NEAREST} method}
\cvarg{INTER\_CUBIC}{bicubic interpolation over 4x4 pixel neighborhood}
\cvarg{INTER\_LANCZOS4}{Lanczos interpolation over 8x8 pixel neighborhood}
\end{description}}
\end{description}

The function \texttt{resize} resizes an image \texttt{src} down to or up to the specified size.
Note that the initial \texttt{dst} type or size are not taken into account. Instead the size and type are derived from the \texttt{src}, \texttt{dsize}, \texttt{fx} and \texttt{fy}. If you want to resize \texttt{src} so that it fits the pre-created \texttt{dst}, you may call the function as:

\begin{lstlisting}
// explicitly specify dsize=dst.size(); fx and fy will be computed from that.
resize(src, dst, dst.size(), 0, 0, interpolation);
\end{lstlisting}

If you want to decimate the image by factor of 2 in each direction, you can call the function this way:

\begin{lstlisting}
// specify fx and fy and let the function to compute the destination image size.
resize(src, dst, Size(), 0.5, 0.5, interpolation);
\end{lstlisting}

See also: \cvCppCross{warpAffine}, \cvCppCross{warpPerspective}, \cvCppCross{remap}.

\cvCppFunc{warpAffine}
Applies an affine transformation to an image.

\cvdefCpp{void warpAffine( const Mat\& src, Mat\& dst,\par
                 const Mat\& M, Size dsize,\par
                 int flags=INTER\_LINEAR,\par
                 int borderMode=BORDER\_CONSTANT,\par
                 const Scalar\& borderValue=Scalar());}
\begin{description}
\cvarg{src}{Source image}
\cvarg{dst}{Destination image; will have size \texttt{dsize} and the same type as \texttt{src}}
\cvarg{M}{$2\times 3$ transformation matrix}
\cvarg{dsize}{Size of the destination image}
\cvarg{flags}{A combination of interpolation methods, see \cvCppCross{resize}, and the optional flag \texttt{WARP\_INVERSE\_MAP} that means that \texttt{M} is the inverse transformation ($\texttt{dst}\rightarrow\texttt{src}$)}
\cvarg{borderMode}{The pixel extrapolation method, see \cvCppCross{borderInterpolate}. When the \\ \texttt{borderMode=BORDER\_TRANSPARENT}, it means that the pixels in the destination image that corresponds to the "outliers" in the source image are not modified by the function}
\cvarg{borderValue}{A value used in case of a constant border. By default it is 0}
\end{description}

The function \texttt{warpAffine} transforms the source image using the specified matrix:

\[
\texttt{dst}(x,y) = \texttt{src}(\texttt{M}_{11} x + \texttt{M}_{12} y + \texttt{M}_{13}, \texttt{M}_{21} x + \texttt{M}_{22} y + \texttt{M}_{23})
\]
when the flag \texttt{WARP\_INVERSE\_MAP} is set. Otherwise, the transformation is first inverted with \cvCppCross{invertAffineTransform} and then put in the formula above instead of \texttt{M}.
The function can not operate in-place.

See also: \cvCppCross{warpPerspective}, \cvCppCross{resize}, \cvCppCross{remap}, \cvCppCross{getRectSubPix}, \cvCppCross{transform}

\cvCppFunc{warpPerspective}
Applies a perspective transformation to an image.

\cvdefCpp{void warpPerspective( const Mat\& src, Mat\& dst,\par
                      const Mat\& M, Size dsize,\par
                      int flags=INTER\_LINEAR,\par
                      int borderMode=BORDER\_CONSTANT,\par
                      const Scalar\& borderValue=Scalar());}
\begin{description}
\cvarg{src}{Source image}
\cvarg{dst}{Destination image; will have size \texttt{dsize} and the same type as \texttt{src}}
\cvarg{M}{$3\times 3$ transformation matrix}
\cvarg{dsize}{Size of the destination image}
\cvarg{flags}{A combination of interpolation methods, see \cvCppCross{resize}, and the optional flag \texttt{WARP\_INVERSE\_MAP} that means that \texttt{M} is the inverse transformation ($\texttt{dst}\rightarrow\texttt{src}$)}
\cvarg{borderMode}{The pixel extrapolation method, see \cvCppCross{borderInterpolate}. When the \\ \texttt{borderMode=BORDER\_TRANSPARENT}, it means that the pixels in the destination image that corresponds to the "outliers" in the source image are not modified by the function}
\cvarg{borderValue}{A value used in case of a constant border. By default it is 0}
\end{description}

The function \texttt{warpPerspective} transforms the source image using the specified matrix:

\[
\texttt{dst}(x,y) = \texttt{src}\left(\frac{M_{11} x + M_{12} y + M_{13}}{M_{31} x + M_{32} y + M_{33}},
    \frac{M_{21} x + M_{22} y + M_{23}}{M_{31} x + M_{32} y + M_{33}}\right)
\]
when the flag \texttt{WARP\_INVERSE\_MAP} is set. Otherwise, the transformation is first inverted with \cvCppCross{invert} and then put in the formula above instead of \texttt{M}.
The function can not operate in-place.

See also: \cvCppCross{warpAffine}, \cvCppCross{resize}, \cvCppCross{remap}, \cvCppCross{getRectSubPix}, \cvCppCross{perspectiveTransform}

\fi


\documentclass{article}
\usepackage{fullpage}
\usepackage{algorithmic}
\usepackage{amssymb}
\usepackage{url}
\usepackage{color}

\providecommand{\e}[1]{\ensuremath{\times 10^{#1}}}

\begin{document}

\title{Band Cholesky Method implementation}
\date{April 18, 2011}
\author{Bysiek Mateusz, Witan Maciej (Computer Science A/R)}
\maketitle

\section{Task}

Design a MATLAB function that will solve a system of equations $Ax=B$ using the
band Cholesky method.

For each computed solution $\tilde{x}$ of the system $Ax=b$, form the relative
residual error:
\[ \frac{ \|b-A\tilde{x}\|_2 }{ \|A\|_F \|\tilde{x}\|_2 } \]

\section{Introdution to the method}

We have decided to design a modular function ``Band Cholesky Solver'', or $BCS$,
which will solve the problem iteratively, step by step. Divide and conquer
method allowed us to tackle the problem as a team, because on one hand both
team members had their own part of the work to do, but on the other hand we
then put the algorithms together, and synchronized them. The result is presented
below.

For this method, the input matrix $A$ must be symmetric positive definite, $A
\in \mathbb{R}^{n \times n}$. It also must be banded, with bandwith $w$. Matrix
$B$ should be of size $n \times m$.

\subsection{Fragmentation of the problem}

At first, the problem was $Ax = B$, where $B \in \mathbb{R}^{n \times m}$ had
$m$ columns: $b_1 \ldots b_m$. The problem was splitted to $m$ smaller
subproblems, such that: \[ \forall i \in \{1 \ldots m\} : Ax_i = b_i \]

Then, we did the following four steps for each column $b_i$, and received $m$
partial solutions.

\subsection{Cholesky decomposition}

Symmetric positive definite matrix $A \in \mathbb{R}^{n \times n}$, with bandwith $w$ is
given to the following algorithm.

\begin{algorithmic}
\STATE $L=A$
\FOR{$j = 1:n$}
	\FOR{$k = max(1,j-w):j-1$}
		\STATE $\lambda = min(k+w,n)$
		\STATE $L(j:\lambda,j) = L(j:\lambda,j) - L(j,k) L(j:\lambda,k)$
	\ENDFOR
	\STATE $\lambda = min(j+w,n)$
	\STATE $L(j:\lambda,j) = L(j:\lambda,j) / \sqrt{A(j,j)}$
\ENDFOR
\STATE $L = L - triu(L, 1)$
\end{algorithmic}

This algorithm ends with creation of matrix $L$, such that $A = LL^T$.
Initially, $L = A$, but L is overwritten step by step with correct values in the
lower-triangular area. After that, values over the diagonal are cleared.

\subsection{Solution of the preliminary system of equations}

Banded, lower triangular matrix $L \in \mathbb{R}^{n \times n}$ with bandwidth $w$ is
given to the algorithm below. Also, vector $b$ of length $n$ is given.

\begin{algorithmic}
\STATE $y = b$
\FOR{$j = 1:n$}
	\FOR{$i = max(j, j-w):j-1$}
		\STATE $y(j) = y(j) - L(j,i) y(i)$
	\ENDFOR
	\STATE $y(j) = y(j)/L(j,j)$
\ENDFOR
\end{algorithmic}

Resulting vector $y \in \mathbb{R}^{n \times 1}$ is an intermediate solution
that is used in final calculations.

\subsection{Calculation of the final result}

The intermediate solution $y$ and upper-triangular matrix $U = L^T$ with
bandwith $w$ is given as the input.

\begin{algorithmic}
\STATE $x=y$
\FOR{$j = n:-1:1$}
	\STATE $x(j) = x(j)/U(j,j)$
	\FOR{$i = max(1,j-w):j-1$}
		\STATE $x(i) = x(i) - U(i,j) x(j)$
	\ENDFOR
\ENDFOR
\end{algorithmic}

\subsection{Calculating the error}

For every result, error $e$ is equal to $\|b-A\tilde{x}\|_2 /( \|A\|_F
\|\tilde{x}\|_2 )$.

\begin{algorithmic}
\STATE $e = norm(b-Ax,2) / (norm(A,"fro") norm(x,2))$
\end{algorithmic}

\section{Implementation}

The above-mentioned algorithms were implemented as separate functions.

\begin{enumerate}
	\item To split problem into smaller, we simply used $for$ loop.
	\item Cholesky decomposition: $cholband.m$
	\item Solution of the preliminary system of equations: $lsolve.m$
	\item Calculation of the final result: $usolve.m$
	\item Error estimation: $residerr.m$
\end{enumerate}

An aggregate function $BCS$ is used to launch those operations in sequence, as
well as to calculate residual error.

We also have constructed the following support files
\begin{itemize}
	\item solver that uses built-in functions, for comparison: $STS.m$
	\item menu to make using using our solution easier: $BCS\_menu.m$
	\item examples: $ex1.m$ $ex2.m$ $ex3.m$
\end{itemize}

\subsection{Examples}

For better presentation of how our solution works, we have included 3 examples
in our project.

\subsubsection{First example}

A is based on matrix of ones in the following way:

\begin{algorithmic}
\STATE $A = ones(8)$
\STATE $A = A-tril(A,-2)-triu(A,2)$
\STATE $A = A*A$
\STATE $A = A+eye(8)$
\end{algorithmic}

Vector b is obtained by $A \times ones(8, 1)$, and solving for $x$ using Band
Cholesky Solver gives exacly that vector.

\[ A = \left[
\begin{array}{cccccccc}
3 & 2 & 1 & 0 & 0 & 0 & 0 & 0 \\
2 & 4 & 2 & 1 & 0 & 0 & 0 & 0 \\
1 & 2 & 4 & 2 & 1 & 0 & 0 & 0 \\
0 & 1 & 2 & 4 & 2 & 1 & 0 & 0 \\
0 & 0 & 1 & 2 & 4 & 2 & 1 & 0 \\
0 & 0 & 0 & 1 & 2 & 4 & 2 & 1 \\
0 & 0 & 0 & 0 & 1 & 2 & 4 & 2 \\
0 & 0 & 0 & 0 & 0 & 1 & 2 & 3 \\
\end{array} \right],
b = \left( \begin{array}{c}
6 \\ 9 \\ 10 \\ 10 \\ 10 \\ 10 \\ 9 \\ 6
\end{array} \right),
x = \left( \begin{array}{c}
1\\ 1\\ 1\\ 1\\ 1\\ 1\\ 1\\ 1
\end{array} \right)
\]

Becasuse the relative error is very small, i.e $e =  6.8776\e{-16}$ indicating
that some deviations in results can appear not closer than at $16^{th}$ decimal
place this is thanks to the matrix A being well-conditioned as condtion number
of matrix A is very close to the unity, namely: $cond(A) = 9.2909$.

\subsubsection{Second example}

A is based on product of Pascal and Hilbert matrices of size 11 in the
following way:

\begin{algorithmic}
\STATE $n = 11$
\STATE $A = hilb(n). * pascal(n)$
\STATE $A = A-tril(A,-2)-triu(A,2)$
\STATE $A = A^T * A$
\end{algorithmic}

Vector b is randolmy generated, but every time the same vector is obtained
thanks to $randn("state", 0)$ instruction:

\begin{algorithmic}
\STATE $randn("state", 0)$
\STATE $b = randn(n, 1)$
\end{algorithmic}

\[ b = \left(
\begin{array}{c}
  -1.2248\e{+00} \\
   7.6384\e{-01} \\
  -4.1902\e{-01} \\
   5.0462\e{-01} \\
   5.7743\e{-01} \\
   5.4095\e{-01} \\
  -6.0212\e{-01} \\
  -4.2490\e{-02} \\
   1.8388\e{-01} \\
  -6.5878\e{-02}
\end{array} \right)
\]

Solving for $x$ using Band Cholesky Solver the vector:

\[ x_{BCS} = \left(
\begin{array}{c}
  -5.9584181126\underline{1340}\e{+00} \\
  +7.5253648228\underline{5158}\e{+00} \\
  -1.2786997060\underline{7469}\e{-01} \\
  -2.8406092064\underline{3483}\e{+00} \\
  +1.5221562124\underline{2016}\e{+00} \\
  +1.1955147494\underline{0896}\e{-01} \\
  -5.6776267273\underline{6740}\e{-01} \\
  +2.8906110676\underline{8170}\e{-01} \\
  +8.9938809722\underline{0923}\e{-03} \\
  -9.1118417264\underline{6874}\e{-02} \\
  +4.7840224822\underline{5803}\e{-02}
\end{array} \right)
\]

whereas the vector obtaied by using build-in Octave function $A \backslash b$:

\[ x_{OCT} = \left(
\begin{array}{c}
  -5.9584181126\underline{3058}\e{+00} \\
  +7.5253648228\underline{7429}\e{+00} \\
  -1.2786997060\underline{0663}\e{-01} \\
  -2.8406092064\underline{5384}\e{+00} \\
  +1.5221562124\underline{2985}\e{+00} \\
  +1.1955147494\underline{1742}\e{-01} \\
  -5.6776267274\underline{0454}\e{-01} \\
  +2.8906110677\underline{0057}\e{-01} \\
  +8.9938809722\underline{6832}\e{-03} \\
  -9.1118417265\underline{2829}\e{-02} \\
  +4.7840224822\underline{8929}\e{-02}
\end{array} \right)
\]

Becasuse the relative error is high, i.e $e =  3.59547536244005\e{-12}$
indicating that some deviations in results can appear even around $11^{th}$
decimal place this is due to the matrix A being ill-conditioned as condtion
number of matrix A is high $cond(A) = 1.08771091073209\e{9}$.

\subsubsection{Example no.3}

A is based on inverted magic square of size 9 in the following way:

\begin{algorithmic}
\STATE $n=9$
\STATE $A=magic(n)$
\STATE $A=inv(A)$
\STATE $A=(A-triu(A,3))-tril(A,-3)$
\STATE $A=A^T * A$
\end{algorithmic}

Vector $b$ is randolmy generated, but every time the same vector is obtained
thanks to $randn("state", 0)$ instruction:

\begin{algorithmic}
\STATE $randn("state", 0)$
\STATE $b = randn(n, 1)$
\end{algorithmic}

\[ b = \left( \begin{array}{c}
  -1.2248\e{+00} \\
   7.6384\e{-01} \\
  -4.1902\e{-01} \\
   5.0462\e{-01} \\
   5.7743\e{-01} \\
   5.4095\e{-01} \\
  -6.0212\e{-01} \\
  -4.2490\e{-02} \\
   1.8388\e{-01}
\end{array} \right)
\]

Solving for x using Band Cholesky Solver, the vector:

\[ x_{BCS} = \left(
\begin{array}{c}
  -8.54210613880\underline{332}\e{+06} \\
  -3.54982602561\underline{955}\e{+06} \\
  -2.32076201267\underline{229}\e{+06} \\
  -2.73885343436\underline{123}\e{+06} \\
  +2.91011321147\underline{994}\e{+07} \\
  -6.01576142237\underline{722}\e{+06} \\
  -6.06596003263\underline{988}\e{+06} \\
  -3.81886151192\underline{721}\e{+06} \\
  +1.37176843830\underline{206}\e{+07}
\end{array} \right)
\]

Whereas a vector obtaied by using build-in Octave function $A \backslash b$:

\[ x_{OCT} = \left(
\begin{array}{c}
%tez tylko 3 zaznaczyc (ww sensie w calym vektorze)
  -8.54210613880\underline{413}\e{+06} \\
  -3.54982602561\underline{894}\e{+06} \\
  -2.32076201267\underline{168}\e{+06} \\
  -2.73885343436\underline{059}\e{+06} \\
  +2.91011321147\underline{946}\e{+07} \\
  -6.01576142237\underline{623}\e{+06} \\
  -6.06596003263\underline{887}\e{+06} \\
  -3.81886151192\underline{656}\e{+06} \\
  +1.37176843830\underline{185}\e{+07}
\end{array} \right)
\]

Becasuse the relative error is quite significant, i.e $e =
1.62773786927727\e{-13}$ indicating that some deviations in results can appear
even around $12^{th}$ decimal place this is due to the matrix A being
ill-conditioned as condtion number of matrix A is quite high $cond(A) =
6.17597814680354e+04$.

\subsection{How to use the function in Octave/MATLAB}

Usage of aggregate function $BCS$:

\[ [result\_matrix, error\_vector] = BCS(source\_matrix, parameter\_matrix,
bandwith\_value) \]

The script $BCS\_menu$ can be used for easier access to the function.

\section{Sources}

\begin{itemize}
	\item Algorithm 4.3.5, p. 155, G.H.Golub and Ch.F. Van Loan -
	\emph{Matrix Computations}, Third Edition, The Johns Hopkins University Press,
	Baltimore and London, 1996
	\item Article \emph{Forward Substitution}, retrieved: 7th
	April 2011, available at:
	\url{https://ece.uwaterloo.ca/~ece204/howtos/forward/}
	\item Fortuna, Macukow, Wąsowski - \emph{Metody Numeryczne}
\end{itemize}

\end{document}


% To be compiled by XeLaTeX, preferably under TeX Live.
% LaTeX source for ``Yanqi Lake Lectures on Algebra'' Part III.
% Copyright 2019  李文威 (Wen-Wei Li).
% Permission is granted to copy, distribute and/or modify this
% document under the terms of the Creative Commons
% Attribution-NonCommercial 4.0 International (CC BY-NC 4.0)
% https://creativecommons.org/licenses/by-nc/4.0/

% To be included
\chapter{Primary decompositions}

We shall follow \cite[\S 3]{Eis95} and \cite[\S 8]{Mat80} closely.

\section{The support of a module}
Fix a ring $R$. For any $R$-module $M$ and $x \in M$, we define the \emph{annihilator} $\text{ann}_R(x) := \{ r \in R: rx=0 \}$; it is an ideal of $R$. Also define $\text{ann}_R(M) := \{r \in R: rM=0 \} = \bigcap_{x \in M} \text{ann}_R(x)$. \index{$\text{ann}_R(M), \text{ann}_R(x)$}

\begin{definition}\index{support}\index{SuppM@$\Supp(M)$}
	The \emph{support} of an $R$-module $M$ is
	\[ \Supp(M) := \left\{ \mathfrak{p} \in \Spec(R): M_{\mathfrak{p}} \neq 0 \right\}. \]
\end{definition}
Let us unwind the definition: $\mathfrak{p} \notin \Supp(M)$ means that every $x \in M$ maps to $0 \in M_{\mathfrak{p}}$, equivalently $\text{ann}_R(x) \not\subset \mathfrak{p}$. To test whether $\mathfrak{p} \notin \Supp(M)$, we only need to check the foregoing condition for $x$ ranging over a generating set of $M$.

\begin{proposition}
	If $M$ is finitely generated then $\Supp(M) = V(\mathrm{ann}_R(M))$; in particular it is Zariski-closed in $\Spec(R)$.
\end{proposition}
\begin{proof}
	Suppose $M = Rx_1 + \cdots + Rx_n$. Set $I_i := \text{ann}_R(x_i)$ and observe that $\bigcap_{i=1}^n I_i = \text{ann}_R(M)$. Then $\mathfrak{p} \in \Supp(M)$ if and only if $\mathfrak{p} \supset I_i$ for some $i$, that is, $\mathfrak{p} \in \bigcup_{i=1}^n V(I_i)$. To conclude, note that $V(I) \cup V(J) = V(IJ) = V(I \cap J)$ for any ideals $I, J \subset R$ (an easy exercise).
\end{proof}

Finite generation is needed in the result above. Consider the $\Z$-module $M := \bigoplus_{a \geq 1} \Z/p^a \Z$. Its support is $\{p\}$ but $\mathrm{ann}(M) = \{0\}$.

\begin{proposition}
	For an exact sequence $0 \to M' \to M \to M'' \to 0$ we have $\Supp(M) = \Supp(M') \cup \Supp(M'')$. For arbitrary direct sums we have $\Supp(\bigoplus_i M_i) = \bigcup_i \Supp(M_i)$.
\end{proposition}
\begin{proof}
	Again, we use the exactness of localization for the first assertion. For $\mathfrak{p} \in \Spec(R)$ we have an exact $0 \to M'_{\mathfrak{p}} \to M_{\mathfrak{p}} \to M''_{\mathfrak{p}} \to 0$, hence $M_{\mathfrak{p}} \neq 0$ if and only if $\mathfrak{p} \in \Supp(M') \cup \Supp(M'')$. The second assertion is obvious.
\end{proof}

From an $R$-module $M$, one can build a ``field of modules'' over $\Spec(R)$ by assigning to each $\mathfrak{p}$ the $R_{\mathfrak{p}}$-module $M_{\mathfrak{p}}$, and $\Supp(M)$ is precisely the subset of $M_{\mathfrak{p}}$ over which the field is non-vanishing. This is how the support arises in algebraic geometry. A more precise description will require the notion of quasi-coherent sheaves on schemes.

\begin{exercise}
	Let $M$, $N$ be finitely generated $R$-modules. Show that $\Supp(M \dotimes{R} N) = \Supp(M) \cap \Supp(N)$. Hint: since localization commutes with $\otimes$, it suffices to prove that $M \dotimes{R} N \neq \{0\}$ when $M, N$ are both nonzero finitely generated modules over a local ring $R$. Nakayama's Lemma implies that $M \dotimes{R} \Bbbk$ and $\Bbbk \dotimes{R} N$ are both nonzero where $\Bbbk$ is the residue field of $R$. Now
	\[ (M \dotimes{R} \Bbbk) \dotimes{\Bbbk} (\Bbbk \dotimes{R} N) \simeq M \dotimes{R} (\Bbbk \dotimes{\Bbbk} \Bbbk) \dotimes{R} N \simeq (M \dotimes{R} N) \dotimes{R} \Bbbk \]
	is nonzero.
\end{exercise}

\section{Associated primes}
Throughout this section, $R$ will be a Noetherian ring.

\begin{definition}\index{associated prime}\index{$\text{Ass}(M)$}
	A prime ideal $\mathfrak{p}$ is said to be an \emph{associated prime} of an $R$-module $M$ if $\text{ann}_R(x) = \mathfrak{p}$ for some $x \in M$; equivalently, $R/\mathfrak{p}$ embeds into $M$. Denote the set of associated primes of $M$ by $\text{Ass}(M)$.
\end{definition}

\begin{example}
	For the $\Z$-module $M := \Z/n\Z$ with $n \in \Z_{> 1}$, one easily checks that $\text{Ass}(M)$ is the set of prime factors of $n$.
\end{example}

\begin{lemma}\label{prop:Ass-maximal}
	Consider the set $\mathcal{S} := \{ \mathrm{ann}_R(x) : x \in M, \; x \neq 0 \}$ of ideals, partially ordered by inclusion. Every maximal element in $\mathcal{S}$ is prime.
\end{lemma}
\begin{proof}
	Let $\mathfrak{p} = \text{ann}_R(x)$ be a maximal element of $\mathcal{S}$ and suppose $ab \in \mathfrak{p}$. If $b \notin \mathfrak{p}$, then
	\[ bx \neq 0, \quad abx = 0, \quad R \neq \text{ann}_R(bx) \supset \text{ann}_R(x) = \mathfrak{p}. \]
	Hence $a \in \text{ann}_R(bx) = \mathfrak{p}$ by the maximality of $\mathfrak{p}$ in $\mathcal{S}$.
\end{proof}

\begin{definition}\index{zero divisor}
	Call $r \in R$ a \emph{zero divisor} on $M$ if $rx=0$ for some $x \in M \smallsetminus \{0\}$.
\end{definition}

\begin{theorem}\label{prop:Ass-properties}
	Let $M$ be an $R$-module.
	\begin{enumerate}[(i)]
		\item We have $M = \{0\}$ if and only if $\mathrm{Ass}(M) = \emptyset$.
		\item The union of all $\mathfrak{p} \in \mathrm{Ass}(M)$ equals the set of zero divisors on $M$.
		\item For any multiplicative subset $S \subset R$, we have
			\[ \mathrm{Ass}(M[S^{-1}]) = \left\{ \mathfrak{p}[S^{-1}] : \mathfrak{p} \in \mathrm{Ass}(M), \; \mathfrak{p} \cap S = \emptyset \right\} . \]
		\item If $0 \to M' \to M \to M''$ is exact, then $\mathrm{Ass}(M') \subset \mathrm{Ass}(M) \subset \mathrm{Ass}(M') \cup \mathrm{Ass}(M'')$.
	\end{enumerate}
\end{theorem}
\begin{proof}
	\begin{asparaenum}[(i)]
	\item Clearly $\text{Ass}(\{0\}) = \emptyset$. If $M \neq 0$, the set $\mathcal{S}$ in Lemma \ref{prop:Ass-maximal} is then nonempty, hence contains a maximal element $\mathfrak{p}$ because $R$ is Noetherian; this yields $\mathfrak{p} \in \text{Ass}(M)$.

	\item Elements of any $\mathfrak{p} \in \text{Ass}(M)$ are all zero divisors by the very definition of associated primes. Conversely, if $r \in \text{ann}_R(x)$ for some $x \in M \smallsetminus \{0\}$, there must exist some maximal element $\mathfrak{p}$ of $\mathcal{S}$ with $\mathfrak{p} \supset \text{ann}_R(x)$ as $R$ is Noetherian; so $\mathfrak{p}$ is the required associated prime containing $r$.

	\item If $\mathfrak{p} \in \Spec(R)$, $\mathfrak{p} \cap S = \emptyset$ and there is some $R/\mathfrak{p} \hookrightarrow M$, then
	\[ \{0\} \neq R[S^{-1}]/\mathfrak{p}[S^{-1}] \simeq (R/\mathfrak{p})[S^{-1}] \hookrightarrow M[S^{-1}] \]
	by the exactness of localizations, hence $\mathfrak{p}[S^{-1}] \in \text{Ass}(M[S^{-1}])$. Conversely, every element of $\text{Ass}(M[S^{-1}])$ has the form $\mathfrak{p}[S^{-1}]$ for some $\mathfrak{p} \in \Spec(R)$ disjoint from $S$. Also recall that $\mathfrak{p}$ equals the preimage of $\mathfrak{p}[S^{-1}]$ under $R \to R[S^{-1}]$. There exist $x \in M$ and $s \in S$ such that $\mathfrak{p}[S^{-1}] = \text{ann}_{R[S^{-1}]}(x/s) = \text{ann}_{R[S^{-1}]}(x)$. Ideals in a Noetherian ring being finitely generated, we infer that $\exists t \in S$ with $\mathfrak{p} \subset \text{ann}_R(tx)$. It remains to show $\mathfrak{p} = \text{ann}_R(tx)$. If $rtx=0$ for some $r \in R$, then $r$ maps into $r/1 \in \mathfrak{p}[S^{-1}] = \text{ann}_{R[S^{-1}]}(x)$; thus $r \in \mathfrak{p}$.

	\item It suffices to treat the second $\subset$. Suppose that $\mathfrak{p} \in \text{Ass}(M)$ and $R/\mathfrak{p} \simeq N$ for some submodule $N \subset M$. Identify $M'$ with $\Ker(M \to M'')$. If $M' \cap N = \{0\}$ then $R/\mathfrak{p} \simeq N \hookrightarrow M''$, so $\mathfrak{p} \in \text{Ass}(M'')$. If there exists $x \in M' \cap N \subset N$ with $x \neq 0$, then we have $\text{ann}_R(x) = \mathfrak{p}$ since $N \simeq R/\mathfrak{p}$ and $\mathfrak{p}$ is prime; in this case $\mathfrak{p} \in \text{Ass}(M')$.
	\end{asparaenum}
\end{proof}

We remark that $0 \to M' \to M \to M'' \to 0$ being exact does not imply $\text{Ass}(M) = \text{Ass}(M') \cup \text{Ass}(M'')$. To see this, consider $R=\Z$ and $0 \to \Z \xrightarrow{p} \Z \to \Z/p\Z \to 0$ for some prime number $p$.

\begin{exercise}
	Show that $\text{Ass}(M_1 \oplus M_2) = \text{Ass}(M_1) \cup \text{Ass}(M_2)$.
\end{exercise}

\begin{theorem}\label{prop:Supp-Ass}
	For every $R$-module $M$ we have $\Supp(M) = \bigcup_{\mathfrak{p} \in \mathrm{Ass}(M)} V(\mathfrak{p})$, in particular $\mathrm{Ass}(M) \subset \Supp(M)$.	Furthermore, every minimal element of $\Supp(M)$ with respect to inclusion is actually a minimal element of $\mathrm{Ass}(M)$.
\end{theorem}
\begin{proof}
	For any prime $\mathfrak{q}$ we have $M_{\mathfrak{q}} \neq 0 \iff \text{Ass}(M_{\mathfrak{q}}) \neq \emptyset$, and the latter condition holds precisely when there exists $\mathfrak{p} \in \text{Ass}(M)$ with $\mathfrak{p} \cap (R \smallsetminus \mathfrak{q}) = \emptyset$, i.e. $\mathfrak{q} \supset \mathfrak{p}$. This proves the first assertion. The second assertion is a direct consequence.
\end{proof}

Observe that if $\mathfrak{p} \in \Supp(M)$, then $\mathfrak{q} \supset \mathfrak{p} \implies \mathfrak{q} \in \Supp(M)$: the reason is that
\begin{gather}\label{eqn:localization-in-stages}
	M_{\mathfrak{p}} = (M_{\mathfrak{q}})_{\mathfrak{p}R_{\mathfrak{q}}}
\end{gather}
thus the occurrence of non-minimal elements in $\Supp(M)$ is unsurprising. In contrast, the non-minimal elements in $\text{Ass}(M)$ are somehow mysterious. These non-minimal associated primes are called \emph{embedded primes}. \index{embedded prime}

\begin{exercise}
	Prove the formula \eqref{eqn:localization-in-stages} of ``localization in stages''.
\end{exercise}

Hereafter we impose finite generation on $M$. This implies $M$ is Noetherian.
\begin{theorem}
	Let $M$ be a finitely generated $R$-module. There exists a chain $M = M_n \supset M_{n-1} \supset \cdots \supset M_0 = \{0\}$ of submodules such that for every $0 < i \leq n$, the subquotient $M_i/M_{i-1}$ is isomorphic to $R/\mathfrak{p}_i$ for some prime ideal $\mathfrak{p}_i$.

	Furthermore we have $\mathrm{Ass}(M) \subset \{\mathfrak{p}_1, \ldots, \mathfrak{p}_n \}$; in particular $\mathrm{Ass}(M)$ is a finite set.
\end{theorem}
\begin{proof}
	Assume $M \supsetneq M_0 := \{0\}$. There exists $\mathfrak{p}_1 \in \text{Ass}(M)$ together with a submodule $M_1 \subset M$ isomorphic to $R/\mathfrak{p}_1$. Furthermore $\text{Ass}(M_1) = \text{Ass}(R/\mathfrak{p}_1) = \{ \mathfrak{p}_1\}$ as easily seen. Hence the Theorem \ref{prop:Ass-properties} entails $\text{Ass}(M) \subset \{\mathfrak{p}_1\} \cup \text{Ass}(M/M_1)$.

	If $M_1 = M$ we are done. Otherwise we start over with $M/M_1$, finding $M_1 \subset M_2 \subset M$ with $M_2/M_1 \simeq R/\mathfrak{p}_2$ where $\mathfrak{p}_2 \in \text{Ass}_R(M/M_1)$, and so forth. This procedure terminates in finite steps since $M$ is Noetherian.
\end{proof}

\section{Primary and coprimary modules}
The classical framework of primary decompositions concerns ideals, but it is advantageous to allow modules here. As before, the ring $R$ is Noetherian.

\begin{definition}\index{coprimary module}\index{primary module}
	An $R$-module $M$ is called \emph{coprimary} if $\text{Ass}(M)$ is a singleton. A submodule $N \subsetneq M$ is called a $\mathfrak{p}$-\emph{primary} submodule if $M/N$ is coprimary with associated prime $\mathfrak{p} \in \Spec(R)$.
\end{definition}

\begin{proposition}\label{prop:coprimary-locnil}
	The following are equivalent for a nonzero $R$-module $M$.
	\begin{enumerate}[(i)]
		\item $M$ is coprimary;
		\item for every zero divisor $r \in R$ for $M$ and every $x \in M$, there exists $n \geq 1$ such that $r^n x = 0$.
	\end{enumerate}
\end{proposition}
The condition (ii) is usually called the local-nilpotency of $r$ on $M$. When $M = R/I$ for some ideal $I$, it translates into: all zero divisors of the ring $R/I$ are nilpotent.
\begin{proof}
	(i) $\implies$ (ii): Suppose $\text{Ass}(M) = \{ \mathfrak{p}\}$ and $x \in M \smallsetminus \{0\}$. From $\emptyset \neq \text{Ass}(Rx) \subset \text{Ass}(M)$ we infer that $\text{Ass}(Rx) = \{\mathfrak{p}\}$, thus by Theorem \ref{prop:Supp-Ass} we see $V(\mathfrak{p}) = \Supp(Rx) = V(\text{ann}(Rx))$. Hence $\mathfrak{p} = \sqrt{\text{ann}(Rx)}$. This implies (ii) by the definition of $\sqrt{\hspace{5pt}}$.

	(ii) $\implies$ (i): It is routine to check that
	\[ \mathfrak{p} := \left\{ r \in R: \forall x \in M \; \exists n \geq 1, \; r^n x = 0 \right\} \]
	is an ideal of $R$. For every $\mathfrak{q} \in \text{Ass}(M)$ there exists $x \in M$ with $\text{ann}_R(x) = \mathfrak{q}$. Every $r \in \mathfrak{p}$ has some power falling in $\mathfrak{q}$, thus $\mathfrak{p} \subset \mathfrak{q}$. Conversely, (ii) and Theorem \ref{prop:Ass-properties} imply $\mathfrak{q} = \text{ann}_R(x) \subset \mathfrak{p}$. From $\mathfrak{q}=\mathfrak{p}$ we conclude $M$ is coprimary with the unique associated prime $\mathfrak{p}$.
\end{proof}

\begin{exercise}[Classical definition of primary ideals]\label{ex:primary-ideal}
	Let $I$ be a proper ideal of $R$. Show that $R/I$ is coprimary if and only if the following holds:
	\[ \forall a,b \in R, \; (ab \in I) \wedge (a \notin I) \implies \exists n \geq 1, \; b^n \in I. \]
	In this case we also say $I$ is a \emph{primary ideal} of $R$. Show that $\{\sqrt{I}\} = \text{Ass}(R/I)$ if $I$ is a primary ideal. Hint: apply Proposition \ref{prop:coprimary-locnil}.
\end{exercise}

\begin{exercise}\label{ex:maximal-primary}
	Let $\mathfrak{m}$ be a maximal ideal of $R$. Show that every ideal $I \subsetneq R$ containing some power of $\mathfrak{m}$ is primary, and $\text{Ass}(R/I) = \{\mathfrak{m}\}$. Hint: show that $\mathfrak{m}$ is the only prime ideal containing $I = \text{ann}_R(R/I)$.
\end{exercise}

\begin{lemma}\label{prop:intersection-primary}
	Let $\mathfrak{p} \in \Spec(R)$ and $N_1, N_2 \subset M$ are $\mathfrak{p}$-primary submodules. Then $N_1 \cap N_2$ is a $\mathfrak{p}$-primary submodule of $M$.
\end{lemma}
\begin{proof}
	We have $M/N_1 \cap N_2 \hookrightarrow M/N_1 \oplus M/N_2$. Since $N_1 \cap N_2 \neq M$, we have
	\[ \emptyset \neq \text{Ass}(M/N_1 \cap N_2) \subset \text{Ass}(M/N_1) \cup \text{Ass}(M/N_2) = \{\mathfrak{p}\} \]
	by Theorem \ref{prop:Ass-properties}.
\end{proof}

\section{Primary decomposition: the main theorem}
We still assume $R$ Noetherian and fix a finitely generated $R$-module $M$.

\begin{theorem}[Lasker--Noether]\label{prop:primary-decomp}\index{primary decomposition}
	Let $N \subsetneq M$ be an $R$-submodule. Then we can express $N$ as
	\[ N = M_1 \cap \cdots \cap M_n, \]
	for some $n \geq 1$ and primary $R$-submodules $M_i$, say with $\mathrm{Ass}(M/M_i) = \{\mathfrak{p}_i\}$ for $i = 1, \ldots, n$. Such a decomposition is called a \emph{primary decomposition} of $N$. We say it is \emph{irredundant} if none of the $M_i$ can be dropped, and \emph{minimal} if there is no such decomposition with fewer terms.
	\begin{enumerate}[(i)]
		\item We have $\mathrm{Ass}(M/N) \subset \{ \mathfrak{p}_1, \ldots, \mathfrak{p}_n \}$, equality holds when the decomposition is irredundant.
		\item If the decomposition is minimal, then for every $\mathfrak{p} \in \mathrm{Ass}(M/N)$ there exists a unique $1 \leq i \leq n$ with $\mathfrak{p} = \mathfrak{p}_i$; consequently $n = |\mathrm{Ass}(M/N)|$.
		\item Consider a primary decomposition of $N$. Let $S \subset R$ be any multiplicative subset, and assume without loss of generality that $\mathfrak{p}_1, \ldots, \mathfrak{p}_m$ are the primes among $\{\mathfrak{p}_1, \ldots, \mathfrak{p}_n \}$ which are disjoint from $S$, then $m \geq 1 \iff N[S^{-1}] \subsetneq M[S^{-1}]$ and
		\[ N[S^{-1}] = M_1[S^{-1}] \cap \cdots \cap M_m[S^{-1}] \]
		is a primary decomposition of the $R[S^{-1}]$-submodule $N[S^{-1}] \subsetneq M[S^{-1}]$; this decomposition of $N[S^{-1}]$ is minimal if the one for $N$ is.
	\end{enumerate}
\end{theorem}
Note that the ``irredundant'' condition in \cite[(8.D)]{Mat80} corresponds to minimality here. The rings whose ideals all have primary decompositions are called \emph{Laskerian rings}, thus part (i) of the Theorem says Noetherian implies Laskerian, but there exist non-Noetherian examples.

\begin{proof}
	Establish the existence of primary decompositions first. Replacing $M$ by $M/N$, we may assume $N = \{0\}$ from the outset. We claim that
	\begin{gather}\label{eqn:Lasker-Noether-aux}
		\forall \mathfrak{p} \in \text{Ass}(M), \; \exists Q(\mathfrak{p}) \subset M, \;
		\left\{ \begin{array}{l}
			Q(\mathfrak{p}) \text{ is } \mathfrak{p}\text{-primary}, \\
			\text{Ass}(Q(\mathfrak{p})) = \text{Ass}(M) \smallsetminus \{ \mathfrak{p} \}.
		\end{array}\right.
	\end{gather}
	Granting this, $Q := \bigcap_{\mathfrak{p} \in \text{Ass}(M)} Q(\mathfrak{p})$ yields the required decomposition since $\text{Ass}(M)$ is finite and $\text{Ass}(Q) = \emptyset$.

	Establish \eqref{eqn:Lasker-Noether-aux} as follows. Put $\Psi := \{ \mathfrak{p} \}$. By Zorn's Lemma\index{Zorn's Lemma} we get a maximal element $Q(\mathfrak{q})$ from the set
	\[ \emptyset \neq \left\{  Q \subset M: \text{submodule}, \; \text{Ass}(Q) \subset \text{Ass}(M) \smallsetminus \Psi \right\} \]
	which is partially ordered by inclusion (details omitted, and you may also use the Noetherian property of $M$). Since
	\[ \text{Ass}(M) \subset \text{Ass}(M/Q(\mathfrak{p})) \cup \text{Ass}(Q(\mathfrak{p})), \]
	it suffices to show $\text{Ass}(M/Q(\mathfrak{p})) \subset \Psi$.  Let $\mathfrak{q} \in \text{Ass}(M/Q(\mathfrak{p}))$ so that there exists $M \supset Q' \supset Q(\mathfrak{p})$ with $Q'/Q(\mathfrak{p}) \simeq R/\mathfrak{q}$. Since $\text{Ass}(Q') \subset \text{Ass}(Q(\mathfrak{p})) \cup \{ \mathfrak{q} \}$, maximality forces $\mathfrak{q} \in \Psi$ (otherwise $\text{Ass}(Q') \subset \text{Ass}(M) \smallsetminus \Psi$), whence \eqref{eqn:Lasker-Noether-aux}. Now we turn to the properties (i) --- (iii).

	\begin{asparaenum}[(i)]
		\item The obvious embedding
		\[ M/N \hookrightarrow \bigoplus_{i=1}^n M/M_i \]
		together with Theorem \ref{prop:Ass-properties} yield $\text{Ass}(M/N) \subset \bigcup_{i=1}^n \text{Ass}(M/M_i) = \{ \mathfrak{p}_1, \ldots, \mathfrak{p}_n \}$.

		Now assume the given primary decomposition is irredundant, we have
			\begin{align*}
				\{0\} & \neq \frac{M_2 \cap \cdots \cap M_n}{N} = \frac{M_2 \cap \cdots \cap M_n}{M_1 \cap (M_2 \cap \cdots \cap M_n)} \\
				& \simeq \frac{M_1 + M_2 \cap \cdots \cap M_n}{M_1} \hookrightarrow M/M_1.
			\end{align*}
			Thus $\text{Ass}(M/N)$ contains $\text{Ass}((M_2 \cap \cdots \cap M_n)/N) = \{\mathfrak{p}_1\}$. Same for $\mathfrak{p}_2, \ldots, \mathfrak{p}_n$.
		\item Suppose $N = M_1 \cap \cdots \cap M_n$ is an irredundant primary decomposition, so that $\text{Ass}(M/N) = \{ \mathfrak{p}_1, \ldots, \mathfrak{p}_n \}$. If $\mathfrak{p}_i = \mathfrak{p}_j$ for some $1 \leq i \neq j \leq n$, Lemma \ref{prop:intersection-primary} will imply that $M_i \cap M_j$ is primary, leading to a shorter primary decomposition. This is impossible when the primary decomposition is minimal.
		\item Suppose $S \cap \mathfrak{p}_i = \emptyset$ (equivalently, $i \leq m$). By Theorem \ref{prop:Ass-properties} and the exactness of localization, $M_i[S^{-1}] \subset M[S^{-1}]$ will be $\mathfrak{p}_i[S^{-1}]$-primary. On the other hand $S \cap \mathfrak{p}_i \neq \emptyset$ implies $\text{Ass}((M/M_i)[S^{-1}]) = \emptyset$ by Theorem \ref{prop:Ass-properties}, thus $M[S^{-1}]/M_i[S^{-1}] = 0$. Since localization respects intersections, we obtain
		\[ N[S^{-1}] = \bigcap_{i=1}^m M_i[S^{-1}]. \]
		In particular $N[S^{-1}]$ is proper if and only if $m \geq 1$.

		It remains to show $\text{Ass}(M[S^{-1}]/N[S^{-1}])$ has $m$ elements if the original primary decomposition is minimal. Indeed, that set is just $\{ \mathfrak{p}[S^{-1}] : \mathfrak{p} \in \text{Ass}(M/N), \; \mathfrak{p} \cap S = \emptyset \}$ by Theorem \ref{prop:Ass-properties}, which equals $\{ \mathfrak{p}_1[S^{-1}], \ldots, \mathfrak{p}_m[S^{-1}] \}$ (distinct) by (ii).
	\end{asparaenum}
\end{proof}

A natural question arises: to what extent are minimal primary decompositions unique? For those $M_i$ whose associated primes are minimal in $\text{Ass}(M/N)$, the answer turns out to be positive.

\begin{corollary}\label{prop:p-primary-component}
	Let $N = M_1 \cap \cdots \cap M_n$ be a minimal primary decomposition of $N \subsetneq M$. Suppose that $M_1$ is $\mathfrak{p}$-primary where $\mathfrak{p} := \mathfrak{p}_1$ is a minimal element in $\mathrm{Ass}(M/N)$, then $M_1$ equals the preimage of $N_{\mathfrak{p}}$ under $M \to M_{\mathfrak{p}}$. Call it the $\mathfrak{p}$-\emph{primary component} of $N$.
\end{corollary}
\begin{proof}
	Recall the proof of Theorem \ref{prop:primary-decomp}, especially the part (iii); here we localize with respect to $S := R \smallsetminus \mathfrak{p}$. The minimality assumption entails
	\[ N_{\mathfrak{p}} = M_{1, \mathfrak{p}} \subset M_{\mathfrak{p}}. \]
	It remains to show that the preimage of $M_{1, \mathfrak{p}}$ under $M \to M_{\mathfrak{p}}$ equals $M_1$, in other words the injectivity of the natural map $M/M_1 \to (M/M_1)_{\mathfrak{p}} = M_{\mathfrak{p}}/M_{1, \mathfrak{p}}$. Indeed, $\bar{x} \in M/M_1$ maps to $0$ if and only if there exists $s \notin \mathfrak{p}$ with $s\bar{x}=0$, but Theorem \ref{prop:Ass-properties} implies that the zero divisors of $M/M_1$ must lie in $\mathfrak{p}$.
\end{proof}

It follows that the non-uniqueness of minimal primary decompositions can only arise from embedded primes in $\text{Ass}(M/N)$.

\section{Examples and remarks}
Primary decompositions are most often applied in the case $M = R$ and $N = I$ is a proper ideal. The goal is to express $I$ as an intersection of primary ideals. To begin with, let us take $R = \Z$. Observations:
\begin{itemize}
	\item The primary ideals of $R$ take the form $(p)^n$, where $n \geq 1$ and $p$ is a prime number or zero. This may be deduced from Exercise \ref{ex:primary-ideal} or directly from definitions.
	\item The irredundant primary decompositions of $\Z/n\Z$, for $n > 1$, corresponds to the factorization of $n$ into prime-powers. There are no embedded primes in this case; the irredundant primary decomposition is unique and automatically minimal.
\end{itemize}

\begin{exercise}
	Justify the foregoing assertions.
\end{exercise}

\begin{figure}[h]
	\centering \includegraphics[height=170pt]{ELasker.jpg} \\ \vspace{1em}
	\begin{minipage}{0.7\textwidth}
		\small Emanuel Lasker (1868--1941) first obtained the primary decomposition for finitely generated $\Bbbk$-algebras and the algebras of convergent power series in 1905. His method involves techniques from \emph{elimination theory}. His result is then generalized and rewritten by Emmy Noether in 1921, in which the ascending chain condition plays a pivotal role. Lasker is best known for being the World Chess Champion from 1894 to 1921. (Picture taken from \href{https://commons.wikimedia.org/w/index.php?curid=5676713}{Wikimedia Commons})
	\end{minipage}
\end{figure}

Therefore one can regard primary decompositions as a generalization of factorization of integers, now performed on the level of ideals. The most important case is $R = \Bbbk[X_1, \ldots, X_n]$ (fix some field $\Bbbk$), as it is naturally connected to classical problems in algebraic geometry. Let us consider a simple yet non-trivial example from \cite[\S 3]{Eis95}.

\begin{example}
	Take $R=\Bbbk[X,Y]$ and $I = (X^2, XY)$. The reader is invited to check that
	\[ I = (X) \cap (X^2, XY, Y^2) = (X) \cap (X^2, Y). \]
	Claim: this gives two minimal primary decompositions of $I$. The ideal $(X)$ is prime, hence primary. In fact, $(X^2, XY, Y^2) = (X,Y)^2$ and $(X^2, Y)$ are both primary ideals associated to the maximal ideal $(X, Y)$. This follows either by direct arguments or by Exercise \ref{ex:maximal-primary}, noting that $(X,Y)^2 = (X^2, XY, Y^2)$ is contained in $(X^2, Y)$. The embedded prime $(X,Y)$ is seen to be responsible non-uniqueness of primary decompositions.

	To see the geometry behind, recall that $V(\mathfrak{a}) \cup V(\mathfrak{b}) = V(\mathfrak{a}\mathfrak{b}) = V(\mathfrak{a} \cap \mathfrak{b})$ for any ideals $\mathfrak{a}, \mathfrak{b}$, thus expressing $I$ as an intersection means breaking the corresponding geometric object into a union of simpler pieces. Also recall that for an ideal $I \subset \Bbbk[X,Y]$, the points in $\bigcap_{f \in I} \{f=0\}$ are in bijection with the maximal ideals lying over $I$, at least for $\Bbbk$ algebraically closed (Nullstellensatz). Thus we may interpret these primary decompositions as equalities among ``geometric objects'' embedded in $\Bbbk^2$:
	\[ \left\{ X^2=0, XY=0 \right\} = \begin{cases} \left\{ X = 0 \right\} \cup \left\{X^2=XY=Y^2=0 \right\} \\ \left\{ X = 0 \right\} \cup \left\{ X^2=Y=0 \right\}. \end{cases} \]
	\begin{enumerate}
		\item The geometric object defined by $X=0$ inside $\Bbbk^2$ is certainly the $Y$-axis: the regular functions living on this space form the $\Bbbk$-algebra $\Bbbk[X,Y]/(X) = \Bbbk[Y]$.
		\item The geometric object defined by $X^2=XY=Y^2=0$ looks ``physically'' like the origin $(0,0)$, but the $\Bbbk$-algebra of ``regular functions'' (in an extended sense) living on it equals $\Bbbk[X,Y]/(X^2,XY,Y^2)$: by restricting a polynomial function $f(X,Y)$ to this ``thickened point'', we see not only $f(0,0)$ but also $\frac{\partial f}{\partial x}(0,0)$ and $\frac{\partial f}{\partial y}(0,0)$. In other words, we shall view $X^2=XY=Y^2=0$ as the first-order infinitesimal neighborhood of $(0,0) \in \Bbbk^2$.
		\item In a similar vein, $X^2=Y=0$ physically defines $(0,0)$, but by restricting $f$ to that thickened point, we retrieve $f(0,0)$ as well as $\frac{\partial f}{\partial x}(0,0)$. Therefore we obtain the first-order infinitesimal neighborhood of $0$ inside the $X$-axis.
	\end{enumerate}

	Both decomposition says that we obtain the $Y$-axis together with first-order infinitesimal information at the origin $(0,0)$. This is also a nice illustration of the use of nilpotent elements in scheme theory.
\end{example}

\begin{example}[Symbolic powers]
	Let $\mathfrak{p}$ be a prime ideal in a Noetherian ring $R$ and fix $n \geq 1$. Observe that $\mathfrak{p}$ is the unique minimal element in $\Supp(R/\mathfrak{p}^n) = V(\mathfrak{p}^n)$. By Theorem \ref{prop:Supp-Ass} $\mathfrak{p} \in \text{Ass}(R/\mathfrak{p}^n)$, so it makes sense to denote by $\mathfrak{p}^{(n)}$ the $\mathfrak{p}$-primary component (Corollary \ref{prop:p-primary-component}) of $\mathfrak{p}^n$, called the $n$-th \emph{symbolic power} of $\mathfrak{p}$. In general $\mathfrak{p}^{(n)} \supsetneq \mathfrak{p}^n$. For a nice geometric interpretation of symbolic powers due to Nagata and Zariski, we refer to \cite[\S 3.9]{Eis95}.
\end{example}

Getting primary decomposition of ideals in polynomial algebras is a non-trivial task. Thanks to the pioneers in computational commutative algebra, this can now achieved on your own computer, eg. by the open-source \href{http://www.sagemath.org}{SageMath} system.\index{SageMath}


\section{Introduction}
\label{sec:GEVD_overview}

In this chapter, we search for a linear combination of channels that yields
an output signal $y_t$ useful for sharp wave-ripple (SWR) detection. More
precisely, we search for a vector $\w \in \reals^C$ in channel (or electrode)
space to project the samples $\z_t \in \reals^C$ on, so that the signal
%
\begin{equation}
\label{eq:linear}
y_t = \w^T \z_t
\end{equation}
%
has high variance (or power) during SWR events, and low variance outside
them. This principle is illustrated with a two-dimensional toy dataset in
\cref{fig:GEVD_principle}. (We can then detect SWR events using threshold
crossings of the envelope of $y_t$, as discussed in \cref{ch:BPF}).

\begin{figure}
\includegraphics[width=0.6\textwidth]{GEVD_principle_scatter}
\includegraphics[width=0.6\textwidth]{GEVD_principle_strips}
\captionn{Signal-to-noise maximisation via the GEVD}{Toy example to
illustrate the generalised eigenvalue decomposition (GEVD) principle for
signal detection. \emph{Left}: multi-channel time-series data plotted in
`phase space' (meaning without time axis), with blue dots representing
samples where the signal was present, and orange dots representing samples
where it was not. Actually toy data drawn from two 2-dimensional Gaussian
distributions with different covariance matrices. Red vector: first
eigenvector of the signal covariance matrix (also known as the first
principal component). Green vector: first generalised eigenvector of the
signal and noise covariance matrices. \emph{Right}: Projection of both data
sets on both the ordinary eigenvector (``PCA'') and the generalised
eigenvector (``GEVD''). The ratio of the projected signal data variance
versus the projected noise data variance is maximised for the GEVD case.}
\label{fig:GEVD_principle}
\end{figure}

We assume the input signal $\z_t$ to be zero mean. This is achieved with a
straightforward preprocessing step in offline SWR detection, and can be
approximated in online SWR detection by keeping track of a running mean of
$\z_t$, such as an exponentially weighted moving average.

This is a supervised (or data-driven) method: we need to have access to
training data $\z\train_t$, and an associated labelling $x\train_t$ which
marks when there is an SWR event present in $\z\train_t$, and when there is
not. These can then be used to find a good weight vector $\w$, that can then
be applied to detect SWR events in unlabelled data $\z\test_t$.

The algorithm as described above is also a purely spatial filtering method
(at each timestep $t$, only current information from the different channels
is used in calculating the output $y_t$, without incorporating temporal
information from previous timesteps $t_p < t$). The algorithm can be adapted
to also incorporate temporal information, by defining a vector
$\z^\text{stack}_t \in
\reals^{C \cdot P}$, which consists of stacked sample vectors (each
consisting of $C$ channels) from $P$ different timesteps $t_p \leq t$. The
linear weights $\w^\text{stack} \in \reals^{C \cdot P}$ are then calculated
in the same way as for $\w$.

The following sections describes how the vector $\w$ can be found.


{\Large Lecturer: Prof. Dr. Bethge}\\[1cm]

\textbf{Examples of unsupervised learning applications}
\begin{itemize}
	\item \emph{density estimation} (e.g. texture synthesis), image compression
	\item level set estimation
	\item clustering/mode finding e.g. spike sorting; algorithm Mixture of Gaussians
	\item metric learning e.g. 3D visualization; algorithm multidimensional scaling
	\item feature extraction -> representation learning, e.g. whitening; algorithm principle component analysis
\end{itemize}

\section{Set algebras, measures, and probabilities}
\begin{itemize}
\item {\bf Overview for the mathematical construction of random variables}
\begin{itemize}
\item Sample Space $S$ (often also $\Omega $). $s\in S$ ``outcomes''
\item Boolean Algebra (or Sigma Algebra)  $\mathcal{B}(S)$ and $A \in \mathcal{B}(S)$ are called ``events''. An event $A$ is realized if an outcome $s$ is in $A$. \\For example, $\mathcal{B}(S)=\mathcal{P}(S)$
\item Measure $m: \mathcal{B}(S) \to [0,\infty)$ and $m(A\dot\cup B) = m(A) + m(B)$
\item probability measure $p(S)=1$, if $m$ is a measure with $m(S)<\infty$ then $p(A) := m(A)/m(S)$ is a probability measure.
\item random variables: consider $M \in \mathcal{B}(S)$ with $M=\{s \in S: X(s) < \vartheta\} \subseteq S$
\end{itemize}

\item {\bf Power Set:}  $\mathcal{P}(S) := \{A\subseteq S\}$, $|\mathcal{P}(S) |= 2^{|S|}$

\item {\bf Boolean Algebra:}  $\mathcal{B}(S) \subseteq \mathcal{P}(S)$ is called a {\it Boolean Algebra} iff
\begin{itemize}
\item[(i)] $A \in \mathcal{B}(S)  \Rightarrow \overline{A} \in \mathcal{B}(S)  $
\item[(ii)] $A, B \in \mathcal{B}(S)  \Rightarrow A \cup B \in \mathcal{B}(S)  $
\end{itemize}
Examples:
\begin{itemize}
\item[(i)] $\{ \emptyset, S\}  $ and $\mathcal{P}(S)$ are Boolean Algebras.
\item[(ii)] If $a\in S$ then $\{\emptyset, \{a\}, S-\{a\}, S\}$ is the Boolean Algebra {\it generated by} $a$.
\item[(iii)] $I(R)$ is defined to be the smallest Boolean Algebra which contains all open intervals $(a,b)$ for which $-\infty \le a < b\le \infty$.
\end{itemize}

\item {\bf Sigma-Algebra:}  $\mathcal{B}(S) \subseteq \mathcal{P}(S)$ is called a Sigma-Algebra if
\begin{itemize}
\item[(i)] $A \in \mathcal{B}(S)  \Rightarrow \overline{A} \in \mathcal{B}(S)  $
\item[(ii)] For all sequences $A_1, A_2, \dots \in \mathcal{B}(S)  \Rightarrow \bigcup_{k=1}^\infty A_k \in \mathcal{B}(S)  $
\end{itemize}

\item {\bf Venn diagram}

\end{itemize}

%\item {\bf Proposition 1 (Boolean laws):}
\begin{proposition}[Boolean laws]
\begin{itemize}
\item[(B1)] Idempotency: $A \cup A = A$;   \hspace{.5cm} $A \cap A = A$
\item[(B2)] Associativity: $A \cap (B\cap C) = (A\cap B) \cap C$
\item[(B3)] Commutativity: $A \cup B = B \cup A$;    \hspace{.5cm}  $A \cap B = B \cap A$
\item[(B4)] Distributivity: $A \cap (B\cup C)  = (A \cap B) \cup (A \cap C)$; \hspace{.5cm}  $A \cup (B\cap C)  = (A \cup B) \cap (A \cup C)$
\item[(B5)] de Morgan's law:  $\overline{A \cup B} = \overline{A} \cap \overline{B} $;    \hspace{.5cm}  $\overline{A \cap B} = \overline{A} \cup \overline{B} $
\item[(B6)] Complements: $ \overline{ \overline{A}} =  A$; \hspace{.5cm}  $A \cap \overline{A} = \emptyset$; \hspace{.5cm}  $A \cup \overline{A} = S$
\item[(B7)] Properties of $S$ and $ \emptyset$: \hspace{.2cm} $A \cup S = S$; \hspace{.5cm} $A \cup \emptyset = A$; \hspace{.5cm} $A \cap S = A$; \hspace{.5cm} $A \cap \emptyset = \emptyset$

\end{itemize}
\end{proposition}

\begin{itemize}

\item {\bf (Finite) Measure:} A mapping $m: \mathcal{B}(S) \rightarrow [0, \infty), A\in  \mathcal{B}(S) \to m(A) \in [0, \infty)$ is called a {\it measure} if for all $A,B \in \mathcal{B}(S)$ with $A\cap B = \emptyset$ holds: $m(A\cup B) = m(A) + m(B)$.\\(This property can be written compactly as $m(A\dot\cup B) = m(A) + m(B)$)

Examples:
\begin{itemize}
\item[(i)] $m(A) := |A|$ is the {\it counting measure}.
\item[(ii)] For $S=[a,b]$ and $\mathcal{B}(S)=\mathcal{I}([a,b])$ with $|a|, |b| < \infty$ the {\it Lebesgue Measure} is defined as $m([c, d]) := d-c$.
\item[(iii)] If $f(x) \ge 0$ for all $a \le x \le b$ with $\int_a^b f(x) dx <\infty$ then $m([c, d]) := \int_c^d f(x) dx $ is a measure.
\item[(iv)] For $S=R$ and $\mathcal{B}(S)=\mathcal{I}(R)$ the {\it Dirac Ma\ss} is given by $\delta_a(J) = 1$ if $a\in J$ and $\delta_a(J) = 0$ otherwise.
\end{itemize}

\item {\bf Proposition 2:}
\begin{itemize}
\item[(i)] If $B\subseteq A$ then $m(A-B) = m(A) - m(B)$.
\item[(ii)] If $B\subseteq A$ then $m(B) \le m(A)$.
\item[(iii)] $m(\emptyset) =0$.
\item[(iv)] $m(A \cup B) = m(A) + m(B) - m(A\cap B)$.
\end{itemize}

\item {\bf Proposition 3:}\\
If $A,B \in \mathcal{B}(S)$ then:
\begin{itemize}
\item[(i)] $ (A \cap B) \in \mathcal{B}(S)$ since $ (A \cap B) = \overline{\overline{A} \cup \overline{B}}$.
\item[(ii)] $\emptyset, S$ belong to every Boolean Algebra, since for $A \in \mathcal{B}(S)$ also $\overline{A} \in \mathcal{B}(S)$ and $A \cap \overline{A} = \emptyset$ and $A\cup \overline{A} = S$ .
\item[(iii)] $\mathcal{B}_B(S) := B \cap \mathcal{B}(S) = \{A \cap B: A \in \mathcal{B}(S)\}$ is a Boolean Algebra if $B\ne \emptyset$.
\item[(iv)] $\mathcal{B}_B(S) \subseteq \mathcal{B}(S)$.
\end{itemize}

\item {\bf Probability measure:} For a sample space $S$ and a Sigma Algebra $\mathcal{B}(S)$ a measure \, $P$ is a {\it probability measure} if $P(S)=1$.

\item {\bf Probability space:} A probability space consists of the triple $(S,\mathcal{B}(S), P)$ where $S$ is the sample space, $\mathcal{B}(S)$ the event algebra (which is a Boolean or a sigma algebra) and $P$ is a  probability measure on $\mathcal{B}(S)$.

\item {\bf Proposition 4:}
\begin{itemize}
\item[(i)] $0 \le P(A) \le 1$.
\item[(ii)] $P(\overline{A}) = 1-P(A)$.
\item[(iii)] If $\mathcal{E}= \{E_1, E_2, \dots, E_n\}$ is a partition of $S$ (that is $S= \bigcup_{k=1}^n E_k$ and $E_l \cap E_m = \emptyset$), then \quad $\sum_{k=1}^n P(E_k) = 1$ \quad and \quad $P(A)=\sum_{k=1}^n P(A\cap E_k)$.
\end{itemize}

\item {\bf Specification of probabilities}
\begin{itemize}
\item[(i)] As a limiting value of relative frequencies: $P(A) = \lim_{n\to \infty} \frac{r_n(A)}{n}$
\item[(ii)] Symmetrie assumptions (e.g. if we assume $P(s)=P(s^\prime)$ for all $s,s^\prime \in S$ then it follows: $P(s) = 1/|S|$).
\item[(iii)] Subjective probabilities (based on Cox axioms) can be determined via betting games.
\end{itemize}
\item {\bf Conditional probabilities:}
If $(S,\mathcal{B}(S), P)$ is a probability space and $B \in \mathcal{B}(S)$ then the restriction of $P$ to the reduced Sigma-Algebra $\mathcal{B}_B(S):=\{B\cap A: A \in \mathcal{B}(S)\}$ defines a measure \ $M_B: \mathcal{B}_B \to [0,\infty)$. If in addition $P(B) >0$, then $P_B: \mathcal{B}_B(S) \to [0,1], \quad A_B \mapsto P_B(A_B) :=  M_B(A_B)/P(B)=P(A_B)/P(B), \, \forall A_B \in \mathcal{B}_B(S)$ is a probability measure on $\mathcal{B}_B(S)$.\\
The $A_B\in \mathcal{B}_B(S)$ usually originate from intersection of the elements of the original Sigma-Algebra $\mathcal{B}(S)$ with the event $B$.
\item {\bf Independence:}\\
Two events $A$ and $B$ are {\it statistically independent} iff $P(A\cap B) = P(A) P(B)$.
\item {\bf Proposition 5:}
\begin{itemize}
\item[(i)] Two events $A$ and $B$ are (statistically) independent if $P(A)=0$ or $P(B)=0$.
\item[(ii)] If two events $A$ and $B$ are independent and $P(A)>0$, then: $P(B|A)=P_A(B)=P(B)$.
\item[(iii)] If two events $A$ and $B$ are independent and $P(A)>0$, then $A$ and $\overline{B}$ are independent as well.
\end{itemize}

\end{itemize}

\section{Random Variables}
\begin{itemize}
\item {\bf Cumulative distribution function (cdf):}
Any univariate random variable can be uniquely defined by the cumulative distribution function:
$$
F(x) = P(\{s \in S: X(s) \le x\}), \quad \forall x \in S^\prime
$$
It holds:
\begin{itemize}
\item[(i)] $F$ is a nondecreasing function.
\item[(ii)] $\lim_{x\to -\infty} F(x)= 0$, $\lim_{x\to \infty} F(x)= 1$
\item[(iii)] Any function that fulfills (i)+(ii) is a cdf.
\item[(iv)] $ P(\{s \in S: X(s) > x\}) = 1-F(x)$
\item[(v)] $ P(\{s \in S: x_1 < X(s) \le x_2\}) = F(x_2)-F(x_1)$
\item[(vi)] discrete case: probability mass function / point probability $p(x) = F(x) - \lim_{\epsilon \to 0}F(x-\epsilon)$
\item[(vii)] continuous case: probability density function (pdf) $\rho(x) = \frac{d}{dx} \mathcal{F}(x)$
\end{itemize}

\item {\bf Expectation (value):}
$$
E[X] = \sum_{k=1}^n p(x_k) x_k , \quad E[f(X)] = \sum_{k=1}^n p(x_k) f(x_k)
$$
\item {\bf Moments:}
$$
E[X^m] =  \sum_{k=1}^n p(x_k) x_k^m
$$
\item {\bf Variance:}
$$
Var[X]= E[X^2]-E[X]^2
$$

\end{itemize}
\vfil