You can do it with adjustbox
, the trick is to scale up the contents so it needs to be scaled down.
\documentclass{article}
\usepackage{amsmath}
\usepackage{adjustbox}
\newlength{\myheight}
\newlength{\mywidth}
\setlength{\myheight}{3cm}
\setlength{\mywidth}{6cm}
% A box for displaying the result
\newsavebox{\FRAME}
\sbox\FRAME{%
\setlength{\fboxsep}{0pt}%
\kern-\fboxrule
\fbox{\rule{\mywidth}{0pt}\rule{0pt}{\myheight}}%
\kern-\mywidth\kern-\fboxrule
}
\begin{document}
\usebox{\FRAME}%
\adjustbox{scale=100,max width=\mywidth,max height=\myheight}{%
\raisebox{\depth}{%
$\displaystyle
\int_{-\infty}^{\infty}e^{-x^{2}}\,dx=\sqrt{\pi}
$%
}%
}
\bigskip
\usebox{\FRAME}%
\adjustbox{scale=100,max width=\mywidth,max height=\myheight}{%
\raisebox{\depth}{%
$\displaystyle
\begin{bmatrix} 1 & 0 \\ 0 & 1 \\ 0 & 0 \\ 0 & 0 \end{bmatrix}
$%
}%
}
\end{document}
I used \usebox{\FRAME}
just for showing the intended area to fill. It's also necessary to raise the box by its depth, or it would stick below the area.

adjustbox
package does this. – egreg Oct 4 '15 at 18:10