\documentclass[10pt,a4paper]{article} % Packages \usepackage{fancyhdr} % For header and footer \usepackage{multicol} % Allows multicols in tables \usepackage{tabularx} % Intelligent column widths \usepackage{tabulary} % Used in header and footer \usepackage{hhline} % Border under tables \usepackage{graphicx} % For images \usepackage{xcolor} % For hex colours %\usepackage[utf8x]{inputenc} % For unicode character support \usepackage[T1]{fontenc} % Without this we get weird character replacements \usepackage{colortbl} % For coloured tables \usepackage{setspace} % For line height \usepackage{lastpage} % Needed for total page number \usepackage{seqsplit} % Splits long words. %\usepackage{opensans} % Can't make this work so far. Shame. Would be lovely. \usepackage[normalem]{ulem} % For underlining links % Most of the following are not required for the majority % of cheat sheets but are needed for some symbol support. \usepackage{amsmath} % Symbols \usepackage{MnSymbol} % Symbols \usepackage{wasysym} % Symbols %\usepackage[english,german,french,spanish,italian]{babel} % Languages % Document Info \author{chewxy} \pdfinfo{ /Title (data-science-in-go-a.pdf) /Creator (Cheatography) /Author (chewxy) /Subject (Data Science In Go: A Cheat Sheet) } % Lengths and widths \addtolength{\textwidth}{6cm} \addtolength{\textheight}{-1cm} \addtolength{\hoffset}{-3cm} \addtolength{\voffset}{-2cm} \setlength{\tabcolsep}{0.2cm} % Space between columns \setlength{\headsep}{-12pt} % Reduce space between header and content \setlength{\headheight}{85pt} % If less, LaTeX automatically increases it \renewcommand{\footrulewidth}{0pt} % Remove footer line \renewcommand{\headrulewidth}{0pt} % Remove header line \renewcommand{\seqinsert}{\ifmmode\allowbreak\else\-\fi} % Hyphens in seqsplit % This two commands together give roughly % the right line height in the tables \renewcommand{\arraystretch}{1.3} \onehalfspacing % Commands \newcommand{\SetRowColor}[1]{\noalign{\gdef\RowColorName{#1}}\rowcolor{\RowColorName}} % Shortcut for row colour \newcommand{\mymulticolumn}[3]{\multicolumn{#1}{>{\columncolor{\RowColorName}}#2}{#3}} % For coloured multi-cols \newcolumntype{x}[1]{>{\raggedright}p{#1}} % New column types for ragged-right paragraph columns \newcommand{\tn}{\tabularnewline} % Required as custom column type in use % Font and Colours \definecolor{HeadBackground}{HTML}{333333} \definecolor{FootBackground}{HTML}{666666} \definecolor{TextColor}{HTML}{333333} \definecolor{DarkBackground}{HTML}{295E7D} \definecolor{LightBackground}{HTML}{F1F4F6} \renewcommand{\familydefault}{\sfdefault} \color{TextColor} % Header and Footer \pagestyle{fancy} \fancyhead{} % Set header to blank \fancyfoot{} % Set footer to blank \fancyhead[L]{ \noindent \begin{multicols}{3} \begin{tabulary}{5.8cm}{C} \SetRowColor{DarkBackground} \vspace{-7pt} {\parbox{\dimexpr\textwidth-2\fboxsep\relax}{\noindent \hspace*{-6pt}\includegraphics[width=5.8cm]{/web/www.cheatography.com/public/images/cheatography_logo.pdf}} } \end{tabulary} \columnbreak \begin{tabulary}{11cm}{L} \vspace{-2pt}\large{\bf{\textcolor{DarkBackground}{\textrm{Data Science In Go: A Cheat Sheet}}}} \\ \normalsize{by \textcolor{DarkBackground}{chewxy} via \textcolor{DarkBackground}{\uline{cheatography.com/42427/cs/12774/}}} \end{tabulary} \end{multicols}} \fancyfoot[L]{ \footnotesize \noindent \begin{multicols}{3} \begin{tabulary}{5.8cm}{LL} \SetRowColor{FootBackground} \mymulticolumn{2}{p{5.377cm}}{\bf\textcolor{white}{Cheatographer}} \\ \vspace{-2pt}chewxy \\ \uline{cheatography.com/chewxy} \\ \end{tabulary} \vfill \columnbreak \begin{tabulary}{5.8cm}{L} \SetRowColor{FootBackground} \mymulticolumn{1}{p{5.377cm}}{\bf\textcolor{white}{Cheat Sheet}} \\ \vspace{-2pt}Published 12th October, 2017.\\ Updated 25th October, 2020.\\ Page {\thepage} of \pageref{LastPage}. \end{tabulary} \vfill \columnbreak \begin{tabulary}{5.8cm}{L} \SetRowColor{FootBackground} \mymulticolumn{1}{p{5.377cm}}{\bf\textcolor{white}{Sponsor}} \\ \SetRowColor{white} \vspace{-5pt} %\includegraphics[width=48px,height=48px]{dave.jpeg} Measure your website readability!\\ www.readability-score.com \end{tabulary} \end{multicols}} \begin{document} \raggedright \raggedcolumns % Set font size to small. Switch to any value % from this page to resize cheat sheet text: % www.emerson.emory.edu/services/latex/latex_169.html \footnotesize % Small font. \begin{multicols*}{2} \begin{tabularx}{8.4cm}{X} \SetRowColor{DarkBackground} \mymulticolumn{1}{x{8.4cm}}{\bf\textcolor{white}{Introduction}} \tn \SetRowColor{white} \mymulticolumn{1}{x{8.4cm}}{Go is the future for doing data science. In this cheatsheet, we look at 2 families of libraries that will allow you to do that. \newline % Row Count 3 (+ 3) They are: `gorgonia.org/tensor` and `gonum.org/v1/gonum/mat` . The gonum libraries will be referred to as `gonum/mat` \newline % Row Count 6 (+ 3) For this cheatsheet, assume the following: \newline % Row Count 7 (+ 1) `import ts "gorgonia.org/tensor"`% Row Count 8 (+ 1) } \tn \hhline{>{\arrayrulecolor{DarkBackground}}-} \SetRowColor{LightBackground} \mymulticolumn{1}{x{8.4cm}}{{\bf{Note on panic and error behaviour}}: \newline 1. Most `tensor` operations return `error`. \newline 2. `gonum` has a \{\{link="http://talks.godoc.org/github.com/gonum/talks/2017/gonumtour.slide\#14"\}\}good policy\{\{/link\}\} of when errors are returned and when panics happen.} \tn \hhline{>{\arrayrulecolor{DarkBackground}}-} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{8.4cm}{X} \SetRowColor{DarkBackground} \mymulticolumn{1}{x{8.4cm}}{\bf\textcolor{white}{What To Use}} \tn % Row 0 \SetRowColor{LightBackground} \mymulticolumn{1}{x{8.4cm}}{I ever only want a `float64` matrix or vector} \tn \mymulticolumn{1}{x{8.4cm}}{\hspace*{6 px}\rule{2px}{6px}\hspace*{6 px}use `gonum/mat`} \tn % Row Count 2 (+ 2) % Row 1 \SetRowColor{white} \mymulticolumn{1}{x{8.4cm}}{I want to focus on doing statistical/scientific work} \tn \mymulticolumn{1}{x{8.4cm}}{\hspace*{6 px}\rule{2px}{6px}\hspace*{6 px}use `gonum/mat`} \tn % Row Count 5 (+ 3) % Row 2 \SetRowColor{LightBackground} \mymulticolumn{1}{x{8.4cm}}{I want to focus on doing machine learning work} \tn \mymulticolumn{1}{x{8.4cm}}{\hspace*{6 px}\rule{2px}{6px}\hspace*{6 px}use `gonum/mat` or `gorgonia.org/tensor`.} \tn % Row Count 7 (+ 2) % Row 3 \SetRowColor{white} \mymulticolumn{1}{x{8.4cm}}{I want to focus on deep learning work} \tn \mymulticolumn{1}{x{8.4cm}}{\hspace*{6 px}\rule{2px}{6px}\hspace*{6 px}use `gorgonia.org/tensor`} \tn % Row Count 9 (+ 2) % Row 4 \SetRowColor{LightBackground} \mymulticolumn{1}{x{8.4cm}}{I want multidimensional arrays} \tn \mymulticolumn{1}{x{8.4cm}}{\hspace*{6 px}\rule{2px}{6px}\hspace*{6 px}use `gorgonia.org/tensor`, or `{[}{]}mat.Matrix`} \tn % Row Count 11 (+ 2) % Row 5 \SetRowColor{white} \mymulticolumn{1}{x{8.4cm}}{I want to work with different data types} \tn \mymulticolumn{1}{x{8.4cm}}{\hspace*{6 px}\rule{2px}{6px}\hspace*{6 px}use `gorgonia.org/tensor`} \tn % Row Count 13 (+ 2) % Row 6 \SetRowColor{LightBackground} \mymulticolumn{1}{x{8.4cm}}{I want to wrangle data like in Pandas or R - with data frames} \tn \mymulticolumn{1}{x{8.4cm}}{\hspace*{6 px}\rule{2px}{6px}\hspace*{6 px}use `kniren/gota`} \tn % Row Count 16 (+ 3) \hhline{>{\arrayrulecolor{DarkBackground}}-} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{8.4cm}{X} \SetRowColor{DarkBackground} \mymulticolumn{1}{x{8.4cm}}{\bf\textcolor{white}{Default Values}} \tn % Row 0 \SetRowColor{LightBackground} \mymulticolumn{1}{x{8.4cm}}{{\bf{Numpy}}\{\{nl\}\}`a = np.Zeros((2,3))`} \tn % Row Count 1 (+ 1) % Row 1 \SetRowColor{white} \mymulticolumn{1}{x{8.4cm}}{{\bf{gonum/mat}}\{\{nl\}\}`a := mat.NewDense(2, 3, nil)`} \tn % Row Count 2 (+ 1) % Row 2 \SetRowColor{LightBackground} \mymulticolumn{1}{x{8.4cm}}{{\bf{tensor}}\{\{nl\}\}`a := ts.New(ts.Of(Float32), ts.WithShape(2,3))` \{\{bb\}\}} \tn % Row Count 4 (+ 2) % Row 3 \SetRowColor{white} \mymulticolumn{1}{x{8.4cm}}{\{\{bt\}\}\{\{ac\}\}{\bf{A Range...}}\{\{bb\}\}} \tn % Row Count 5 (+ 1) % Row 4 \SetRowColor{LightBackground} \mymulticolumn{1}{x{8.4cm}}{{\bf{Numpy}}\{\{nl\}\}`a = np.arange(0, 9).reshape(3,3)`} \tn % Row Count 6 (+ 1) % Row 5 \SetRowColor{white} \mymulticolumn{1}{x{8.4cm}}{{\bf{gonum}}\{\{nl\}\}`a := mat.NewDense(3, 3, floats.Span(make({[}{]}float64, 9), 0, 8)` \{\{noshy\}\}} \tn % Row Count 8 (+ 2) % Row 6 \SetRowColor{LightBackground} \mymulticolumn{1}{x{8.4cm}}{{\bf{tensor}}\{\{nl\}\}`a := \seqsplit{ts.New(ts.WithBacking(ts.Range(ts.Int}, 0, 9), ts.WithShape(3,3))`\{\{bb\}\}} \tn % Row Count 10 (+ 2) % Row 7 \SetRowColor{white} \mymulticolumn{1}{x{8.4cm}}{\{\{bt\}\}\{\{ac\}\}{\bf{Identity Matrices}}\{\{bb\}\}} \tn % Row Count 11 (+ 1) % Row 8 \SetRowColor{LightBackground} \mymulticolumn{1}{x{8.4cm}}{{\bf{Numpy}}\{\{nl\}\}`a = np.eye(3,3)`} \tn % Row Count 12 (+ 1) % Row 9 \SetRowColor{white} \mymulticolumn{1}{x{8.4cm}}{{\bf{gonum/mat}}\{\{nl\}\}`a := mat.NewDiagonal(3, {[}{]}float64\{1, 1, 1\})`} \tn % Row Count 14 (+ 2) % Row 10 \SetRowColor{LightBackground} \mymulticolumn{1}{x{8.4cm}}{{\bf{tensor}}\{\{nl\}\}`a := ts.I(3, 3, 0)`} \tn % Row Count 15 (+ 1) \hhline{>{\arrayrulecolor{DarkBackground}}-} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{8.4cm}{X} \SetRowColor{DarkBackground} \mymulticolumn{1}{x{8.4cm}}{\bf\textcolor{white}{Elementwise Arithmetic Operations}} \tn % Row 0 \SetRowColor{LightBackground} \mymulticolumn{1}{x{8.4cm}}{\{\{bt\}\}\{\{ac\}\}{\bf{Addition}}\{\{bb\}\}} \tn % Row Count 1 (+ 1) % Row 1 \SetRowColor{white} \mymulticolumn{1}{x{8.4cm}}{{\bf{Numpy}}\{\{nl\}\}`c = a + b`\{\{nl\}\}`c = np.add(a, b)`\{\{nl\}\}`a += b ~~~~~~~~~~~~~\# in-place`\{\{nl\}\}`np.add(a, b, out=c) \# reuse array`} \tn % Row Count 5 (+ 4) % Row 2 \SetRowColor{LightBackground} \mymulticolumn{1}{x{8.4cm}}{{\bf{gonum/mat}}\{\{nl\}\}`c.Add(a, b)`\{\{nl\}\}`a.Add(a, b) // in-place`} \tn % Row Count 7 (+ 2) % Row 3 \SetRowColor{white} \mymulticolumn{1}{x{8.4cm}}{{\bf{tensor}}\{\{nl\}\}`var c *ts.Dense; c, err = a.Add(b)`\{\{nl\}\}`var c ts.Tensor; c, err = ts.Add(a, b)`\{\{nl\}\}`a.Add(b, ts.UseUnsafe()) ~~~~~// in-place`\{\{nl\}\}`a.Add(b, ts.WithReuse(c)) ~~~~// reuse tensor`\{\{nl\}\}`ts.Add(a, b, ts.UseUnsafe()) ~// in-place`\{\{nl\}\}`ts.Add(a, b, ts.WithReuse(c)) // reuse`\{\{nl\}\}{\bf{Note:}} The operations all returns a result and an error, omitted for brevity here. It's good habit to check for errors. \{\{bb\}\}} \tn % Row Count 17 (+ 10) % Row 4 \SetRowColor{LightBackground} \mymulticolumn{1}{x{8.4cm}}{\{\{bt\}\}\{\{ac\}\}{\bf{Subtraction}}\{\{bb\}\}} \tn % Row Count 18 (+ 1) % Row 5 \SetRowColor{white} \mymulticolumn{1}{x{8.4cm}}{{\bf{Numpy}}\{\{nl\}\}`c = a - b`\{\{nl\}\}`c = np.subtract(a, b)`} \tn % Row Count 20 (+ 2) % Row 6 \SetRowColor{LightBackground} \mymulticolumn{1}{x{8.4cm}}{{\bf{gonum/mat}}\{\{nl\}\}`c.Sub(a, b)`} \tn % Row Count 21 (+ 1) % Row 7 \SetRowColor{white} \mymulticolumn{1}{x{8.4cm}}{{\bf{tensor}}\{\{nl\}\}`c, err:= a.Sub(b)`\{\{nl\}\}`c, err = ts.Sub(a, b)` \{\{bb\}\}} \tn % Row Count 23 (+ 2) % Row 8 \SetRowColor{LightBackground} \mymulticolumn{1}{x{8.4cm}}{\{\{bt\}\}\{\{ac\}\}{\bf{Multiplication}}\{\{bb\}\}} \tn % Row Count 24 (+ 1) % Row 9 \SetRowColor{white} \mymulticolumn{1}{x{8.4cm}}{{\bf{Numpy}}\{\{nl\}\}`c = a * b`\{\{nl\}\}`c = np.multiply(a, b)`} \tn % Row Count 26 (+ 2) % Row 10 \SetRowColor{LightBackground} \mymulticolumn{1}{x{8.4cm}}{{\bf{gonum/mat}}\{\{nl\}\}`c.MulElem(a, b)`} \tn % Row Count 27 (+ 1) % Row 11 \SetRowColor{white} \mymulticolumn{1}{x{8.4cm}}{{\bf{tensor}}\{\{nl\}\}`c, err := a.Mul(b)`\{\{nl\}\}`c, err := ts.Mul(a, b)` \{\{bb\}\}} \tn % Row Count 29 (+ 2) % Row 12 \SetRowColor{LightBackground} \mymulticolumn{1}{x{8.4cm}}{\{\{bt\}\}\{\{ac\}\}{\bf{Division}}\{\{bb\}\}} \tn % Row Count 30 (+ 1) \end{tabularx} \par\addvspace{1.3em} \vfill \columnbreak \begin{tabularx}{8.4cm}{X} \SetRowColor{DarkBackground} \mymulticolumn{1}{x{8.4cm}}{\bf\textcolor{white}{Elementwise Arithmetic Operations (cont)}} \tn % Row 13 \SetRowColor{LightBackground} \mymulticolumn{1}{x{8.4cm}}{{\bf{Numpy}}\{\{nl\}\}`c = a / b`\{\{nl\}\}`c = np.divide(a, b)`} \tn % Row Count 2 (+ 2) % Row 14 \SetRowColor{white} \mymulticolumn{1}{x{8.4cm}}{{\bf{gonum/mat}}\{\{nl\}\}`c.DivElem(a, b)`} \tn % Row Count 3 (+ 1) % Row 15 \SetRowColor{LightBackground} \mymulticolumn{1}{x{8.4cm}}{{\bf{tensor}}\{\{nl\}\}`c, err := a.Div(b)`\{\{nl\}\}`c, err := ts.Div(a, b)` \{\{nl\}\}{\bf{Note:}} When encountering division by 0 for non-floats, an error will be returned, and the value at which the offending value will be 0 in the result. \{\{bb\}\}} \tn % Row Count 8 (+ 5) \hhline{>{\arrayrulecolor{DarkBackground}}-} \SetRowColor{LightBackground} \mymulticolumn{1}{x{8.4cm}}{{\bf{Note:}} All variations of arithmetic operations follow the patterns available in {\emph{Addition}} for all examples. \newline \newline {\bf{Note on Shapes}} \newline In all of these functions, `a` and `b` has to be of the same shape. In Numpy operations with dissimilar shapes will throw an exception. With `gonum/mat` it'd panic. With `tensor`, it will be returned as an error.} \tn \hhline{>{\arrayrulecolor{DarkBackground}}-} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{8.4cm}{X} \SetRowColor{DarkBackground} \mymulticolumn{1}{x{8.4cm}}{\bf\textcolor{white}{Aggregation}} \tn % Row 0 \SetRowColor{LightBackground} \mymulticolumn{1}{x{8.4cm}}{\{\{bt\}\}\{\{ac\}\}{\bf{Sum}}\{\{bb\}\}} \tn % Row Count 1 (+ 1) % Row 1 \SetRowColor{white} \mymulticolumn{1}{x{8.4cm}}{{\bf{Numpy}}\{\{nl\}\}`s = a.sum()` \{\{nl\}\}`s = np.sum(a)`} \tn % Row Count 2 (+ 1) % Row 2 \SetRowColor{LightBackground} \mymulticolumn{1}{x{8.4cm}}{{\bf{gonum/mat}}\{\{nl\}\}`var s float64 = mat.Sum(a)`} \tn % Row Count 3 (+ 1) % Row 3 \SetRowColor{white} \mymulticolumn{1}{x{8.4cm}}{{\bf{tensor}}\{\{nl\}\}`var s *ts.Dense = a.Sum()`\{\{nl\}\}`var s ts.Tensor = ts.Sum(a)`\{\{nl\}\}{\bf{Note: }} The result, which is a scalar value in this case, can be retrieved by calling `s.ScalarValue()`\{\{bb\}\}} \tn % Row Count 7 (+ 4) % Row 4 \SetRowColor{LightBackground} \mymulticolumn{1}{x{8.4cm}}{\{\{bt\}\}\{\{ac\}\}{\bf{Sum Along An Axis}} \{\{bb\}\}} \tn % Row Count 8 (+ 1) % Row 5 \SetRowColor{white} \mymulticolumn{1}{x{8.4cm}}{{\bf{Numpy}}\{\{nl\}\}`s = a.sum(axis=0)` \{\{nl\}\}`s = np.sum(a, axis=0)`} \tn % Row Count 10 (+ 2) % Row 6 \SetRowColor{LightBackground} \mymulticolumn{1}{x{8.4cm}}{{\bf{gonum/mat}}\{\{nl\}\} Write a loop, with manual aid from `mat.Col` and `mat.Row`\{\{nl\}\}{\bf{Note:}} There's no performance loss by writing a loop. In fact there arguably may be a cognitive gain in being aware of what one is doing.} \tn % Row Count 15 (+ 5) % Row 7 \SetRowColor{white} \mymulticolumn{1}{x{8.4cm}}{{\bf{tensor}}\{\{nl\}\}`var s *ts.Dense = a.Sum(0)`\{\{nl\}\}`var s ts.Tensor = ts.Sum(a, 0)` \{\{bb\}\}} \tn % Row Count 17 (+ 2) % Row 8 \SetRowColor{LightBackground} \mymulticolumn{1}{x{8.4cm}}{\{\{bt\}\}\{\{ac\}\}{\bf{Argmax/Argmin}}\{\{bb\}\}} \tn % Row Count 18 (+ 1) % Row 9 \SetRowColor{white} \mymulticolumn{1}{x{8.4cm}}{{\bf{Numpy}}\{\{nl\}\}`am = a.argmax()`\{\{nl\}\}`am = np.argmax(a)`} \tn % Row Count 20 (+ 2) % Row 10 \SetRowColor{LightBackground} \mymulticolumn{1}{x{8.4cm}}{{\bf{gonum}}\{\{nl\}\}Write a loop, using `mat.Col` and `mat.Row`} \tn % Row Count 22 (+ 2) % Row 11 \SetRowColor{white} \mymulticolumn{1}{x{8.4cm}}{{\bf{tensor}}\{\{nl\}\}`var am *ts.Dense; am, err = a.Argmax(ts.AllAxes)`\{\{nl\}\}`var am ts.Tensor; am, err = ts.Argmax(a, ts.AllAxes)` \{\{bb\}\} \{\{nobreak\}\}} \tn % Row Count 25 (+ 3) % Row 12 \SetRowColor{LightBackground} \mymulticolumn{1}{x{8.4cm}}{\{\{bt\}\}\{\{ac\}\}{\bf{Argmax/Argmin Along An Axis}}\{\{bb\}\}} \tn % Row Count 26 (+ 1) % Row 13 \SetRowColor{white} \mymulticolumn{1}{x{8.4cm}}{{\bf{Numpy}}\{\{nl\}\}`am = a.argmax(axis=0)`\{\{nl\}\}`am = np.argmax(a, axis=0)`} \tn % Row Count 28 (+ 2) % Row 14 \SetRowColor{LightBackground} \mymulticolumn{1}{x{8.4cm}}{{\bf{gonum}}\{\{nl\}\}Write a loop, using `mat.Col` and `mat.Row`} \tn % Row Count 30 (+ 2) \end{tabularx} \par\addvspace{1.3em} \vfill \columnbreak \begin{tabularx}{8.4cm}{X} \SetRowColor{DarkBackground} \mymulticolumn{1}{x{8.4cm}}{\bf\textcolor{white}{Aggregation (cont)}} \tn % Row 15 \SetRowColor{LightBackground} \mymulticolumn{1}{x{8.4cm}}{{\bf{tensor}}\{\{nl\}\}`var am *ts.Dense; am, err = a.Argmax(0)`\{\{nl\}\}`var am ts.Tensor; am, err = ts.Argmax(a, 0)` \{\{bb\}\}} \tn % Row Count 3 (+ 3) \hhline{>{\arrayrulecolor{DarkBackground}}-} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{8.4cm}{X} \SetRowColor{DarkBackground} \mymulticolumn{1}{x{8.4cm}}{\bf\textcolor{white}{Data Structure Creation}} \tn % Row 0 \SetRowColor{LightBackground} \mymulticolumn{1}{x{8.4cm}}{{\bf{Numpy}}\{\{nl\}\}`a = np.array({[}1, 2, 3{]})`} \tn % Row Count 1 (+ 1) % Row 1 \SetRowColor{white} \mymulticolumn{1}{x{8.4cm}}{{\bf{gonum/mat}}\{\{nl\}\}`a := mat.NewDense(1, 3, {[}{]}float64\{1, 2, 3\})`} \tn % Row Count 3 (+ 2) % Row 2 \SetRowColor{LightBackground} \mymulticolumn{1}{x{8.4cm}}{{\bf{tensor}}\{\{nl\}\}`a := ts.New(ts.WithBacking({[}{]}int\{1, 2, 3\})` \{\{bb\}\}} \tn % Row Count 5 (+ 2) % Row 3 \SetRowColor{white} \mymulticolumn{1}{x{8.4cm}}{\{\{bt\}\}\{\{ac\}\}{\bf{Creating a float64 matrix}} \{\{bb\}\}} \tn % Row Count 6 (+ 1) % Row 4 \SetRowColor{LightBackground} \mymulticolumn{1}{x{8.4cm}}{{\bf{Numpy}}\{\{nl\}\}`a = np.array({[}{[}0, 1, 2{]}, {[}3, 4, 5{]}{]}, dtype='float64')`} \tn % Row Count 8 (+ 2) % Row 5 \SetRowColor{white} \mymulticolumn{1}{x{8.4cm}}{{\bf{gonum/mat}}\{\{nl\}\}`a := mat.NewDense(2, 3, {[}{]}float64\{0, 1, 2, 3, 4, 5\})`} \tn % Row Count 10 (+ 2) % Row 6 \SetRowColor{LightBackground} \mymulticolumn{1}{x{8.4cm}}{{\bf{tensor}}\{\{nl\}\}`a := ts.New(ts.WithBacking({[}{]}float64\{0, 1, 2, 3, 4, 5\}, ts.WithShape(2, 3))` \{\{bb\}\}} \tn % Row Count 12 (+ 2) % Row 7 \SetRowColor{white} \mymulticolumn{1}{x{8.4cm}}{\{\{bt\}\}\{\{ac\}\}{\bf{Creating a float32 3-D array}}\{\{bb\}\}} \tn % Row Count 13 (+ 1) % Row 8 \SetRowColor{LightBackground} \mymulticolumn{1}{x{8.4cm}}{{\bf{Numpy}}\{\{nl\}\}`a = np.array({[}{[}{[}0, 1, 2{]}, {[}3, 4, 5{]}{]}, {[}{[}100, 101, 102{]}, {[}103, 104, 105{]}{]}{]}, dtype='float32')`} \tn % Row Count 16 (+ 3) % Row 9 \SetRowColor{white} \mymulticolumn{1}{x{8.4cm}}{{\bf{tensor}}\{\{nl\}\}`a := ts.New(ts.WithShape(2, 2, 3), ts.WithBacking({[}{]}float32\{0, 1, 2, 3, 4, 5, 100, 101, 102, 103, 104, 105\}))` \{\{noshy\}\}} \tn % Row Count 19 (+ 3) \hhline{>{\arrayrulecolor{DarkBackground}}-} \SetRowColor{LightBackground} \mymulticolumn{1}{x{8.4cm}}{{\bf{Note:}} The the `tensor` package is imported as `ts` \newline Additionally, `gonum/mat` actually offers many different data structures, each being useful to a particular subset of computations. The examples given in this document mainly assumes a dense matrix.} \tn \hhline{>{\arrayrulecolor{DarkBackground}}-} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{8.4cm}{x{1.84 cm} x{6.16 cm} } \SetRowColor{DarkBackground} \mymulticolumn{2}{x{8.4cm}}{\bf\textcolor{white}{gonum Types}} \tn % Row 0 \SetRowColor{LightBackground} \seqsplit{`mat.Matrix`} & Abstract data type representing any `float64` matrix \tn % Row Count 2 (+ 2) % Row 1 \SetRowColor{white} \seqsplit{`*mat.Dense`} & Data type representing a dense `float64` matrix \tn % Row Count 4 (+ 2) \hhline{>{\arrayrulecolor{DarkBackground}}--} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{8.4cm}{x{3.6 cm} x{4.4 cm} } \SetRowColor{DarkBackground} \mymulticolumn{2}{x{8.4cm}}{\bf\textcolor{white}{`tensor` Types}} \tn % Row 0 \SetRowColor{LightBackground} `tensor.Tensor` \{\{nobreak\}\} & An abstract data type representing any kind of tensors. Package functions work on these types. \tn % Row Count 5 (+ 5) % Row 1 \SetRowColor{white} `*tensor.Dense` \{\{nobreak\}\} & A representation of a densely packed multidimensional array. Methods return `*tensor.Dense` instead of `tensor.Tensor` \{\{noshy\}\} \tn % Row Count 11 (+ 6) % Row 2 \SetRowColor{LightBackground} `*tensor.CS` \{\{nobreak\}\} & A representation of compressed sparse row/column matrices. \tn % Row Count 14 (+ 3) % Row 3 \SetRowColor{white} `*tensor.MA` & {\emph{Coming soon}} - representation of masked multidimensional array. Methods return `*tensor.MA` instead of `tensor.Tensor` \{\{noshy\}\} \tn % Row Count 20 (+ 6) % Row 4 \SetRowColor{LightBackground} \seqsplit{`tensor.DenseTensor`} \{\{nobreak\}\} & Utility type that represents densely packed multidimensional arrays \{\{noshy\}\} \tn % Row Count 24 (+ 4) % Row 5 \SetRowColor{white} \seqsplit{`tensor.MaskedTensor`} \{\{nobreak\}\} & Utility type that represents densely packed multidimensional arrays that are masked by a slice of `bool` \{\{noshy\}\} \tn % Row Count 30 (+ 6) \end{tabularx} \par\addvspace{1.3em} \vfill \columnbreak \begin{tabularx}{8.4cm}{x{3.6 cm} x{4.4 cm} } \SetRowColor{DarkBackground} \mymulticolumn{2}{x{8.4cm}}{\bf\textcolor{white}{`tensor` Types (cont)}} \tn % Row 6 \SetRowColor{LightBackground} `tensor.Sparse` \{\{nobreak\}\} & Utility type that represents any sparsely packed multi-dim arrays (for now: only `*CS`) \{\{noshy\}\} \tn % Row Count 5 (+ 5) \hhline{>{\arrayrulecolor{DarkBackground}}--} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{8.4cm}{x{1.296 cm} x{2.304 cm} x{1.08 cm} x{2.52 cm} } \SetRowColor{DarkBackground} \mymulticolumn{4}{x{8.4cm}}{\bf\textcolor{white}{Metadata}} \tn % Row 0 \SetRowColor{LightBackground} {\bf{Metadata}} & {\bf{Numpy}} & {\bf{gonum}} & {\bf{tensor}} \tn % Row Count 2 (+ 2) % Row 1 \SetRowColor{white} {\bf{Shape}} & `a.shape`\{\{nobreak\}\} & \seqsplit{`a.Dims()`} & `a.Shape()`\{\{nobreak\}\} \tn % Row Count 4 (+ 2) % Row 2 \SetRowColor{LightBackground} {\bf{Strides}} & `a.strides`\{\{nobreak\}\} & & `a.Strides()`\{\{nobreak\}\} \tn % Row Count 6 (+ 2) % Row 3 \SetRowColor{white} {\bf{Dims}} & `a.ndim`\{\{nobreak\}\} & & `a.Dims()`\{\{nobreak\}\} \tn % Row Count 8 (+ 2) \hhline{>{\arrayrulecolor{DarkBackground}}----} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{8.4cm}{X} \SetRowColor{DarkBackground} \mymulticolumn{1}{x{8.4cm}}{\bf\textcolor{white}{Tensor Manipulation}} \tn % Row 0 \SetRowColor{LightBackground} \mymulticolumn{1}{x{8.4cm}}{\{\{bt\}\}\{\{ac\}\}{\bf{Zero-op Transpose}}\{\{bb\}\}} \tn % Row Count 1 (+ 1) % Row 1 \SetRowColor{white} \mymulticolumn{1}{x{8.4cm}}{{\bf{Numpy}}\{\{nl\}\}`aT = a.T`} \tn % Row Count 2 (+ 1) % Row 2 \SetRowColor{LightBackground} \mymulticolumn{1}{x{8.4cm}}{{\bf{gonum/mat}}\{\{nl\}\}`aT := a.T()`} \tn % Row Count 3 (+ 1) % Row 3 \SetRowColor{white} \mymulticolumn{1}{x{8.4cm}}{{\bf{tensor}}\{\{nl\}\}`a.T()` \{\{bb\}\}} \tn % Row Count 4 (+ 1) % Row 4 \SetRowColor{LightBackground} \mymulticolumn{1}{x{8.4cm}}{\{\{bt\}\}\{\{ac\}\}{\bf{Transpose With Data Movement}} \{\{bb\}\}} \tn % Row Count 6 (+ 2) % Row 5 \SetRowColor{white} \mymulticolumn{1}{x{8.4cm}}{{\bf{Numpy}}\{\{nl\}\}`aT = np.transpose(a)`} \tn % Row Count 7 (+ 1) % Row 6 \SetRowColor{LightBackground} \mymulticolumn{1}{x{8.4cm}}{{\bf{gonum/mat}}\{\{nl\}\}`b := a.T(); aT := mat.DenseCopyOf(b)`} \tn % Row Count 9 (+ 2) % Row 7 \SetRowColor{white} \mymulticolumn{1}{x{8.4cm}}{{\bf{tensor}}\{\{nl\}\}`aT, err := ts.Transpose(a)`\{\{nl\}\}{\emph{or}}\{\{nl\}\}`a.T(); err := a.Transpose()` \{\{bb\}\}} \tn % Row Count 11 (+ 2) % Row 8 \SetRowColor{LightBackground} \mymulticolumn{1}{x{8.4cm}}{\{\{bt\}\}\{\{ac\}\}{\bf{Reshape}}\{\{bb\}\}} \tn % Row Count 12 (+ 1) % Row 9 \SetRowColor{white} \mymulticolumn{1}{x{8.4cm}}{{\bf{Numpy}}\{\{nl\}\}`b = a.reshape(2,3)`} \tn % Row Count 13 (+ 1) % Row 10 \SetRowColor{LightBackground} \mymulticolumn{1}{x{8.4cm}}{{\bf{gonum/mat}}\{\{nl\}\}`b := NewDense(2, 3, a.RawMatrix().Data)`} \tn % Row Count 15 (+ 2) % Row 11 \SetRowColor{white} \mymulticolumn{1}{x{8.4cm}}{{\bf{tensor}}\{\{nl\}\}`err := a.Reshape(2,3)`} \tn % Row Count 16 (+ 1) \hhline{>{\arrayrulecolor{DarkBackground}}-} \SetRowColor{LightBackground} \mymulticolumn{1}{x{8.4cm}}{Note on reshaping when using gonum: the matrix `a` mustn't be a view.} \tn \hhline{>{\arrayrulecolor{DarkBackground}}-} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{8.4cm}{X} \SetRowColor{DarkBackground} \mymulticolumn{1}{x{8.4cm}}{\bf\textcolor{white}{Linear Algebra}} \tn % Row 0 \SetRowColor{LightBackground} \mymulticolumn{1}{x{8.4cm}}{\{\{bt\}\}\{\{ac\}\}{\bf{Inner Product of Vectors}}\{\{bb\}\}} \tn % Row Count 1 (+ 1) % Row 1 \SetRowColor{white} \mymulticolumn{1}{x{8.4cm}}{{\bf{Numpy}}\{\{nl\}\}`c = np.inner(a, b)`} \tn % Row Count 2 (+ 1) % Row 2 \SetRowColor{LightBackground} \mymulticolumn{1}{x{8.4cm}}{{\bf{gonum}}\{\{nl\}\}`var c float64 = mat.Dot(a, b)`} \tn % Row Count 3 (+ 1) % Row 3 \SetRowColor{white} \mymulticolumn{1}{x{8.4cm}}{{\bf{tensor}}\{\{nl\}\}`var c interface\{\} = ts.Inner(a, b)`\{\{nl\}\}{\emph{or}}\{\{nl\}\}`var c interface\{\} = a.Inner(b)`\{\{nl\}\}{\bf{Note:}} The `tensor` package comes with specialized execution engines for `float64` and `float32` which will return `float64` or `float32` without returning an `interface\{\}` \{\{bb\}\}} \tn % Row Count 9 (+ 6) % Row 4 \SetRowColor{LightBackground} \mymulticolumn{1}{x{8.4cm}}{\{\{bt\}\}\{\{ac\}\}{\bf{Matrix-Vector Multiplication}}\{\{bb\}\}} \tn % Row Count 10 (+ 1) % Row 5 \SetRowColor{white} \mymulticolumn{1}{x{8.4cm}}{{\bf{Numpy}} \{\{nl\}\}`mv = np.dot(m, v)`\{\{nl\}\}{\emph{or}}\{\{nl\}\}`mv = np.matmul(m, v)`\{\{nl\}\}{\emph{or}}\{\{nl\}\}`mv = m @ v`\{\{nl\}\}{\emph{or}}\{\{nl\}\}`mv = m.dot(v)`} \tn % Row Count 13 (+ 3) % Row 6 \SetRowColor{LightBackground} \mymulticolumn{1}{x{8.4cm}}{{\bf{gonum}}\{\{nl\}\}`mv.Mul(m, v)`} \tn % Row Count 14 (+ 1) % Row 7 \SetRowColor{white} \mymulticolumn{1}{x{8.4cm}}{{\bf{tensor}}\{\{nl\}\}`var mv ts.Tensor; mv, \_ = ts.MatVecMul(m, v)`\{\{nl\}\}{\emph{or}}\{\{nl\}\}`var mv *Dense; mv, \_ = m.MatVecMul(v)` \{\{bb\}\}} \tn % Row Count 17 (+ 3) % Row 8 \SetRowColor{LightBackground} \mymulticolumn{1}{x{8.4cm}}{\{\{bt\}\}\{\{ac\}\}{\bf{Matrix-Matrix Multiplication}} \{\{bb\}\}} \tn % Row Count 19 (+ 2) % Row 9 \SetRowColor{white} \mymulticolumn{1}{x{8.4cm}}{{\bf{Numpy}} \{\{nl\}\}`mm = np.dot(m1, m2)`\{\{nl\}\}{\emph{or}}\{\{nl\}\}`mm = np.matmul(m1, m2)`\{\{nl\}\}{\emph{or}}\{\{nl\}\}`mm = m1 @ m2`\{\{nl\}\}{\emph{or}}\{\{nl\}\}`mm = m1.dot(m2)`} \tn % Row Count 22 (+ 3) % Row 10 \SetRowColor{LightBackground} \mymulticolumn{1}{x{8.4cm}}{{\bf{gonum}}\{\{nl\}\}`mm.Mul(m1, m2)`} \tn % Row Count 23 (+ 1) % Row 11 \SetRowColor{white} \mymulticolumn{1}{x{8.4cm}}{{\bf{tensor}}\{\{nl\}\}`var mm Tensor; mm, \_ = ts.MatMul(m1, m2)`\{\{nl\}\}{\emph{or}}\{\{nl\}\}`var mm *ts.Dense; mm, \_ = m1.MatMul(m2)`\{\{bb\}\}} \tn % Row Count 26 (+ 3) % Row 12 \SetRowColor{LightBackground} \mymulticolumn{1}{x{8.4cm}}{\{\{bt\}\}\{\{ac\}\}{\bf{Magic}}\{\{bb\}\}} \tn % Row Count 27 (+ 1) % Row 13 \SetRowColor{white} \mymulticolumn{1}{x{8.4cm}}{{\bf{Numpy}}\{\{nl\}\}`c = np.dot(a, b)`\{\{nl\}\}`c = a.dot(b)`} \tn % Row Count 29 (+ 2) % Row 14 \SetRowColor{LightBackground} \mymulticolumn{1}{x{8.4cm}}{{\bf{tensor}}\{\{nl\}\}`var c ts.Tensor; c, \_ = ts.Dot(a, b)`\{\{nl\}\}`var c *ts.Dense; c, \_ = a.Dot(b)`} \tn % Row Count 31 (+ 2) \end{tabularx} \par\addvspace{1.3em} \vfill \columnbreak \begin{tabularx}{8.4cm}{X} \SetRowColor{DarkBackground} \mymulticolumn{1}{x{8.4cm}}{\bf\textcolor{white}{Linear Algebra (cont)}} \tn % Row 15 \SetRowColor{LightBackground} \mymulticolumn{1}{x{8.4cm}}{{\bf{Note:}} The `Dot` function and method in package `tensor` works similarly to `dot` in Numpy - depending on the number of dimensions of the inputs, different functions will be called. You should treat it as a "magic" function that does products of two multi-dimensional arrays.\{\{noshy\}\}} \tn % Row Count 6 (+ 6) \hhline{>{\arrayrulecolor{DarkBackground}}-} \SetRowColor{LightBackground} \mymulticolumn{1}{x{8.4cm}}{`gonum` has a whole suite of linear-algebra functions and structures that are too many to enumerate here. You should check it out too.} \tn \hhline{>{\arrayrulecolor{DarkBackground}}-} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{8.4cm}{X} \SetRowColor{DarkBackground} \mymulticolumn{1}{x{8.4cm}}{\bf\textcolor{white}{Combinations}} \tn % Row 0 \SetRowColor{LightBackground} \mymulticolumn{1}{x{8.4cm}}{\{\{bt\}\}\{\{ac\}\}{\bf{Concatenation}}\{\{bb\}\}} \tn % Row Count 1 (+ 1) % Row 1 \SetRowColor{white} \mymulticolumn{1}{x{8.4cm}}{{\bf{Numpy}} \{\{nl\}\}`c = np.concatenate((a, b), axis=0)`} \tn % Row Count 3 (+ 2) % Row 2 \SetRowColor{LightBackground} \mymulticolumn{1}{x{8.4cm}}{{\bf{gonum/mat}}\{\{nl\}\}`c.Stack(a,b)`} \tn % Row Count 4 (+ 1) % Row 3 \SetRowColor{white} \mymulticolumn{1}{x{8.4cm}}{{\bf{tensor}}\{\{nl\}\}`var c ts.Tensor; c, err = ts.Concat(0, a, b)`\{\{nl\}\}`var c *ts.Dense; c, err = a.Concat(0, b)`} \tn % Row Count 7 (+ 3) % Row 4 \SetRowColor{LightBackground} \mymulticolumn{1}{x{8.4cm}}{\{\{bt\}\}\{\{ac\}\}{\bf{Vstack}}\{\{bb\}\}} \tn % Row Count 8 (+ 1) % Row 5 \SetRowColor{white} \mymulticolumn{1}{x{8.4cm}}{{\bf{Numpy}} \{\{nl\}\}`c = np.vstack((a, b))`} \tn % Row Count 9 (+ 1) % Row 6 \SetRowColor{LightBackground} \mymulticolumn{1}{x{8.4cm}}{{\bf{gonum/mat}}\{\{nl\}\}`c.Stack(a,b)`} \tn % Row Count 10 (+ 1) % Row 7 \SetRowColor{white} \mymulticolumn{1}{x{8.4cm}}{{\bf{tensor}}\{\{nl\}\}`var c *ts.Dense; c, err = a.Vstack(0, b)` \{\{bb\}\}} \tn % Row Count 12 (+ 2) % Row 8 \SetRowColor{LightBackground} \mymulticolumn{1}{x{8.4cm}}{\{\{bt\}\}\{\{ac\}\}{\bf{Hstack}}\{\{bb\}\}} \tn % Row Count 13 (+ 1) % Row 9 \SetRowColor{white} \mymulticolumn{1}{x{8.4cm}}{{\bf{Numpy}} \{\{nl\}\}`c = np.hstack((a, b))`} \tn % Row Count 14 (+ 1) % Row 10 \SetRowColor{LightBackground} \mymulticolumn{1}{x{8.4cm}}{{\bf{gonum/mat}}\{\{nl\}\}`c.Augment(a,b)`} \tn % Row Count 15 (+ 1) % Row 11 \SetRowColor{white} \mymulticolumn{1}{x{8.4cm}}{{\bf{tensor}}\{\{nl\}\}`var c *ts.Dense; c, err = a.Hstack(0, b)` \{\{bb\}\}} \tn % Row Count 17 (+ 2) % Row 12 \SetRowColor{LightBackground} \mymulticolumn{1}{x{8.4cm}}{\{\{bt\}\}\{\{ac\}\}{\bf{Stack onto a New Axis}}\{\{bb\}\}} \tn % Row Count 18 (+ 1) % Row 13 \SetRowColor{white} \mymulticolumn{1}{x{8.4cm}}{{\bf{Numpy}} \{\{nl\}\}`c = np.stack((a, b))`} \tn % Row Count 19 (+ 1) % Row 14 \SetRowColor{LightBackground} \mymulticolumn{1}{x{8.4cm}}{{\bf{gonum/mat}}\{\{nl\}\}`var stacked {[}{]}mat.Matrix; stacked = append(stacked, a, b)`} \tn % Row Count 21 (+ 2) % Row 15 \SetRowColor{white} \mymulticolumn{1}{x{8.4cm}}{{\bf{tensor}}\{\{nl\}\}`var c ts.Tensor; c, \_ = ts.Stack(0, a, b)`\{\{nl\}\}`var c *ts.Dense; c, \_ = a.Stack(0,b)`\{\{nl\}\}{\bf{Note:}} Unlike in Numpy, `Stack` in `tensor` is a little more strict on the axis. It has to be specified.\{\{bb\}\}} \tn % Row Count 26 (+ 5) % Row 16 \SetRowColor{LightBackground} \mymulticolumn{1}{x{8.4cm}}{\{\{bt\}\}\{\{ac\}\}{\bf{Repeats}}\{\{bb\}\}} \tn % Row Count 27 (+ 1) % Row 17 \SetRowColor{white} \mymulticolumn{1}{x{8.4cm}}{{\bf{Numpy}} \{\{nl\}\}`c = np.repeat(a, 2) \# returns a flat array`\{\{nl\}\}`c = np.repeat(a, 2, axis=0) \# repeats along axis 0`\{\{nl\}\}`c = np.repeat(a, 2, axis=1) \# repeats along axis 1`} \tn % Row Count 31 (+ 4) \end{tabularx} \par\addvspace{1.3em} \vfill \columnbreak \begin{tabularx}{8.4cm}{X} \SetRowColor{DarkBackground} \mymulticolumn{1}{x{8.4cm}}{\bf\textcolor{white}{Combinations (cont)}} \tn % Row 18 \SetRowColor{LightBackground} \mymulticolumn{1}{x{8.4cm}}{{\bf{gonum}}\{\{nl\}\}Unsupported for now} \tn % Row Count 1 (+ 1) % Row 19 \SetRowColor{white} \mymulticolumn{1}{x{8.4cm}}{{\bf{tensor}}\{\{nl\}\}`var c ts.Tensor; c, \_ = ts.Repeat(a, ts.AllAxes, 2) // returns a flat array`\{\{nl\}\}`c = ts.Repeat(a, 0, 2) // repeats along axis 0 `\{\{nl\}\}`c = ts.Repeat(a, 1, 2) // repeats along axis 1`} \tn % Row Count 6 (+ 5) \hhline{>{\arrayrulecolor{DarkBackground}}-} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{8.4cm}{X} \SetRowColor{DarkBackground} \mymulticolumn{1}{x{8.4cm}}{\bf\textcolor{white}{Data Access}} \tn % Row 0 \SetRowColor{LightBackground} \mymulticolumn{1}{x{8.4cm}}{\{\{bt\}\}\{\{ac\}\}{\bf{Value At (Assuming Matrices)}}\{\{bb\}\}} \tn % Row Count 1 (+ 1) % Row 1 \SetRowColor{white} \mymulticolumn{1}{x{8.4cm}}{{\bf{Numpy}}\{\{nl\}\}`val = a{[}0, 0{]}`} \tn % Row Count 2 (+ 1) % Row 2 \SetRowColor{LightBackground} \mymulticolumn{1}{x{8.4cm}}{{\bf{gonum/mat}}\{\{nl\}\}`var va float64 := a.At(0, 0)`} \tn % Row Count 3 (+ 1) % Row 3 \SetRowColor{white} \mymulticolumn{1}{x{8.4cm}}{{\bf{tensor}}\{\{nl\}\}`var val interface\{\}; val, \_ = a.At(0,0)` \{\{bb\}\}} \tn % Row Count 5 (+ 2) % Row 4 \SetRowColor{LightBackground} \mymulticolumn{1}{x{8.4cm}}{\{\{bt\}\}\{\{ac\}\}{\bf{Slice Row or Column (Assuming Matrices) }}\{\{bb\}\}} \tn % Row Count 7 (+ 2) % Row 5 \SetRowColor{white} \mymulticolumn{1}{x{8.4cm}}{{\bf{Numpy}}\{\{nl\}\}`row = a{[}0{]}`\{\{nl\}\}`col = a{[}:, 0{]}`} \tn % Row Count 8 (+ 1) % Row 6 \SetRowColor{LightBackground} \mymulticolumn{1}{x{8.4cm}}{{\bf{gonum/mat}}\{\{nl\}\}`var row mat.Vector = a.RowView(0)`\{\{nl\}\}`var col mat.Vector = a.ColView(0)`} \tn % Row Count 10 (+ 2) % Row 7 \SetRowColor{white} \mymulticolumn{1}{x{8.4cm}}{{\bf{tensor}}\{\{nl\}\}`var row ts.View = a.Slice(s(0))`\{\{nl\}\}`var col ts.View = a.Slice(nil, s(0))` \{\{bb\}\}} \tn % Row Count 12 (+ 2) % Row 8 \SetRowColor{LightBackground} \mymulticolumn{1}{x{8.4cm}}{\{\{bt\}\}\{\{ac\}\}{\bf{Advanced Slicing (Assuming 9x9 Matrices)}}\{\{bb\}\}} \tn % Row Count 14 (+ 2) % Row 9 \SetRowColor{white} \mymulticolumn{1}{x{8.4cm}}{{\bf{Numpy}}\{\{nl\}\}`b = a{[}1:4, 3:6{]}`} \tn % Row Count 15 (+ 1) % Row 10 \SetRowColor{LightBackground} \mymulticolumn{1}{x{8.4cm}}{{\bf{gonum/mat}}\{\{nl\}\}`var b mat.Matrix = a.Slice(1,4, 3,6)`} \tn % Row Count 17 (+ 2) % Row 11 \SetRowColor{white} \mymulticolumn{1}{x{8.4cm}}{{\bf{tensor}}\{\{nl\}\}`var b ts.View = a.Slice(rs(1,4), rs(3,6))`\{\{bb\}\}} \tn % Row Count 19 (+ 2) % Row 12 \SetRowColor{LightBackground} \mymulticolumn{1}{x{8.4cm}}{\{\{bt\}\}\{\{ac\}\}{\bf{Advanced Slicing With Steps}} \{\{bb\}\}} \tn % Row Count 20 (+ 1) % Row 13 \SetRowColor{white} \mymulticolumn{1}{x{8.4cm}}{{\bf{Numpy}}\{\{nl\}\}`b = a{[}1:4:1, 3:6:2{]}`} \tn % Row Count 21 (+ 1) % Row 14 \SetRowColor{LightBackground} \mymulticolumn{1}{x{8.4cm}}{{\bf{gonum/mat}}\{\{nl\}\}Unsupported} \tn % Row Count 22 (+ 1) % Row 15 \SetRowColor{white} \mymulticolumn{1}{x{8.4cm}}{{\bf{tensor}}\{\{nl\}\}`var b ts.View = a.Slice(rs(1,4,1), rs(3,6,2))` \{\{bb\}\}} \tn % Row Count 24 (+ 2) % Row 16 \SetRowColor{LightBackground} \mymulticolumn{1}{x{8.4cm}}{\{\{bt\}\}\{\{ac\}\}{\bf{Getting Underlying Data}}\{\{bb\}\}} \tn % Row Count 25 (+ 1) % Row 17 \SetRowColor{white} \mymulticolumn{1}{x{8.4cm}}{{\bf{Numpy}}\{\{nl\}\}`b = a.ravel()`} \tn % Row Count 26 (+ 1) % Row 18 \SetRowColor{LightBackground} \mymulticolumn{1}{x{8.4cm}}{{\bf{gonum/mat}}\{\{nl\}\}`var b {[}{]}float64 = a.RawMatrix().Data`} \tn % Row Count 28 (+ 2) % Row 19 \SetRowColor{white} \mymulticolumn{1}{x{8.4cm}}{{\bf{tensor}}\{\{nl\}\}`var b interface\{\} = a.Data()` \{\{bb\}\}} \tn % Row Count 30 (+ 2) \end{tabularx} \par\addvspace{1.3em} \vfill \columnbreak \begin{tabularx}{8.4cm}{X} \SetRowColor{DarkBackground} \mymulticolumn{1}{x{8.4cm}}{\bf\textcolor{white}{Data Access (cont)}} \tn % Row 20 \SetRowColor{LightBackground} \mymulticolumn{1}{x{8.4cm}}{\{\{bt\}\}\{\{ac\}\}{\bf{Setting One Value (Assuming Matrices)}} \{\{bb\}\}} \tn % Row Count 2 (+ 2) % Row 21 \SetRowColor{white} \mymulticolumn{1}{x{8.4cm}}{{\bf{Numpy}}\{\{nl\}\}`a{[}r, c{]} = 100`} \tn % Row Count 3 (+ 1) % Row 22 \SetRowColor{LightBackground} \mymulticolumn{1}{x{8.4cm}}{{\bf{gonum/mat}}\{\{nl\}\}`a.Set(r, c, 100)`} \tn % Row Count 4 (+ 1) % Row 23 \SetRowColor{white} \mymulticolumn{1}{x{8.4cm}}{{\bf{tensor}}\{\{nl\}\}`a.SetAt(100, r, c)` \{\{bb\}\}} \tn % Row Count 5 (+ 1) % Row 24 \SetRowColor{LightBackground} \mymulticolumn{1}{x{8.4cm}}{\{\{bt\}\}\{\{ac\}\}{\bf{Setting Row/Col (Assuming 3x3 Matrix)}}\{\{bb\}\}} \tn % Row Count 7 (+ 2) % Row 25 \SetRowColor{white} \mymulticolumn{1}{x{8.4cm}}{{\bf{Numpy}}\{\{nl\}\}`a{[}r{]} = {[}1, 2, 3{]}`\{\{nl\}\}`a{[}:, c{]} = {[}1, 2, 3{]}`} \tn % Row Count 9 (+ 2) % Row 26 \SetRowColor{LightBackground} \mymulticolumn{1}{x{8.4cm}}{{\bf{gonum/mat}}\{\{nl\}\}`a.SetRow(r, {[}{]}float64\{1, 2, 3\})`\{\{nl\}\}`a.SetCol(c, {[}{]}float64\{1, 2, 3\})`} \tn % Row Count 11 (+ 2) % Row 27 \SetRowColor{white} \mymulticolumn{1}{x{8.4cm}}{{\bf{tensor}}\{\{nl\}\}No simple method - requires `Iterator`s and multiple lines of code.} \tn % Row Count 13 (+ 2) \hhline{>{\arrayrulecolor{DarkBackground}}-} \SetRowColor{LightBackground} \mymulticolumn{1}{x{8.4cm}}{{\bf{Note}}: in the `tensor` examples, the `a.Slice` method take a list of `tensor.Slice` which is an interface defined \{\{link="https://github.com/chewxy/gorgonia/blob/master/tensor/slice.go\#L4-L8"\}\}here\{\{/link\}\}. `s`, and `rs` in the examples simply represent types that implement the `tensor.Slice` type. A `nil` is treated as a `:` in Python. There are no default `tensor.Slice` types provided, and it is up to the user to define their own.} \tn \hhline{>{\arrayrulecolor{DarkBackground}}-} \end{tabularx} \par\addvspace{1.3em} % That's all folks \end{multicols*} \end{document}