├── .gitignore ├── AUTHORS ├── COPYING ├── ChangeLog ├── Makefile.am ├── NEWS ├── README ├── build.sh ├── configure.ac ├── cpp ├── Makefile.am ├── add_ok.cpp ├── cholesky_ok.cpp ├── diag_prod_ok.cpp ├── element_div_ok.cpp ├── element_prod_ok.cpp ├── element_unary_ok.cpp ├── extend_ok.cpp ├── getstarted.cpp ├── mat2cpp_ok.cpp ├── matrix_div_ok.cpp ├── matrix_prod_ok.cpp ├── max_ok.cpp ├── min_ok.cpp ├── mztuni_ok.cpp ├── ones_ok.cpp ├── rand_ok.cpp ├── randn_ok.cpp ├── scalar_div_ok.cpp ├── scalar_ok.cpp ├── scalar_prod_ok.cpp ├── size_ok.cpp ├── slice_ok.cpp ├── sub_ok.cpp ├── sum_ok.cpp ├── transpose_ok.cpp └── zeros_ok.cpp ├── doc.omh ├── gh_pages.sh ├── lib ├── Makefile.am ├── c2dpotrf.f ├── cholesky.cpp ├── diag_prod.cpp ├── element_unary.cpp ├── matrix_div.cpp ├── max.cpp ├── min.cpp ├── mztuni.cpp ├── rand.cpp ├── randn.cpp ├── scalar.cpp └── sum.cpp ├── mat ├── add_ok.m ├── cholesky_ok.m ├── diag_prod_ok.m ├── element_div_ok.m ├── element_prod_ok.m ├── element_unary_ok.m ├── extend_ok.m ├── getstarted.m ├── mat2cpp_ok.m ├── matrix_div_ok.m ├── matrix_prod_ok.m ├── max_ok.m ├── min_ok.m ├── ones_ok.m ├── rand_ok.m ├── randn_ok.m ├── scalar_div_ok.m ├── scalar_ok.m ├── scalar_prod_ok.m ├── size_ok.m ├── slice_ok.m ├── sub_ok.m ├── sum_ok.m ├── transpose_ok.m └── zeros_ok.m ├── mat2cpp.hpp ├── mztuni.hpp ├── omh ├── add.omh ├── cholesky.omh ├── diag_prod.omh ├── element_div.omh ├── element_prod.omh ├── element_unary.omh ├── elementwise.omh ├── extend.omh ├── getstarted.omh ├── library.omh ├── matrix_div.omh ├── matrix_prod.omh ├── matrixwise.omh ├── max.omh ├── min.omh ├── mztuni.omh ├── ones.omh ├── operation.omh ├── other.omh ├── rand.omh ├── randn.omh ├── scalar.omh ├── scalar_div.omh ├── scalar_prod.omh ├── scalar_valued.omh ├── size.omh ├── slice.omh ├── sub.omh ├── sum.omh ├── transpose.omh └── zeros.omh └── run_omhelp.sh /.gitignore: -------------------------------------------------------------------------------- 1 | # directories to ignore 2 | /build/ 3 | /autom4te.cache/ 4 | /doc/ 5 | # files to ignore 6 | /INSTALL 7 | /aclocal.m4 8 | /compile 9 | /config.h.in~ 10 | /config.guess 11 | /config.h.in 12 | /config.sub 13 | /configure 14 | /depcomp 15 | /git_commit.sh 16 | /install-sh 17 | /junk 18 | /missing 19 | /omhelp.log 20 | # --------------------------------------------------------------------------- 21 | # files to ignore in any directory 22 | # 23 | # autotools output files 24 | Makefile.in 25 | # 26 | # vim editor swap files 27 | *.swp 28 | -------------------------------------------------------------------------------- /AUTHORS: -------------------------------------------------------------------------------- 1 | Name: Bradley M. Bell 2 | Web: http://www.seanet.com/~bradbell 3 | -------------------------------------------------------------------------------- /COPYING: -------------------------------------------------------------------------------- 1 | $begin License$$ 2 | $spell 3 | cpp 4 | Matlab 5 | Bradley 6 | $$ 7 | 8 | $section The mat2cpp Software License$$ 9 | 10 | $pre 11 | Name: mat2cpp: Example Conversion of Matlab or Octave to C++ 12 | Owner: Copyright (C) 2006 University of Washington 13 | Author: (Bradley M. Bell) 14 | 15 | $$ 16 | $href%http://www.boost.org/LICENSE_1_0.txt%$$ 17 | $pre 18 | 19 | Boost Software License - Version 1.0 - August 17th, 2003 20 | 21 | Permission is hereby granted, free of charge, to any person or organization 22 | obtaining a copy of the software and accompanying documentation covered by 23 | this license (the "Software") to use, reproduce, display, distribute, 24 | execute, and transmit the Software, and to prepare derivative works of the 25 | Software, and to permit third-parties to whom the Software is furnished to 26 | do so, all subject to the following: 27 | 28 | The copyright notices in the Software and this entire statement, including 29 | the above license grant, this restriction and the following disclaimer, 30 | must be included in all copies of the Software, in whole or in part, and 31 | all derivative works of the Software, unless such copies or derivative 32 | works are solely in the form of machine-executable object code generated by 33 | a source language processor. 34 | 35 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 36 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 37 | FITNESS FOR A PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT. IN NO EVENT 38 | SHALL THE COPYRIGHT HOLDERS OR ANYONE DISTRIBUTING THE SOFTWARE BE LIABLE 39 | FOR ANY DAMAGES OR OTHER LIABILITY, WHETHER IN CONTRACT, TORT OR OTHERWISE, 40 | ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER 41 | DEALINGS IN THE SOFTWARE. 42 | $$ 43 | 44 | $end 45 | -------------------------------------------------------------------------------- /ChangeLog: -------------------------------------------------------------------------------- 1 | see ./NEWS or ./doc/news.xml file 2 | -------------------------------------------------------------------------------- /Makefile.am: -------------------------------------------------------------------------------- 1 | # 2 | SUBDIRS = lib cpp 3 | # 4 | include_HEADERS = mat2cpp.hpp mztuni.hpp 5 | # 6 | EXTRA_DIST = \ 7 | build.sh \ 8 | run_omhelp.sh \ 9 | doc.omh \ 10 | doc \ 11 | omh \ 12 | mat 13 | # 14 | dist-hook: 15 | rm -rf $(distdir)/.git 16 | -------------------------------------------------------------------------------- /NEWS: -------------------------------------------------------------------------------- 1 | $begin News$$ 2 | $spell 3 | cpp 4 | randn 5 | $$ 6 | 7 | $section The mat2cpp News file$$ 8 | 9 | $head 2013-07-25$$ 10 | Use 3 standard deviations, instead of two, when checking results 11 | in $cref/randn C++ example/randn/C++/Example/$$ 12 | (because two standard deviations was failing to often). 13 | 14 | $head 2013-07-20$$ 15 | Added the $cref mztuni$$ function so that random number generation 16 | was repeatable on different machines. 17 | In addition, seeded the random numbers off the clock 18 | so that a different test is run each time (but can be reproduced 19 | in the case of an error). 20 | 21 | $head 06-05-15$$ 22 | Added $xref/randn/$$ library routine. 23 | Also added the section $tref library$$ to the documentation. 24 | 25 | $head 06-05-05$$ 26 | Added the $xref/cholesky/$$ library routine. 27 | 28 | $head 06-04-18$$ 29 | Release of first version. 30 | 31 | $end 32 | -------------------------------------------------------------------------------- /README: -------------------------------------------------------------------------------- 1 | # Example Conversion of Matlab or Octave to C++ 2 | 3 | ## Support 4 | This package is no longer under development or supported. 5 | You could clone it and start your own development stream. 6 | 7 | ## Documentation 8 | https://bradbell.github.io/mat2cpp/doc/mat2cpp.htm 9 | 10 | ## License 11 | https://opensource.org/licenses/BSL-1.0 12 | 13 | ## Default Branch 14 | Converted master -> main on 2025-04-17 15 | -------------------------------------------------------------------------------- /build.sh: -------------------------------------------------------------------------------- 1 | #! /bin/bash -e 2 | # ----------------------------------------------------------------------------- 3 | if [ "$0" != './build.sh' ] 4 | then 5 | echo './build.sh: must be run from top source directory' 6 | exit 1 7 | fi 8 | # ---------------------------------------------------------------- 9 | boost_dir=/usr/include # prefix for boost on this machine 10 | prefix=$HOME/prefix/mat2cpp # prefix for installation 11 | # ---------------------------------------------------------------- 12 | # 13 | Today=`date +%F | sed -e 's|-||g'` 14 | # 15 | if [ "$1" == "-g" ] 16 | then 17 | echo "Building Debug Version:" 18 | compile_flags="-g -Wall" 19 | else 20 | echo "Building Optimized Version:" 21 | compile_flags="-DNDEBUG -O2 -Wall" 22 | fi 23 | # 24 | for name in \ 25 | NEWS \ 26 | README \ 27 | AUTHORS \ 28 | ChangeLog 29 | do 30 | if [ ! -e $name ] 31 | then 32 | echo "no $name file in mat2cpp distribution" > $name 33 | fi 34 | done 35 | # 36 | sed -i configure.ac \ 37 | -e "s|AC_INIT(mat2cpp, [0-9]\{8\},|AC_INIT(mat2cpp, $Today,|" 38 | # 39 | sed -i doc.omh \ 40 | -e "s|mat2cpp-[0-9]\{8\}|mat2cpp-$Today|" 41 | # 42 | echo "autoreconf --force --install" 43 | autoreconf --force --install 44 | # 45 | echo "./run_omhelp.sh" 46 | ./run_omhelp.sh 47 | # 48 | echo 'cd build' 49 | if [ ! -e build ] 50 | then 51 | mkdir build 52 | fi 53 | cd build 54 | # 55 | cat << EOF 56 | ../configure \ 57 | --prefix=$prefix \ 58 | BOOST_DIR=$boost_dir \ 59 | COMPILE_FLAGS="$compile_flags" 60 | EOF 61 | ../configure \ 62 | --prefix=$prefix \ 63 | BOOST_DIR=$boost_dir \ 64 | COMPILE_FLAGS="$compile_flags" 65 | # 66 | echo "make" 67 | make 68 | # 69 | # 70 | echo "make dist" 71 | make dist 72 | # 73 | if [ -e "mat2cpp-$Today" ] 74 | then 75 | rm mat2cpp-$Today 76 | fi 77 | # 78 | echo "cp mat2cpp-$Today.tar.gz ../doc/mat2cpp-$Today.tar.gz" 79 | cp mat2cpp-$Today.tar.gz ../doc/mat2cpp-$Today.tar.gz 80 | # 81 | echo "tar -xvzf mat2cpp-$Today.tar.gz" 82 | tar -xvzf mat2cpp-$Today.tar.gz 83 | # 84 | cd mat2cpp-$Today 85 | # 86 | cat << EOF 87 | ./configure \\ 88 | --prefix=$prefix \\ 89 | BOOST_DIR=$boost_dir \\ 90 | COMPILE_FLAGS="$compile_flags" 91 | EOF 92 | # 93 | ./configure \ 94 | --prefix=$prefix \ 95 | BOOST_DIR=$boost_dir \ 96 | COMPILE_FLAGS="$compile_flags" 97 | # 98 | echo "make" 99 | make 100 | # 101 | echo "build/cpp/mat2cpp_ok" 102 | cpp/mat2cpp_ok 103 | # ----------------------------------------------------------------------------- 104 | echo "build.sh: OK" 105 | exit 0 106 | -------------------------------------------------------------------------------- /configure.ac: -------------------------------------------------------------------------------- 1 | dnl package, version, bugreport 2 | AC_INIT(mat2cpp, 20180924, https://github.com/bradbell/mat2cpp/issues) 3 | 4 | dnl The directory corresponding to the installation of BOOST ------------ 5 | AC_MSG_CHECKING( [BOOST_DIR] ) 6 | AC_SUBST(BOOST_DIR) 7 | if test "$BOOST_DIR" != "" ; then 8 | AC_MSG_RESULT( [ BOOST_DIR=$BOOST_DIR ] ) 9 | else 10 | AC_MSG_ERROR( [ BOOST_DIR=$BOOST_DIR ] ) 11 | fi 12 | 13 | dnl The C++ error and warning flags for every compliation --------------- 14 | AC_MSG_CHECKING( [COMPILE_FLAGS] ) 15 | AC_SUBST(COMPILE_FLAGS) 16 | AM_CONDITIONAL(mat2cpp_COMPILE_FLAGS, test "$COMPILE_FLAGS" != "" ) 17 | if test "$COMPILE_FLAGS" != "" ; then 18 | AC_MSG_RESULT( [COMPILE_FLAGS=$COMPILE_FLAGS] ) 19 | else 20 | AC_MSG_RESULT( [no] ) 21 | fi 22 | 23 | dnl Run any macros required for proper operation of generated Makefiles 24 | dnl nostdinc: Do not define standard include directories in generated Makefiles 25 | AM_INIT_AUTOMAKE(nostdinc) 26 | 27 | dnl Use config.h instead of make file command line arguments 28 | AM_CONFIG_HEADER( config.h ) 29 | 30 | dnl AC_PROG_CXX([compiler-search-list]) outputs CXX as C++ compiler to use 31 | AC_PROG_CXX 32 | 33 | dnl No default setting for compiler flags 34 | AC_SUBST(CXXFLAGS, "") 35 | 36 | dnl AC_PROG_F77([compiler-search-list]) outputs F77 as Fortran compiler to use 37 | AC_PROG_F77 38 | dnl Macros for linking fortran to C 39 | AC_F77_WRAPPERS 40 | 41 | dnl Determine if ranlib is present and where it is if present (set RANLIB) 42 | AC_PROG_RANLIB 43 | 44 | dnl Have configure check distribution directory 45 | AC_CONFIG_SRCDIR([mat2cpp.hpp]) 46 | 47 | dnl AC_CONFIG_FILES(file-list) for each file in the list, configure will 48 | dnl read file.in, do its substitutions, and create file 49 | AC_CONFIG_FILES([ 50 | Makefile 51 | lib/Makefile 52 | cpp/Makefile 53 | ]) 54 | 55 | dnl create all the requested output files 56 | AC_OUTPUT 57 | -------------------------------------------------------------------------------- /cpp/Makefile.am: -------------------------------------------------------------------------------- 1 | # 2 | AM_CPPFLAGS = \ 3 | -I$(top_srcdir) \ 4 | -I$(BOOST_DIR) \ 5 | $(COMPILE_FLAGS) 6 | # 7 | AM_CXXFLAGS = \ 8 | $(COMPILE_FLAGS) 9 | LDADD = ../lib/libmat2cpp.a -llapack 10 | # 11 | noinst_PROGRAMS = mat2cpp_ok getstarted 12 | getstarted_SOURCES = getstarted.cpp 13 | mat2cpp_ok_SOURCES = \ 14 | add_ok.cpp \ 15 | cholesky_ok.cpp \ 16 | diag_prod_ok.cpp \ 17 | element_div_ok.cpp \ 18 | element_prod_ok.cpp \ 19 | element_unary_ok.cpp \ 20 | extend_ok.cpp \ 21 | mat2cpp_ok.cpp \ 22 | matrix_div_ok.cpp \ 23 | matrix_prod_ok.cpp \ 24 | max_ok.cpp \ 25 | min_ok.cpp \ 26 | mztuni_ok.cpp \ 27 | ones_ok.cpp \ 28 | rand_ok.cpp \ 29 | randn_ok.cpp \ 30 | scalar_ok.cpp \ 31 | scalar_div_ok.cpp \ 32 | scalar_prod_ok.cpp \ 33 | size_ok.cpp \ 34 | slice_ok.cpp \ 35 | sub_ok.cpp \ 36 | sum_ok.cpp \ 37 | transpose_ok.cpp \ 38 | zeros_ok.cpp 39 | # 40 | noinst_DATA = getstarted.out 41 | getstarted.out : getstarted$(EXEEXT) 42 | ./getstarted > getstarted.out 43 | -------------------------------------------------------------------------------- /cpp/add_ok.cpp: -------------------------------------------------------------------------------- 1 | # include 2 | # include 3 | bool add_ok(void) 4 | { bool ok = true; 5 | using namespace mat2cpp; 6 | 7 | size_t i, j, m(3), n(2); 8 | matrix x = rand(m, n); 9 | matrix y = rand(m, n); 10 | // ---------------------- 11 | matrix z = x + y; 12 | // ---------------------- 13 | ok &= (z.size1() == m); 14 | ok &= (z.size2() == n); 15 | for(i = 0; i < m; i++) 16 | { for(j = 0; j < n; j++) 17 | { double zij = x(i,j) + y(i,j); 18 | ok &= std::fabs(z(i, j) - zij) < 1e-10; 19 | } 20 | } 21 | return ok; 22 | } 23 | -------------------------------------------------------------------------------- /cpp/cholesky_ok.cpp: -------------------------------------------------------------------------------- 1 | # include 2 | # include 3 | bool cholesky_ok(void) 4 | { bool ok = true; 5 | using namespace mat2cpp; 6 | 7 | size_t i, j, m(3); 8 | matrix a = rand(m, m); 9 | matrix x = prod(trans(a), a); 10 | // --------------------------------------- 11 | size_t rank; 12 | matrix y = cholesky(x, rank); 13 | // --------------------------------------- 14 | ok &= (rank == m); 15 | ok &= (y.size1() == m); 16 | ok &= (y.size2() == m); 17 | matrix check = prod(trans(y), y); 18 | for(i = 0; i < m; i++) 19 | { for(j = 0; j < m; j++) 20 | ok &= std::fabs( check(i, j) - x(i,j) ) < 1e-10; 21 | } 22 | for(i = 0; i < m; i++) 23 | { for(j = 0; j < i; j++) 24 | ok &= (y(i, j) == 0.); 25 | } 26 | return ok; 27 | } 28 | -------------------------------------------------------------------------------- /cpp/diag_prod_ok.cpp: -------------------------------------------------------------------------------- 1 | # include 2 | bool diag_prod_ok(void) 3 | { bool ok = true; 4 | using namespace mat2cpp; 5 | 6 | size_t i, j, m(3), n(2); 7 | matrix u = rand(m, 1); 8 | matrix x = rand(1, m); 9 | matrix y = rand(m, n); 10 | // -------------------------------- 11 | matrix w = diag_prod(u, y); 12 | matrix z = diag_prod(x, y); 13 | // -------------------------------- 14 | ok &= (z.size1() == m); 15 | ok &= (z.size2() == n); 16 | for(i = 0; i < m; i++) 17 | { for(j = 0; j < n; j++) 18 | { double wij = u(i,0) * y(i,j); 19 | double zij = x(0,i) * y(i,j); 20 | ok &= std::fabs(w(i, j) - wij) < 1e-10; 21 | ok &= std::fabs(z(i, j) - zij) < 1e-10; 22 | } 23 | } 24 | return ok; 25 | } 26 | -------------------------------------------------------------------------------- /cpp/element_div_ok.cpp: -------------------------------------------------------------------------------- 1 | # include 2 | # include 3 | bool element_div_ok(void) 4 | { bool ok = true; 5 | using namespace mat2cpp; 6 | 7 | size_t i, j, m(3), n(2); 8 | matrix x = rand(m, n); 9 | matrix y = rand(m, n); 10 | // ---------------------------------- 11 | matrix z = element_div(x, y); 12 | // ---------------------------------- 13 | ok &= (z.size1() == m); 14 | ok &= (z.size2() == n); 15 | for(i = 0; i < m; i++) 16 | { for(j = 0; j < n; j++) 17 | { double zij = x(i,j) / y(i,j); 18 | ok &= std::fabs(z(i, j) - zij) < 1e-10; 19 | } 20 | } 21 | return ok; 22 | } 23 | -------------------------------------------------------------------------------- /cpp/element_prod_ok.cpp: -------------------------------------------------------------------------------- 1 | # include 2 | bool element_prod_ok(void) 3 | { bool ok = true; 4 | using namespace mat2cpp; 5 | 6 | size_t i, j, m(3), n(2); 7 | matrix x = rand(m, n); 8 | matrix y = rand(m, n); 9 | // ----------------------------------- 10 | matrix z = element_prod(x, y); 11 | // ----------------------------------- 12 | ok &= (z.size1() == m); 13 | ok &= (z.size2() == n); 14 | for(i = 0; i < m; i++) 15 | { for(j = 0; j < n; j++) 16 | { double zij = x(i,j) * y(i,j); 17 | ok &= std::fabs(z(i, j) - zij) < 1e-10; 18 | } 19 | } 20 | return ok; 21 | } 22 | -------------------------------------------------------------------------------- /cpp/element_unary_ok.cpp: -------------------------------------------------------------------------------- 1 | # include 2 | # include 3 | 4 | bool element_unary_ok(void) 5 | { bool ok = true; 6 | using namespace mat2cpp; 7 | double eps = 10. * std::numeric_limits::epsilon(); 8 | 9 | size_t i, j, m(2), n(3); 10 | matrix x = rand(m, n); 11 | // --------------------------- 12 | matrix abs_x = abs(x); 13 | matrix log_x = log(x); 14 | // --------------------------- 15 | for(j = 0; j < n; j++) 16 | { for(i = 0; i < m; i++) 17 | { ok &= std::fabs( abs_x(i, j) - std::fabs(x(i, j)) ) < eps; 18 | ok &= std::fabs( log_x(i, j) - std::log(x(i, j)) ) < eps; 19 | } 20 | } 21 | return ok; 22 | } 23 | -------------------------------------------------------------------------------- /cpp/extend_ok.cpp: -------------------------------------------------------------------------------- 1 | # include 2 | bool extend_ok(void) 3 | { using namespace mat2cpp; 4 | 5 | bool ok = true; 6 | size_t i, j, m(2), n(3); 7 | matrix x(m, n); 8 | for(i = 0; i < m; i++) 9 | { for(j = 0; j < n; j++) 10 | x(i, j) = double(i+j); 11 | } 12 | // ------------------------------- 13 | x.resize(m, n + 1); 14 | for(i = 0; i < m; i++) 15 | x(i, n) = double(i + n); 16 | // ------------------------------- 17 | ok &= (x.size1() == m); 18 | ok &= (x.size2() == n + 1); 19 | for(i = 0; i < m; i++) 20 | { for(j = 0; j < n; j++) 21 | ok &= (x(i, j) == double(i+j)); 22 | } 23 | return ok; 24 | } 25 | -------------------------------------------------------------------------------- /cpp/getstarted.cpp: -------------------------------------------------------------------------------- 1 | # include 2 | # include 3 | int main(void) 4 | { using namespace mat2cpp; 5 | 6 | double a_data[] = {1., 2., 3., 4.}; 7 | double b_data[] = {1., 2.}; 8 | matrix a(2, 2); 9 | matrix b(2, 1); 10 | size_t i, j; 11 | for(i = 0; i < 2; i++) 12 | { b(i, 0) = b_data[i]; 13 | for(j = 0; j < 2; j++) 14 | a(i, j) = a_data[i * 2 + j]; 15 | } 16 | size_t rank; 17 | matrix x = matrix_div(a, b, rank); 18 | std::cout << "x =" << x << std::endl; 19 | 20 | return 0; 21 | } 22 | 23 | 24 | -------------------------------------------------------------------------------- /cpp/mat2cpp_ok.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | $begin mat2cpp_ok.cpp$$ 3 | $spell 4 | hpp 5 | ctime 6 | ptr 7 | mztuni 8 | const 9 | randn 10 | cholesky 11 | cstdlib 12 | srand 13 | div 14 | bool 15 | cout 16 | endl 17 | iostream 18 | std 19 | C++ 20 | $$ 21 | 22 | $section Run C++ Version of Examples$$ 23 | 24 | $codep */ 25 | # include 26 | # include 27 | # include 28 | 29 | extern bool add_ok(void); 30 | extern bool cholesky_ok(void); 31 | extern bool diag_prod_ok(void); 32 | extern bool element_div_ok(void); 33 | extern bool element_prod_ok(void); 34 | extern bool element_unary_ok(void); 35 | extern bool extend_ok(void); 36 | extern bool matrix_div_ok(void); 37 | extern bool matrix_prod_ok(void); 38 | extern bool max_ok(void); 39 | extern bool min_ok(void); 40 | extern bool mztuni_ok(void); 41 | extern bool ones_ok(void); 42 | extern bool rand_ok(void); 43 | extern bool randn_ok(void); 44 | extern bool scalar_ok(void); 45 | extern bool scalar_div_ok(void); 46 | extern bool scalar_prod_ok(void); 47 | extern bool size_ok(void); 48 | extern bool slice_ok(void); 49 | extern bool sub_ok(void); 50 | extern bool sum_ok(void); 51 | extern bool transpose_ok(void); 52 | extern bool zeros_ok(void); 53 | 54 | bool run_test(bool test(void), const char *name) 55 | { bool ok = test(); 56 | if( ok ) 57 | std::cout << "ok: " << name << std::endl; 58 | else std::cout << "error: " << name << std::endl; 59 | return ok; 60 | } 61 | int main() 62 | { bool ok = true; 63 | 64 | // initialize the random number generator using the clock for a seed 65 | mztuni_seed(1); 66 | ok &= mztuni_seed(0) != 0; 67 | 68 | // run all the tests 69 | ok = ok & run_test(add_ok, "add_ok"); 70 | ok = ok & run_test(cholesky_ok, "cholesky_ok"); 71 | ok = ok & run_test(diag_prod_ok, "diag_prod_ok"); 72 | ok = ok & run_test(element_div_ok, "element_div_ok"); 73 | ok = ok & run_test(element_prod_ok, "element_prod_ok"); 74 | ok = ok & run_test(element_unary_ok, "element_unary_ok"); 75 | ok = ok & run_test(extend_ok, "extend_ok"); 76 | ok = ok & run_test(matrix_div_ok, "matrix_div_ok"); 77 | ok = ok & run_test(matrix_prod_ok, "matrix_prod_ok"); 78 | ok = ok & run_test(max_ok, "max_ok"); 79 | ok = ok & run_test(min_ok, "min_ok"); 80 | ok = ok & run_test(ones_ok, "ones_ok"); 81 | ok = ok & run_test(rand_ok, "rand_ok"); 82 | ok = ok & run_test(randn_ok,"randn_ok"); 83 | ok = ok & run_test(scalar_ok, "scalar_ok"); 84 | ok = ok & run_test(scalar_div_ok, "scalar_div_ok"); 85 | ok = ok & run_test(scalar_prod_ok, "scalar_prod_ok"); 86 | ok = ok & run_test(size_ok, "size_ok"); 87 | ok = ok & run_test(slice_ok, "slice_ok"); 88 | ok = ok & run_test(sub_ok, "sub_ok"); 89 | ok = ok & run_test(sum_ok, "sum_ok"); 90 | ok = ok & run_test(transpose_ok, "transpose_ok"); 91 | ok = ok & run_test(zeros_ok, "zeros_ok"); 92 | // 93 | // do this test last because it tests setting the seed 94 | if( ok ) 95 | ok = ok & run_test(mztuni_ok, "mztuni_ok"); 96 | std::cout << std::endl; 97 | if( ok ) 98 | std::cout << "All tests passed" << std::endl; 99 | else 100 | { std::cout << "At least one test failed" << std::endl; 101 | std::cout << "mztuni_seed = " << mztuni_seed(0) << std::endl; 102 | } 103 | int error = ! ok; 104 | return error; 105 | } 106 | /* $$ 107 | 108 | $end 109 | */ 110 | -------------------------------------------------------------------------------- /cpp/matrix_div_ok.cpp: -------------------------------------------------------------------------------- 1 | # include 2 | # include 3 | bool matrix_div_ok(void) 4 | { bool ok = true; 5 | using namespace mat2cpp; 6 | 7 | size_t i, j, m(3), n(2); 8 | matrix x = rand(m, m); 9 | matrix y = rand(m, n); 10 | // --------------------------------------- 11 | size_t rank; 12 | matrix z = matrix_div(x, y, rank); 13 | // --------------------------------------- 14 | ok &= (rank == m); 15 | ok &= (z.size1() == m); 16 | ok &= (z.size2() == n); 17 | matrix x_z = prod(x, z); 18 | for(i = 0; i < m; i++) 19 | { for(j = 0; j < n; j++) 20 | ok &= std::fabs( x_z(i,j) - y(i,j) ) < 1e-10; 21 | } 22 | return ok; 23 | } 24 | -------------------------------------------------------------------------------- /cpp/matrix_prod_ok.cpp: -------------------------------------------------------------------------------- 1 | # include 2 | # include 3 | bool matrix_prod_ok(void) 4 | { bool ok = true; 5 | using namespace mat2cpp; 6 | 7 | size_t i, j, k, m(3), n(2), p(4); 8 | matrix x = rand(m, n); 9 | matrix y = rand(n, p); 10 | // --------------------------- 11 | matrix z = prod(x, y); 12 | // --------------------------- 13 | ok &= (z.size1() == m); 14 | ok &= (z.size2() == p); 15 | for(i = 0; i < m; i++) 16 | { for(j = 0; j < p; j++) 17 | { double zij = 0.; 18 | for(k = 0; k < n; k++) 19 | zij += x(i,k) * y(k,j); 20 | ok &= std::fabs(z(i, j) - zij) < 1e-10; 21 | } 22 | } 23 | return ok; 24 | } 25 | -------------------------------------------------------------------------------- /cpp/max_ok.cpp: -------------------------------------------------------------------------------- 1 | # include 2 | bool max_ok(void) 3 | { bool ok = true; 4 | using namespace mat2cpp; 5 | 6 | size_t i, j, m(2), n(3); 7 | matrix x = rand(m, n); 8 | // --------------- 9 | double y = max(x); 10 | // --------------- 11 | bool less = true; 12 | for(i = 0; i < m; i++) 13 | { for(j = 0; j < n; j++) 14 | { ok &= x(i,j) <= y; 15 | less &= x(i,j) < y; 16 | } 17 | } 18 | ok &= ! less; 19 | return ok; 20 | } 21 | -------------------------------------------------------------------------------- /cpp/min_ok.cpp: -------------------------------------------------------------------------------- 1 | # include 2 | bool min_ok(void) 3 | { bool ok = true; 4 | using namespace mat2cpp; 5 | 6 | size_t i, j, m(2), n(3); 7 | matrix x = rand(m, n); 8 | // --------------- 9 | double y = min(x); 10 | // --------------- 11 | bool greater = true; 12 | for(i = 0; i < m; i++) 13 | { for(j = 0; j < n; j++) 14 | { ok &= x(i,j) >= y; 15 | greater &= x(i,j) > y; 16 | } 17 | } 18 | ok &= ! greater; 19 | return ok; 20 | } 21 | -------------------------------------------------------------------------------- /cpp/mztuni_ok.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | $begin mztuni_ok.cpp$$ 3 | $spell 4 | mztuni 5 | $$ 6 | 7 | $section mztuni: Example and Test$$ 8 | 9 | $code 10 | $verbatim%cpp/mztuni_ok.cpp%0%// BEGIN C++%// END C++%1%$$ 11 | $$ 12 | 13 | $end 14 | */ 15 | // BEGIN C++ 16 | # include 17 | # include 18 | 19 | bool mztuni_ok(void) 20 | { bool ok = true; 21 | // 22 | // seed used to create Table 4 in the article 23 | size_t i = 12; 24 | size_t j = 34; 25 | size_t k = 56; 26 | size_t l = 78; 27 | // 28 | // seed in format expected by mztuni 29 | size_t seed = 0; 30 | seed = 168 * seed + l - 1; 31 | seed = 168 * seed + k - 1; 32 | seed = 168 * seed + j - 1; 33 | seed = 168 * seed + i + 1; // note + 1 (not - 1) 34 | // 35 | // seed the random number generator 36 | mztuni_seed(seed); 37 | // 38 | // skip first 20,000 simulations 39 | for(size_t ii = 1; ii <= 20000; ii++) 40 | mztuni(); 41 | // 42 | // check the next 5 simulations using values in Table 4 of reference 43 | size_t table_4[] = { 44 | 6, 3, 11, 3, 0, 4, 0, 45 | 13, 8, 15, 11, 11, 14, 0, 46 | 6, 15, 0, 2, 3, 11, 0, 47 | 5, 14, 2, 14, 4, 8, 0, 48 | 7, 15, 7, 10, 12, 2, 0 49 | }; 50 | for(size_t ii = 0; ii < 5; ii++) 51 | { double x = mztuni(0); 52 | size_t factor = 1; 53 | for(size_t m = 0; m < 7; m++) 54 | { factor *= 16; 55 | size_t value = static_cast( x * factor ) % 16; 56 | size_t check = table_4[ii * 7 + m]; 57 | ok &= value == check; 58 | } 59 | } 60 | // check seed value 61 | ok &= mztuni_seed(0) == seed; 62 | // 63 | return ok; 64 | } 65 | // END C++ 66 | -------------------------------------------------------------------------------- /cpp/ones_ok.cpp: -------------------------------------------------------------------------------- 1 | # include 2 | bool ones_ok(void) 3 | { bool ok = true; 4 | using namespace mat2cpp; 5 | 6 | size_t i, j, m(2), n(3); 7 | // ------------------------------- 8 | scalar_matrix x(m, n, 1.); 9 | // ------------------------------- 10 | ok &= (x.size1() == m); 11 | ok &= (x.size2() == n); 12 | for(i = 0; i < m; i++) 13 | { for(j = 0; j < n; j++) 14 | ok &= (x(i,j) == 1.); 15 | } 16 | return ok; 17 | } 18 | -------------------------------------------------------------------------------- /cpp/rand_ok.cpp: -------------------------------------------------------------------------------- 1 | # include 2 | # include 3 | 4 | bool rand_ok(void) 5 | { bool ok = true; 6 | using namespace mat2cpp; 7 | 8 | size_t i, j, m(2), n(3); 9 | // --------------------------- 10 | matrix x = rand(m, n); 11 | // --------------------------- 12 | ok &= (x.size1() == m); 13 | ok &= (x.size2() == n); 14 | for(i = 0; i < m; i++) 15 | { for(j = 0; j < n; j++) 16 | { ok &= (0. < x(i, j) ); // probability of limit is zero 17 | ok &= (x(i, j) < 1.); 18 | } 19 | } 20 | for(j = 0; j < n; j++) 21 | ok &= (x(0, j) != x(1, j)); // probability zero 22 | return ok; 23 | } 24 | -------------------------------------------------------------------------------- /cpp/randn_ok.cpp: -------------------------------------------------------------------------------- 1 | # include 2 | # include 3 | # include 4 | 5 | bool randn_ok(void) 6 | { bool ok = true; 7 | using namespace mat2cpp; 8 | 9 | size_t i, j, m(20), n(21); 10 | // --------------------------- 11 | matrix x = randn(m, n); 12 | // --------------------------- 13 | ok &= (x.size1() == m); 14 | ok &= (x.size2() == n); 15 | double sum = 0.; 16 | double sumsq = 0.; 17 | for(i = 0; i < m; i++) 18 | { for(j = 0; j < n; j++) 19 | { sum += x(i, j); 20 | sumsq += x(i, j) * x(i, j); 21 | } 22 | } 23 | double N = double( m * n ); 24 | double avg = sum / N; 25 | double var = sumsq / N - avg * avg; 26 | 27 | // The variance of N independent standard normals is N 28 | // and variance of avg in 1/N. 29 | ok &= std::fabs(avg) < 3. / std::sqrt(N); 30 | 31 | // The variance of N independent chi-squares is 2 * N 32 | // and variance of sumsq is 2/N 33 | ok &= std::fabs(var - 1.) < 3. * std::sqrt( 2. / N ); 34 | return ok; 35 | } 36 | -------------------------------------------------------------------------------- /cpp/scalar_div_ok.cpp: -------------------------------------------------------------------------------- 1 | # include 2 | # include 3 | bool scalar_div_ok(void) 4 | { bool ok = true; 5 | using namespace mat2cpp; 6 | 7 | size_t i, j, m(3), n(2); 8 | matrix x = rand(m, n); 9 | double y = std::rand() / double(RAND_MAX); 10 | // ---------------------- 11 | matrix z = x / y; 12 | z /= y; 13 | // ---------------------- 14 | ok &= (z.size1() == m); 15 | ok &= (z.size2() == n); 16 | for(i = 0; i < m; i++) 17 | { for(j = 0; j < n; j++) 18 | { double zij = (x(i,j) / y) / y; 19 | ok &= std::fabs(z(i, j) - zij) < 1e-10; 20 | } 21 | } 22 | return ok; 23 | } 24 | -------------------------------------------------------------------------------- /cpp/scalar_ok.cpp: -------------------------------------------------------------------------------- 1 | # include 2 | # include 3 | bool scalar_ok(void) 4 | { bool ok = true; 5 | using namespace mat2cpp; 6 | 7 | size_t i, j, m(2), n(3); 8 | matrix u = rand(m, n); 9 | matrix v = u; 10 | matrix x(1, 1); 11 | x(0, 0) = 5.; 12 | // ------------ 13 | v *= scalar(x); 14 | // ------------ 15 | for(i = 0; i < m; i++) 16 | { for(j = 0; j < n; j++) 17 | ok &= std::fabs( v(i,j) - u(i,j) * x(0,0)) <= 1e-10; 18 | } 19 | return ok; 20 | } 21 | -------------------------------------------------------------------------------- /cpp/scalar_prod_ok.cpp: -------------------------------------------------------------------------------- 1 | # include 2 | # include 3 | bool scalar_prod_ok(void) 4 | { bool ok = true; 5 | using namespace mat2cpp; 6 | 7 | size_t i, j, m(3), n(2); 8 | matrix x = rand(m, n); 9 | double y = std::rand() / double(RAND_MAX); 10 | // ---------------------- 11 | matrix z = x * y; 12 | z *= y; 13 | // ---------------------- 14 | ok &= (z.size1() == m); 15 | ok &= (z.size2() == n); 16 | for(i = 0; i < m; i++) 17 | { for(j = 0; j < n; j++) 18 | { double zij = (x(i,j) * y) * y; 19 | ok &= std::fabs(z(i, j) - zij) < 1e-10; 20 | } 21 | } 22 | return ok; 23 | } 24 | -------------------------------------------------------------------------------- /cpp/size_ok.cpp: -------------------------------------------------------------------------------- 1 | # include 2 | bool size_ok(void) 3 | { bool ok = true; 4 | using namespace mat2cpp; 5 | 6 | size_t m(2), n(3); 7 | matrix x(m, n); 8 | // -------------------- 9 | ok &= (x.size1() == m); 10 | ok &= (x.size2() == n); 11 | // -------------------- 12 | return ok; 13 | } 14 | -------------------------------------------------------------------------------- /cpp/slice_ok.cpp: -------------------------------------------------------------------------------- 1 | # include 2 | bool slice_ok(void) 3 | { bool ok = true; 4 | using namespace mat2cpp; 5 | 6 | size_t i, j, k, m(3), n(4); 7 | matrix x(m, n); 8 | for(i = 0; i < m; i++) 9 | { for(j = 0; j < n; j++) 10 | x(i, j) = i * j; 11 | } 12 | zero_matrix y(2, 2); 13 | // ------------------------- 14 | slice r = slice(0, 2, 2); 15 | slice c = slice(0, 2, 2); 16 | matrix_slice_double (x, r, c) = y; 17 | // ------------------------- 18 | ok &= (x.size1() == m); 19 | ok &= (x.size2() == n); 20 | for(i = 0; i < m; i++) 21 | { for(j = 0; j < n; j++) 22 | { bool row_match = false; 23 | for(k = 0; k < r.size(); k++) 24 | row_match |= (i == r.start() + r.stride() * k); 25 | bool col_match = false; 26 | for(k = 0; k < c.size(); k++) 27 | col_match |= (j == c.start() + c.stride() * k); 28 | if( row_match & col_match ) 29 | ok &= (x(i,j) == 0.); 30 | else ok &= (x(i,j) == double(i * j)); 31 | } 32 | } 33 | return ok; 34 | } 35 | -------------------------------------------------------------------------------- /cpp/sub_ok.cpp: -------------------------------------------------------------------------------- 1 | # include 2 | # include 3 | bool sub_ok(void) 4 | { bool ok = true; 5 | using namespace mat2cpp; 6 | 7 | size_t i, j, m(3), n(2); 8 | matrix x = rand(m, n); 9 | matrix y = rand(m, n); 10 | // ---------------------- 11 | matrix z = x - y; 12 | // ---------------------- 13 | ok &= (z.size1() == m); 14 | ok &= (z.size2() == n); 15 | for(i = 0; i < m; i++) 16 | { for(j = 0; j < n; j++) 17 | { double zij = x(i,j) - y(i,j); 18 | ok &= std::fabs(z(i, j) - zij) < 1e-10; 19 | } 20 | } 21 | return ok; 22 | } 23 | -------------------------------------------------------------------------------- /cpp/sum_ok.cpp: -------------------------------------------------------------------------------- 1 | # include 2 | bool sum_ok(void) 3 | { bool ok = true; 4 | using namespace mat2cpp; 5 | 6 | size_t i, j, m(2), n(3); 7 | matrix x = rand(m, n); 8 | // --------------- 9 | double y = sum(x); 10 | // --------------- 11 | double check = 0.; 12 | for(i = 0; i < m; i++) 13 | { for(j = 0; j < n; j++) 14 | check += x(i,j); 15 | } 16 | ok &= std::fabs(check - y) < 1e-10; 17 | return ok; 18 | } 19 | -------------------------------------------------------------------------------- /cpp/transpose_ok.cpp: -------------------------------------------------------------------------------- 1 | # include 2 | bool transpose_ok(void) 3 | { bool ok = true; 4 | using namespace mat2cpp; 5 | 6 | size_t i, j, m(2), n(3); 7 | matrix x = rand(m, n); 8 | // ------------------------- 9 | matrix y = trans(x); 10 | // ------------------------- 11 | ok &= (y.size1() == n); 12 | ok &= (y.size2() == m); 13 | for(i = 0; i < m; i++) 14 | { for(j = 0; j < n; j++) 15 | ok &= y(j,i) == x(i,j); 16 | } 17 | return ok; 18 | } 19 | -------------------------------------------------------------------------------- /cpp/zeros_ok.cpp: -------------------------------------------------------------------------------- 1 | # include 2 | bool zeros_ok(void) 3 | { bool ok = true; 4 | using namespace mat2cpp; 5 | 6 | size_t i, j, m(2), n(3); 7 | // ------------------------- 8 | zero_matrix x(m, n); 9 | // ------------------------- 10 | ok &= (x.size1() == m); 11 | ok &= (x.size2() == n); 12 | for(i = 0; i < m; i++) 13 | { for(j = 0; j < n; j++) 14 | ok &= (x(i,j) == 0.); 15 | } 16 | return ok; 17 | } 18 | -------------------------------------------------------------------------------- /doc.omh: -------------------------------------------------------------------------------- 1 | $begin mat2cpp$$ 2 | $escape $$ 3 | $spell 4 | dir 5 | llapack 6 | lmat2cpp 7 | Matlab 8 | Ublas 9 | namespace 10 | mat2cpp 11 | hpp 12 | xvzf 13 | gz 14 | su 15 | getstarted 16 | https 17 | github 18 | bradbell 19 | mkdir 20 | $$ 21 | 22 | $section Example Conversion of Matlab or Octave to C++ using Ublas$$ 23 | 24 | $head Purpose$$ 25 | The purpose of this package is to provide an example of converting 26 | $href%http://www.mathworks.com/%Matlab%$$ 27 | or 28 | $href%http://www.gnu.org/software/octave/%Octave%$$ 29 | to C++ 30 | with the aid of 31 | $href%http://www.boost.org/libs/numeric/ublas/doc/overview.htm%ublas%$$. 32 | 33 | $head Test and Install$$ 34 | $list number$$ 35 | Download the source code 36 | $codep 37 | git clone https://github.com/bradbell/mat2cpp.git mat2cpp.git 38 | $$ 39 | 40 | $lnext 41 | Build the library and test program with the command 42 | $codei% 43 | cd mat2cpp.git 44 | mkdir build 45 | cd build 46 | ../configure --prefix=%prefix_dir% BOOST_DIR=%boost_dir% 47 | make 48 | %$$ 49 | where $icode prefix$$ is the prefix for this installation and 50 | $icode boost_dir$$ prefix for the existing $code boost$$ install 51 | (check that the directory $icode%boost_dir%/include/boost%$$ exists). 52 | 53 | 54 | $lnext 55 | Test the installation of the Matlab and Octave files with the commands 56 | $codep 57 | cd mat 58 | octave mat2cpp_ok 59 | cd .. 60 | $$ 61 | The $code mat2cpp_ok$$ program should have responded 62 | that all the octave tests passed 63 | (you can also run the program in Matlab). 64 | 65 | $lnext 66 | Test the installation of the C++ files with the commands 67 | $codep 68 | build/cpp/mat2cpp_ok 69 | $$ 70 | The $code mat2cpp_ok$$ program should have responded 71 | that all the tests passed. 72 | (you can also run the program in Matlab). 73 | 74 | 75 | $lnext 76 | Install the $code mat2cpp$$ library and include file in the standard locations 77 | with the following commands 78 | $codep 79 | cd build 80 | make install 81 | cd .. 82 | $$ 83 | 84 | $lnext 85 | Compile and run the $cref/getstarted/$$ example with the following commands: 86 | $codei% 87 | cd build 88 | g++ ../cpp/getstarted.cpp -o getstarted \ 89 | -I%boost_dir%/include \ 90 | -I%prefix_dir%/include \ 91 | -L%prefix_dir%/lib \ 92 | -lmat2cpp -llapack 93 | ./getstarted 94 | %$$ 95 | $lend 96 | 97 | 98 | $head mat2cpp.hpp$$ 99 | $index library, mat2cpp$$ 100 | $index link, mat2cpp$$ 101 | The include file $cref/mat2cpp.hpp/$$ includes the 102 | $href%http://www.boost.org/libs/numeric/ublas/doc/index.htm%C++%$$ 103 | header file 104 | $href%http://www.boost.org/libs/numeric/ublas/doc/matrix.htm%matrix.hpp%$$. 105 | It then declares some library routines for that aid in converting 106 | from the Matlab or Octave languages to C++. 107 | 108 | 109 | $contents% 110 | COPYING% 111 | omh/getstarted.omh% 112 | omh/operation.omh% 113 | mat2cpp.hpp% 114 | mat/mat2cpp_ok.m% 115 | cpp/mat2cpp_ok.cpp% 116 | NEWS% 117 | omh/library.omh% 118 | omh/mztuni.omh 119 | %$$ 120 | 121 | $end 122 | -------------------------------------------------------------------------------- /gh_pages.sh: -------------------------------------------------------------------------------- 1 | #! /bin/bash -e 2 | # ----------------------------------------------------------------------------- 3 | if [ "$0" != './gh_pages.sh' ] 4 | then 5 | echo './gh_pages.sh: must be run from top source directory' 6 | exit 1 7 | fi 8 | # ----------------------------------------------------------------------------- 9 | package='mat2cpp' 10 | version=`grep '^AC_INIT(' configure.ac | \ 11 | sed -e "s|AC_INIT( *$package *,||" -e 's|,.*||'` 12 | tmp_doc='build/tmp/doc' 13 | # 14 | # build doc directory 15 | echo "./run_omhelp.sh" 16 | ./run_omhelp.sh 17 | # 18 | # move doc to tmp_doc 19 | if [ ! -e build/tmp ] 20 | then 21 | mkdir -p build/tmp 22 | fi 23 | if [ -e $tmp_doc ] 24 | then 25 | rm -r $tmp_doc 26 | fi 27 | mv doc $tmp_doc 28 | # 29 | # checkout gh-pages branch 30 | echo "git checkout gh-pages" 31 | git checkout gh-pages 32 | # 33 | # remove files that are no longer in doc directory 34 | list=`ls -a doc` 35 | for file in $list 36 | do 37 | if [ ! -e "$tmp_doc/$file" ] 38 | then 39 | echo "git rm doc/$file" 40 | git rm doc/$file 41 | fi 42 | done 43 | # 44 | # copy new version of files 45 | list=`ls $tmp_doc` 46 | for file in $list 47 | do 48 | if [ ! -e doc/$file ] 49 | then 50 | echo "git add doc/$file" 51 | fi 52 | cp $tmp_doc/$file doc/$file 53 | done 54 | # 55 | # stage all the changes 56 | git add doc/* 57 | # ----------------------------------------------------------------------------- 58 | list=`git status -s` 59 | if [ "$list" != '' ] 60 | then 61 | echo 'Currently in the gh-pages branch. The following will commit changes' 62 | echo " git commit -m 'update gh-pages to version $version'" 63 | fi 64 | # ----------------------------------------------------------------------------- 65 | echo 'gh_pages.sh: OK' 66 | exit 0 67 | -------------------------------------------------------------------------------- /lib/Makefile.am: -------------------------------------------------------------------------------- 1 | # 2 | # top_srcdir has mat2cpp include files 3 | # top_builddir has the config.h file generated by autoconf 4 | AM_CPPFLAGS = -I$(top_srcdir) -I$(top_builddir) -I$(BOOST_DIR) 5 | AM_CXXFLAGS = -g 6 | # 7 | lib_LIBRARIES = libmat2cpp.a 8 | libmat2cpp_a_SOURCES = \ 9 | c2dpotrf.f \ 10 | cholesky.cpp \ 11 | diag_prod.cpp \ 12 | element_unary.cpp \ 13 | matrix_div.cpp \ 14 | max.cpp \ 15 | min.cpp \ 16 | scalar.cpp \ 17 | sum.cpp \ 18 | rand.cpp \ 19 | mztuni.cpp \ 20 | randn.cpp 21 | -------------------------------------------------------------------------------- /lib/c2dpotrf.f: -------------------------------------------------------------------------------- 1 | c standarad compliant wrapper to pass a char argument from c to fortran 2 | c (except assume fortran compiler supports >= 8 character identifiers) 3 | subroutine c2dpotrf(c_uplo, n, a, lda, info) 4 | integer c_uplo, info, lda, n 5 | double precision a(lda, *) 6 | call dpotrf(char(c_uplo), n, a, lda, info) 7 | return 8 | end 9 | -------------------------------------------------------------------------------- /lib/cholesky.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | $begin cholesky.cpp$$ 3 | $spell 4 | config.h 5 | cholesky 6 | uplo 7 | dpotrf 8 | cpp 9 | C++ 10 | Lapack 11 | dgesv 12 | nrhs 13 | lda 14 | ipv 15 | ldb 16 | fm 17 | fn 18 | cassert 19 | div 20 | hpp 21 | namespace 22 | const 23 | Elementwise 24 | Matlab or Octave 25 | C++ 26 | $$ 27 | 28 | $section Cholesky Factor a Matrix Source Code$$ 29 | 30 | $codep */ 31 | # include 32 | # include 33 | # include 34 | 35 | # define C2DPOTRF_F77 F77_FUNC_(c2dpotrf, C2DPOTRF) 36 | 37 | namespace mat2cpp { 38 | // Linkage for Fortran version of the Lapack routine dpotrf 39 | extern "C" fortran_subroutine C2DPOTRF_F77( 40 | fortran_integer *c_uplo, 41 | fortran_integer *n, 42 | fortran_double *a, 43 | fortran_integer *lda, 44 | fortran_integer *info 45 | ); 46 | matrix cholesky 47 | (const matrix &x, size_t &rank) 48 | { assert( x.size1() == x.size2() ); 49 | size_t i, j; 50 | 51 | size_t m = x.size1(); 52 | fortran_integer c_uplo = fortran_integer('U'); 53 | fortran_double *a = new fortran_double [m * m]; 54 | for(i = 0; i < m; i++) 55 | { for(j = 0; j < m; j++) 56 | a[ i + j * m ] = x(i, j); 57 | } 58 | fortran_integer lda = fortran_integer(m); 59 | fortran_integer fm = fortran_integer(m); 60 | fortran_integer info; 61 | C2DPOTRF_F77 (&c_uplo, &fm, a, &lda, &info); 62 | 63 | assert( info >= 0 ); 64 | if( info == 0 ) 65 | rank = m; 66 | else rank = size_t(info - 1); 67 | 68 | matrix z(m, m); 69 | for(i = 0; i < m; i++) 70 | { for(j = i; j < m; j++) 71 | z(i, j) = a[ i + j * m ]; 72 | for(j = 0; j < i; j++) 73 | z(i, j) = 0.; 74 | } 75 | delete [] a; 76 | return z; 77 | } 78 | 79 | } 80 | /* $$ 81 | $end 82 | */ 83 | -------------------------------------------------------------------------------- /lib/diag_prod.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | $begin diag_prod.cpp$$ 3 | $spell 4 | ublas 5 | cpp 6 | cassert 7 | hpp 8 | namespace 9 | const 10 | Matlab or Octave 11 | C++ 12 | $$ 13 | 14 | $section Diagonal Matrix Product with General Matrix Source Code$$ 15 | 16 | $codep */ 17 | # include 18 | # include 19 | namespace mat2cpp { 20 | matrix diag_prod 21 | (const matrix &x, const matrix &y) 22 | { size_t i, j, m(y.size1()), n(y.size2()); 23 | matrix z(m, n); 24 | if( x.size1() == 1 ) 25 | { // check that dimensions match 26 | assert( x.size2() == m ); 27 | for(i = 0; i < m; i++) 28 | { for(j = 0; j < n; j++) 29 | z(i, j) = x(0, i) * y(i, j); 30 | } 31 | } 32 | else if( x.size2() == 1 ) 33 | { // check that dimensions match 34 | assert( x.size1() == m ); 35 | for(i = 0; i < m; i++) 36 | { for(j = 0; j < n; j++) 37 | z(i, j) = x(i, 0) * y(i, j); 38 | } 39 | } 40 | else 41 | { // this is a diag_prod usage error 42 | assert(0); 43 | } 44 | return z; 45 | } 46 | } 47 | /* $$ 48 | $end 49 | */ 50 | -------------------------------------------------------------------------------- /lib/element_unary.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | $begin element_unary.cpp$$ 3 | $spell 4 | elementwise 5 | fabs 6 | undef 7 | cmath 8 | hpp 9 | namespace 10 | const 11 | std 12 | mat2cpp 13 | $$ 14 | 15 | $section Elementwise Unary Functions Source Code$$ 16 | 17 | $codep */ 18 | # include 19 | # include 20 | 21 | # define MAT2CPP_ELEMENT_UNARY(mat_f, std_f) \ 22 | matrix mat_f \ 23 | (const matrix &x) \ 24 | { size_t i, j, m(x.size1()), n(x.size2()); \ 25 | matrix y(m, n); \ 26 | for(i = 0; i < m; i++) \ 27 | { for(j = 0; j < n; j++) \ 28 | y(i, j) = std::std_f(x(i, j)); \ 29 | } \ 30 | return y; \ 31 | } 32 | 33 | namespace mat2cpp { 34 | MAT2CPP_ELEMENT_UNARY(abs, fabs) 35 | MAT2CPP_ELEMENT_UNARY(log, log) 36 | } 37 | 38 | # undef MAT2CPP_ELEMENT_UNARY 39 | /* $$ 40 | $end 41 | */ 42 | -------------------------------------------------------------------------------- /lib/matrix_div.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | $begin matrix_div.cpp$$ 3 | $spell 4 | config.h 5 | cpp 6 | C++ 7 | Lapack 8 | dgesv 9 | nrhs 10 | lda 11 | ipv 12 | ldb 13 | fm 14 | fn 15 | cassert 16 | div 17 | hpp 18 | namespace 19 | const 20 | Elementwise 21 | Matlab or Octave 22 | C++ 23 | $$ 24 | 25 | $section Matrix Division Source Code$$ 26 | 27 | $codep */ 28 | # include 29 | # include 30 | # include 31 | 32 | # define DGESV_F77 F77_FUNC_(dgesv, DGESV) 33 | 34 | namespace mat2cpp { 35 | // Linkage for Fortran version of the Lapack routine dgesv 36 | extern "C" fortran_subroutine DGESV_F77( 37 | fortran_integer *n, 38 | fortran_integer *nrhs, 39 | fortran_double *a, 40 | fortran_integer *lda, 41 | fortran_integer *ipv, 42 | fortran_double *b, 43 | fortran_integer *ldb, 44 | fortran_integer *info 45 | ); 46 | matrix matrix_div 47 | (const matrix &x, const matrix &y, size_t &rank) 48 | { assert( x.size1() == x.size2() ); 49 | assert( x.size2() == y.size1() ); 50 | 51 | size_t i, j; 52 | size_t m = y.size1(); 53 | size_t n = y.size2(); 54 | fortran_double *a = new fortran_double [m * m]; 55 | fortran_double *b = new fortran_double [m * n]; 56 | fortran_integer *ipv = new fortran_integer [m]; 57 | for(i = 0; i < m; i++) 58 | { for(j = 0; j < m; j++) 59 | a[ i + j * m ] = x(i, j); 60 | for(j = 0; j < n; j++) 61 | b[ i + j * m ] = y(i, j); 62 | } 63 | fortran_integer lda = fortran_integer(m); 64 | fortran_integer ldb = fortran_integer(m); 65 | fortran_integer fm = fortran_integer(m); 66 | fortran_integer fn = fortran_integer(n); 67 | fortran_integer info; 68 | DGESV_F77(&fm, &fn, a, &lda, ipv, b, &ldb, &info); 69 | 70 | assert( info >= 0 ); 71 | if( info == 0 ) 72 | rank = m; 73 | else rank = size_t(info - 1); 74 | 75 | matrix z(m, n); 76 | for(i = 0; i < m; i++) 77 | { for(j = 0; j < n; j++) 78 | z(i, j) = b[ i + j * m ]; 79 | } 80 | delete [] ipv; 81 | delete [] b; 82 | delete [] a; 83 | return z; 84 | } 85 | 86 | } 87 | /* $$ 88 | $end 89 | */ 90 | -------------------------------------------------------------------------------- /lib/max.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | $begin max.cpp$$ 3 | $spell 4 | Matlab 5 | cassert 6 | hpp 7 | namespace 8 | const 9 | std 10 | mat2cpp 11 | $$ 12 | 13 | $section Maximum Element of a Matrix Source code$$ 14 | 15 | $codep */ 16 | # include 17 | # include 18 | namespace mat2cpp { 19 | double max 20 | (const matrix &x) 21 | { assert( x.size1() * x.size2() > 0 ); 22 | double y = x(0, 0); 23 | size_t m = x.size1(); 24 | size_t n = x.size2(); 25 | size_t i, j; 26 | for(i = 0; i < m; i++) 27 | { for(j = 0; j < n; j++) 28 | y = std::max(y, x(i, j)); 29 | } 30 | return y; 31 | } 32 | } 33 | /* $$ 34 | $end 35 | */ 36 | -------------------------------------------------------------------------------- /lib/min.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | $begin min.cpp$$ 3 | $spell 4 | cassert 5 | hpp 6 | namespace 7 | const 8 | std 9 | mat2cpp 10 | $$ 11 | 12 | $section Minimum Element of a Matrix Source Code$$ 13 | 14 | $codep */ 15 | # include 16 | # include 17 | namespace mat2cpp { 18 | double min 19 | (const matrix &x) 20 | { assert( x.size1() * x.size2() > 0 ); 21 | double y = x(0, 0); 22 | size_t m = x.size1(); 23 | size_t n = x.size2(); 24 | size_t i, j; 25 | for(i = 0; i < m; i++) 26 | { for(j = 0; j < n; j++) 27 | y = std::min(y, x(i, j)); 28 | } 29 | return y; 30 | } 31 | } 32 | /* $$ 33 | $end 34 | */ 35 | -------------------------------------------------------------------------------- /lib/mztuni.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | ----------------------------------------------------------------------------- 3 | $begin mztuni.cpp$$ 4 | $spell 5 | ctime 6 | std 7 | hpp 8 | ptr 9 | seedm1 10 | stmp 11 | Marsaglia 12 | Zaman 13 | Tsang 14 | cstddef 15 | cassert 16 | cm 17 | ip 18 | jp 19 | jj 20 | uni 21 | mztuni 22 | $$ 23 | 24 | $section C++ Marsaglia Zaman Tsang Uniform Random Number Source Code$$ 25 | 26 | $codep 27 | */ 28 | # include // for size_t 29 | # include // for assert 30 | # include // for std::time and std::time_t 31 | # include // routines implemented here 32 | // 33 | size_t mztuni_seed(size_t seed) 34 | { static size_t previous_seed = 0; 35 | if( seed == 0 ) 36 | return previous_seed; 37 | // 38 | if( seed == 1 ) 39 | { // initialize the random number generator using the clock for a seed 40 | std::time_t* null_ptr = 0; 41 | std::time_t seconds = std::time(null_ptr); 42 | seed = static_cast( seconds ); 43 | seed = seed % 796594175 + 3; 44 | } 45 | mztuni(seed - 1); 46 | previous_seed = seed; 47 | // 48 | return previous_seed; 49 | } 50 | double mztuni(size_t seedm1) 51 | { assert( seedm1 <= 796594176 ); 52 | 53 | // define local mod function 54 | class mod_class { 55 | public: 56 | size_t operator()(size_t i, size_t j) 57 | { return i % j; } 58 | }; 59 | mod_class mod; 60 | // 61 | // static data that persists between function calls 62 | static double u[98], c, cd, cm; 63 | static size_t ip, jp; 64 | // 65 | if( seedm1 > 0 ) 66 | { size_t i, j, k, l, m; 67 | size_t stmp = seedm1; 68 | i = mod(stmp, 168); 69 | stmp = stmp / 168; 70 | j = mod(stmp, 168) + 1; 71 | stmp = stmp / 168; 72 | k = mod(stmp, 168) + 1; 73 | stmp = stmp / 168; 74 | l = mod(stmp, 168) + 1; 75 | assert( i != 1 || j != 1 || k != 1 || l != 1 ); 76 | for(size_t ii = 1; ii <=97; ii++) 77 | { double s = 0.0; 78 | double t = 0.5; 79 | for(size_t jj = 1; jj <= 24; jj++) 80 | { m = mod( mod(i * j, 179) * k, 179 ); 81 | i = j; 82 | j = k; 83 | k = m; 84 | l = mod(53 * l + 1, 169); 85 | if( mod(l * m, 64) >= 32) 86 | s = s + t; 87 | t = 0.5 * t; 88 | } 89 | u[ii] = s; 90 | } 91 | // There is a typo in the paper, where the fortran version has 92 | // CD = 7654321. / 167777216.; 93 | c = 362436. / 16777216.; 94 | cd = 7654321. / 16777216.; 95 | cm = 16777213. / 16777216.; 96 | ip = 97; 97 | jp = 33; 98 | // 99 | return 0.0; 100 | } 101 | double uni = u[ip] - u[jp]; 102 | if(uni < 0.0) 103 | uni = uni + 1.0; 104 | u[ip] = uni; 105 | // 106 | ip = ip - 1; 107 | if(ip == 0) 108 | ip = 97; 109 | // 110 | jp = jp - 1; 111 | if(jp == 0) 112 | jp = 97; 113 | // 114 | c = c - cd; 115 | if(c < 0.0) 116 | c = c + cm; 117 | // 118 | uni = uni - c; 119 | if(uni < 0.0) 120 | uni = uni + 1.0; 121 | return uni; 122 | } 123 | /* $$ 124 | $end 125 | */ 126 | -------------------------------------------------------------------------------- /lib/rand.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | $begin rand.cpp$$ 3 | $spell 4 | Matlab 5 | srand 6 | cstdlib 7 | hpp 8 | stdlib 9 | namespace 10 | std 11 | mat2cpp 12 | bool 13 | mztuni 14 | $$ 15 | 16 | $section Uniform Random Matrix Source Code$$ 17 | 18 | $codep */ 19 | # include 20 | # include 21 | # include 22 | 23 | namespace mat2cpp { 24 | matrix rand(size_t m, size_t n) 25 | { matrix x(m, n); 26 | size_t i, j; 27 | double rand_max = double(RAND_MAX); 28 | bool use_mztuni = mztuni_seed(0) != 0; 29 | // 30 | for(i = 0; i < m; i++) 31 | { for(j = 0; j < n; j++) 32 | if( use_mztuni ) 33 | x(i, j) = mztuni(); 34 | else 35 | x(i, j) = double(std::rand()) / rand_max; 36 | } 37 | return x; 38 | } 39 | } 40 | /* $$ 41 | $end 42 | */ 43 | -------------------------------------------------------------------------------- /lib/randn.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | $begin randn.cpp$$ 3 | $spell 4 | randn 5 | cmath 6 | ed 7 | Merran Evans, Nicholas Hastings, and Brian Peacock 8 | atan 9 | sqrt 10 | cos 11 | Matlab 12 | srand 13 | cstdlib 14 | hpp 15 | stdlib 16 | namespace 17 | std 18 | mat2cpp 19 | $$ 20 | 21 | $section Normal Random Matrix Source Code$$ 22 | 23 | $codep */ 24 | # include 25 | # include 26 | # include 27 | 28 | namespace mat2cpp { 29 | matrix randn(size_t m, size_t n) 30 | { // use formula 30.3 of Statistical Distributions (3rd ed) 31 | // Merran Evans, Nicholas Hastings, and Brian Peacock 32 | matrix u = rand(m * n + 1 , 1); 33 | matrix x(m, n); 34 | size_t i, j, k; 35 | double pi = 4. * std::atan(1.); 36 | double square, amp, angle; 37 | k = 0; 38 | for(i = 0; i < m; i++) 39 | { for(j = 0; j < n; j++) 40 | { if( k % 2 == 0 ) 41 | { square = - 2. * std::log( u(k, 0) ); 42 | if( square < 0. ) 43 | square = 0.; 44 | amp = sqrt(square); 45 | angle = 2. * pi * u(k+1, 0); 46 | x(i, j) = amp * std::sin( angle ); 47 | } 48 | else x(i, j) = amp * std::cos( angle ); 49 | k++; 50 | } 51 | } 52 | return x; 53 | } 54 | } 55 | /* $$ 56 | $end 57 | */ 58 | -------------------------------------------------------------------------------- /lib/scalar.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | $begin scalar.cpp$$ 3 | $spell 4 | Matlab 5 | cassert 6 | xj 7 | iterator 8 | hpp 9 | namespace 10 | const 11 | std 12 | vj 13 | mat2cpp 14 | $$ 15 | 16 | $section Convert Matrix to Scalar Source Code$$ 17 | 18 | $codep */ 19 | # include 20 | # include 21 | namespace mat2cpp { 22 | double scalar 23 | (const matrix &x) 24 | { assert( x.size1() * x.size2() == 1 ); 25 | double y = x(0, 0); 26 | return y; 27 | } 28 | } 29 | /* $$ 30 | $end 31 | */ 32 | -------------------------------------------------------------------------------- /lib/sum.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | $begin sum.cpp$$ 3 | $spell 4 | Matlab 5 | cassert 6 | hpp 7 | namespace 8 | const 9 | std 10 | mat2cpp 11 | $$ 12 | 13 | $section Sum The Elements of a Matlab Source Code$$ 14 | 15 | $codep */ 16 | # include 17 | # include 18 | namespace mat2cpp { 19 | double sum 20 | (const matrix &x) 21 | { double y = 0.; 22 | size_t m = x.size1(); 23 | size_t n = x.size2(); 24 | size_t i, j; 25 | for(i = 0; i < m; i++) 26 | { for(j = 0; j < n; j++) 27 | y += x(i, j); 28 | } 29 | return y; 30 | } 31 | } 32 | /* $$ 33 | $end 34 | */ 35 | -------------------------------------------------------------------------------- /mat/add_ok.m: -------------------------------------------------------------------------------- 1 | function [ok] = add_ok() 2 | ok = true; 3 | m = 3; 4 | n = 2; 5 | x = rand(m, n); 6 | y = rand(m, n); 7 | % ------------- 8 | z = x + y; 9 | % ------------- 10 | [m, n] = size(z); 11 | ok = ok & (m == 3); 12 | ok = ok & (n == 2); 13 | for i = [1 : m] 14 | for j = [1 : n] 15 | zij = x(i, j) + y(i, j); 16 | ok = ok & abs(z(i, j) - zij) < 1e-10; 17 | end 18 | end 19 | return 20 | -------------------------------------------------------------------------------- /mat/cholesky_ok.m: -------------------------------------------------------------------------------- 1 | function [ok] = cholesky_ok() 2 | ok = true; 3 | m = 3; 4 | a = rand(m, m); 5 | x = a' * a; 6 | % ------------- 7 | [y, r] = chol(x); 8 | % ------------- 9 | [m, n] = size(y); 10 | ok = ok & (m == 3); 11 | ok = ok & (n == 3); 12 | ok = ok & all ( all( abs(y' * y - x) < 1e-10 ) ); 13 | for i = 1 : m 14 | for j = 1 : (i-1) 15 | ok = ok & (y(i,j) == 0); 16 | end 17 | end 18 | ok = ok & (r == 0); 19 | return 20 | -------------------------------------------------------------------------------- /mat/diag_prod_ok.m: -------------------------------------------------------------------------------- 1 | function [ok] = diag_prod_ok() 2 | ok = true; 3 | m = 3; 4 | n = 2; 5 | x = rand(1, m); 6 | y = rand(m, n); 7 | % ------------------- 8 | z = diag(x) * y; 9 | % ------------------- 10 | [m, n] = size(z); 11 | ok = ok & (m == 3); 12 | ok = ok & (n == 2); 13 | for i = [1 : m] 14 | for j = [1 : n] 15 | zij = x(i) * y(i, j); 16 | ok = ok & abs(z(i, j) - zij) < 1e-10; 17 | end 18 | end 19 | return 20 | -------------------------------------------------------------------------------- /mat/element_div_ok.m: -------------------------------------------------------------------------------- 1 | function [ok] = element_div_ok() 2 | ok = true; 3 | m = 3; 4 | n = 2; 5 | x = rand(m, n); 6 | y = rand(m, n); 7 | % -------------- 8 | z = x ./ y; 9 | % -------------- 10 | [m, n] = size(z); 11 | ok = ok & (m == 3); 12 | ok = ok & (n == 2); 13 | for i = [1 : m] 14 | for j = [1 : n] 15 | zij = x(i, j) / y(i, j); 16 | ok = ok & abs(z(i, j) - zij) < 1e-10; 17 | end 18 | end 19 | return 20 | -------------------------------------------------------------------------------- /mat/element_prod_ok.m: -------------------------------------------------------------------------------- 1 | function [ok] = element_prod_ok() 2 | ok = true; 3 | m = 3; 4 | n = 2; 5 | x = rand(m, n); 6 | y = rand(m, n); 7 | % -------------- 8 | z = x .* y; 9 | % -------------- 10 | [m, n] = size(z); 11 | ok = ok & (m == 3); 12 | ok = ok & (n == 2); 13 | for i = [1 : m] 14 | for j = [1 : n] 15 | zij = x(i, j) * y(i, j); 16 | ok = ok & abs(z(i, j) - zij) < 1e-10; 17 | end 18 | end 19 | return 20 | -------------------------------------------------------------------------------- /mat/element_unary_ok.m: -------------------------------------------------------------------------------- 1 | function [ok] = element_unary_ok() 2 | ok = true; 3 | m = 2; 4 | n = 3; 5 | x = rand(m, n); 6 | % --------------- 7 | abs_x = abs(x); 8 | log_x = log(x); 9 | % --------------- 10 | for i = 1 : m 11 | for j = 1 : n 12 | ok = ok & ( abs_x(i, j) == abs(x(i, j)) ); 13 | ok = ok & ( log_x(i, j) == log(x(i, j)) ); 14 | end 15 | end 16 | return 17 | -------------------------------------------------------------------------------- /mat/extend_ok.m: -------------------------------------------------------------------------------- 1 | function [ok] = extend_ok() 2 | ok = true; 3 | m = 2; 4 | n = 3; 5 | for i = 1 : m 6 | for j = 1 : n 7 | x(i, j) = i + j; 8 | end 9 | end 10 | % ----------------------- 11 | for i = 1 : m 12 | x(i, n + 1) = i + (n + 1); 13 | end 14 | % ----------------------- 15 | [m, n] = size(x); 16 | ok = ok & (m == 2); 17 | ok = ok & (n == 4); 18 | for i = 1 : m 19 | for j = 1 : n 20 | ok = ok & (x(i, j) == i + j); 21 | end 22 | end 23 | return 24 | -------------------------------------------------------------------------------- /mat/getstarted.m: -------------------------------------------------------------------------------- 1 | a = [ 1., 2. ; 3., 4. ]; 2 | b = [ 1. ; 2. ]; 3 | x = a \ b 4 | -------------------------------------------------------------------------------- /mat/mat2cpp_ok.m: -------------------------------------------------------------------------------- 1 | % $begin mat2cpp_ok.m$$ $newlinech %$$ 2 | % $spell 3 | % cholesky 4 | % Matlab 5 | % div 6 | % mat2cpp 7 | % feval 8 | % randn 9 | % $$ 10 | % 11 | % $section Run Matlab or Octave Version of Examples$$ 12 | % 13 | % $codep 14 | function mat2cpp_ok() 15 | ok = true; 16 | ok = ok & run_test('add_ok'); 17 | ok = ok & run_test('cholesky_ok'); 18 | ok = ok & run_test('diag_prod_ok'); 19 | ok = ok & run_test('element_div_ok'); 20 | ok = ok & run_test('element_prod_ok'); 21 | ok = ok & run_test('element_unary_ok'); 22 | ok = ok & run_test('extend_ok'); 23 | ok = ok & run_test('matrix_div_ok'); 24 | ok = ok & run_test('matrix_prod_ok'); 25 | ok = ok & run_test('max_ok'); 26 | ok = ok & run_test('min_ok'); 27 | ok = ok & run_test('ones_ok'); 28 | ok = ok & run_test('rand_ok'); 29 | ok = ok & run_test('randn_ok'); 30 | ok = ok & run_test('scalar_div_ok'); 31 | ok = ok & run_test('scalar_prod_ok'); 32 | ok = ok & run_test('size_ok'); 33 | ok = ok & run_test('slice_ok'); 34 | ok = ok & run_test('sub_ok'); 35 | ok = ok & run_test('sum_ok'); 36 | ok = ok & run_test('transpose_ok'); 37 | ok = ok & run_test('zeros_ok'); 38 | if ok 39 | 'All tests passed' 40 | else 41 | 'At least one test failed' 42 | end 43 | return 44 | % ------------------------------------ 45 | function [ok] = run_test(test_name) 46 | ok = feval(test_name); 47 | if( ok ) 48 | ['ok: ', test_name] 49 | else 50 | ['error: ', test_name] 51 | end 52 | return 53 | % $$ 54 | % 55 | % $end 56 | -------------------------------------------------------------------------------- /mat/matrix_div_ok.m: -------------------------------------------------------------------------------- 1 | function [ok] = matrix_div_ok() 2 | ok = true; 3 | m = 3; 4 | n = 2; 5 | x = rand(m, m); 6 | y = rand(m, n); 7 | % ------------- 8 | z = x \ y; 9 | % ------------- 10 | [m, n] = size(z); 11 | ok = ok & (m == 3); 12 | ok = ok & (n == 2); 13 | ok = ok & all ( all( abs(x * z - y) < 1e-10 ) ); 14 | return 15 | -------------------------------------------------------------------------------- /mat/matrix_prod_ok.m: -------------------------------------------------------------------------------- 1 | function [ok] = matrix_prod_ok() 2 | ok = true; 3 | m = 3; 4 | n = 2; 5 | p = 4; 6 | x = rand(m, n); 7 | y = rand(n, p); 8 | % -------------- 9 | z = x * y; 10 | % -------------- 11 | [m, p] = size(z); 12 | ok = ok & (m == 3); 13 | ok = ok & (p == 4); 14 | for i = [1 : m] 15 | for j = [1 : p] 16 | zij = 0; 17 | for k = [1 : n] 18 | zij = zij + x(i, k) * y(k, j); 19 | end 20 | ok = ok & abs(z(i, j) - zij) < 1e-10; 21 | end 22 | end 23 | return 24 | -------------------------------------------------------------------------------- /mat/max_ok.m: -------------------------------------------------------------------------------- 1 | function [ok] = max_ok() 2 | ok = true; 3 | m = 2; 4 | n = 3; 5 | x = rand(m, n); 6 | % ------------------ 7 | y = max( max(x) ); 8 | % ------------------ 9 | [m, n] = size(y); 10 | ok = ok & (m == 1) & (n == 1); 11 | ok = ok & all( all ( x <= y ) ); 12 | ok = ok & ~ all( all ( x < y ) ); 13 | return 14 | -------------------------------------------------------------------------------- /mat/min_ok.m: -------------------------------------------------------------------------------- 1 | function [ok] = min_ok() 2 | ok = true; 3 | m = 2; 4 | n = 3; 5 | x = rand(m, n); 6 | % ------------------ 7 | y = min( min(x) ); 8 | % ------------------ 9 | [m, n] = size(y); 10 | ok = ok & (m == 1) & (n == 1); 11 | ok = ok & all( all ( x >= y ) ); 12 | ok = ok & ~ all( all ( x > y ) ); 13 | return 14 | -------------------------------------------------------------------------------- /mat/ones_ok.m: -------------------------------------------------------------------------------- 1 | function [ok] = ones_ok() 2 | ok = true; 3 | m = 2; 4 | n = 3; 5 | % ------------------- 6 | x = ones(2, 3); 7 | % ------------------- 8 | [m, n] = size(x); 9 | ok = ok & (m == 2); 10 | ok = ok & (n == 3); 11 | ok = ok & all( all( x == 1 ) ); 12 | return 13 | -------------------------------------------------------------------------------- /mat/rand_ok.m: -------------------------------------------------------------------------------- 1 | function [ok] = rand_ok() 2 | ok = true; 3 | m = 2; 4 | n = 3; 5 | % ------------------- 6 | x = rand(2, 3); 7 | % ------------------- 8 | [m, n] = size(x); 9 | ok = ok & (m == 2); 10 | ok = ok & (n == 3); 11 | ok = ok & all( all( 0 < x ) ); % probability of limit case is zero 12 | ok = ok & all( all( x < 1 ) ); 13 | ok = ok & all( x(1,:) ~= x(2,:) ); % probability of equality is zero 14 | return 15 | -------------------------------------------------------------------------------- /mat/randn_ok.m: -------------------------------------------------------------------------------- 1 | function [ok] = randn_ok() 2 | ok = true; 3 | m = 100; 4 | n = 11; 5 | % ------------------- 6 | x = randn(m, n); 7 | % ------------------- 8 | [m_, n_]= size(x); 9 | ok = ok & (m == m_); 10 | ok = ok & (n == n_); 11 | N = m * n; 12 | sum_ = sum( sum(x) ); 13 | sumsq = sum( sum( x .* x ) ); 14 | avg = sum_ / N; 15 | var = sumsq / N - avg * avg; 16 | ok = ok & abs( avg ) < 2 / sqrt(N); 17 | % The variance of N independent chi-squares is 2 / N 18 | ok = ok & abs(var - 1.) < 2 * sqrt( 2. / N ); 19 | return 20 | -------------------------------------------------------------------------------- /mat/scalar_div_ok.m: -------------------------------------------------------------------------------- 1 | function [ok] = scalar_div_ok() 2 | ok = true; 3 | m = 3; 4 | n = 2; 5 | x = rand(m, n); 6 | y = rand(1, 1); 7 | % ------------- 8 | z = x / y; 9 | % ------------- 10 | [m, n] = size(z); 11 | ok = ok & (m == 3); 12 | ok = ok & (n == 2); 13 | for i = [1 : m] 14 | for j = [1 : n] 15 | zij = x(i, j) / y; 16 | ok = ok & abs(z(i, j) - zij) < 1e-10; 17 | end 18 | end 19 | return 20 | -------------------------------------------------------------------------------- /mat/scalar_ok.m: -------------------------------------------------------------------------------- 1 | function [ok] = scalar_ok() 2 | ok = true; 3 | m = 2; 4 | n = 3; 5 | u = rand(m, n); 6 | x = 5; 7 | % ---------- 8 | v = u * x; 9 | % ---------- 10 | [m, n] = size(v); 11 | ok = ok & (m == 2) & (n == 3); 12 | for i = 1 : m 13 | for j = 1 : n 14 | ok = ok & abs(v(i,j) - u(i,j) * x(1,1)) < 1e-10; 15 | end 16 | end 17 | return 18 | -------------------------------------------------------------------------------- /mat/scalar_prod_ok.m: -------------------------------------------------------------------------------- 1 | function [ok] = scalar_prod_ok() 2 | ok = true; 3 | m = 3; 4 | n = 2; 5 | x = rand(m, n); 6 | y = rand(1, 1); 7 | % ------------- 8 | z = x * y; 9 | % ------------- 10 | [m, n] = size(z); 11 | ok = ok & (m == 3); 12 | ok = ok & (n == 2); 13 | for i = [1 : m] 14 | for j = [1 : n] 15 | zij = x(i, j) * y; 16 | ok = ok & abs(z(i, j) - zij) < 1e-10; 17 | end 18 | end 19 | return 20 | -------------------------------------------------------------------------------- /mat/size_ok.m: -------------------------------------------------------------------------------- 1 | function [ok] = size_ok() 2 | ok = true; 3 | x = [ 1 2 3 ; 4 5 6 ]; 4 | % ---------------- 5 | [m, n] = size(x); 6 | % ---------------- 7 | ok = ok & (m == 2); 8 | ok = ok & (n == 3); 9 | return 10 | -------------------------------------------------------------------------------- /mat/slice_ok.m: -------------------------------------------------------------------------------- 1 | function [ok] = slice_ok() 2 | ok = true; 3 | m = 3; 4 | n = 4; 5 | x = (1 : m)' * (1 : n); 6 | y = zeros(2, 2); 7 | % -------------------- 8 | r = 1 : 2 : 3; 9 | c = 1 : 2 : 3; 10 | x(r, c) = y; 11 | % -------------------- 12 | [p, q] = size(x); 13 | ok = ok & (m == p); 14 | ok = ok & (n == q); 15 | for i = 1 : m 16 | for j = 1 : n 17 | if any(i == r) & any( j == c) 18 | ok = ok & x(i, j) == 0; 19 | else 20 | ok = ok & x(i, j) == i * j; 21 | end 22 | end 23 | end 24 | return 25 | -------------------------------------------------------------------------------- /mat/sub_ok.m: -------------------------------------------------------------------------------- 1 | function [ok] = sub_ok() 2 | ok = true; 3 | m = 3; 4 | n = 2; 5 | x = rand(m, n); 6 | y = rand(m, n); 7 | % ------------- 8 | z = x - y; 9 | % ------------- 10 | [m, n] = size(z); 11 | ok = ok & (m == 3); 12 | ok = ok & (n == 2); 13 | for i = [1 : m] 14 | for j = [1 : n] 15 | zij = x(i, j) - y(i, j); 16 | ok = ok & abs(z(i, j) - zij) < 1e-10; 17 | end 18 | end 19 | return 20 | -------------------------------------------------------------------------------- /mat/sum_ok.m: -------------------------------------------------------------------------------- 1 | function [ok] = sum_ok() 2 | ok = true; 3 | m = 2; 4 | n = 3; 5 | x = rand(m, n); 6 | % ------------------ 7 | y = sum( sum(x) ); 8 | % ------------------ 9 | [p, q] = size(y); 10 | ok = ok & (p == 1) & (q == 1); 11 | ok = ok & abs( y - ones(1, m) * x * ones(n, 1) ) < 1e-10; 12 | return 13 | -------------------------------------------------------------------------------- /mat/transpose_ok.m: -------------------------------------------------------------------------------- 1 | function [ok] = transpose_ok() 2 | ok = true; 3 | m = 2; 4 | n = 3; 5 | x = rand(m, n); 6 | % ------- 7 | y = x'; 8 | % ------- 9 | [r, c] = size(y); 10 | ok = ok & (r == n); 11 | ok = ok & (c == m); 12 | for i = 1 : m 13 | for j = 1 : n 14 | ok = ok & (y(j, i) == x(i, j)); 15 | end 16 | end 17 | return 18 | -------------------------------------------------------------------------------- /mat/zeros_ok.m: -------------------------------------------------------------------------------- 1 | function [ok] = zeros_ok() 2 | ok = true; 3 | m = 2; 4 | n = 3; 5 | % -------------------- 6 | x = zeros(2, 3); 7 | % -------------------- 8 | [m, n] = size(x); 9 | ok = ok & (m == 2); 10 | ok = ok & (n == 3); 11 | ok = ok & all( all( x == 0 ) ); 12 | return 13 | -------------------------------------------------------------------------------- /mat2cpp.hpp: -------------------------------------------------------------------------------- 1 | # ifndef MAT2CPP_INCLUDED 2 | # define MAT2CPP_INCLUDED 3 | /* 4 | $begin mat2cpp.hpp$$ 5 | $spell 6 | randn 7 | cpp 8 | Ublas 9 | Matlab or Octave 10 | C++ 11 | hpp 12 | namespace 13 | const 14 | div 15 | typedef 16 | $$ 17 | 18 | $index include, mat2cpp.hpp$$ 19 | 20 | $section The Matlab or Octave to C++ Using Ublas Include File$$ 21 | 22 | $head Syntax$$ 23 | $code # include $$ 24 | 25 | $head Purpose$$ 26 | 27 | $head Ublas Linkage$$ 28 | The $code mat2cpp.hpp$$ contains 29 | the following include commands: 30 | $codep 31 | # include 32 | # include 33 | $$ 34 | In addition, it links the following 35 | $code ublas$$ identifiers to the $code mat2cpp$$ namespace: 36 | $code matrix$$, $code zero_matrix$$, $code scalar_matrix$$, 37 | $code slice$$, $code matrix_slice$$. 38 | It also defines $code matrix_slice_double$$ using 39 | $codep 40 | typedef matrix_slice< matrix > matrix_slice_double; 41 | $$ 42 | 43 | $head mat2cpp Functions$$ 44 | The $code mat2cpp.hpp$$ include also 45 | defines linkage for the following 46 | $code mat2cpp$$ namespace functions: 47 | 48 | $table 49 | $code abs$$ $cnext $tref element_unary$$ $rnext 50 | $code log$$ $cnext $tref element_unary$$ $rnext 51 | $code diag_prod$$ $cnext $tref diag_prod$$ $rnext 52 | $code matrix_div$$ $cnext $tref matrix_div$$ $rnext 53 | $code max$$ $cnext $tref max$$ $rnext 54 | $code min$$ $cnext $tref min$$ $rnext 55 | $code scalar$$ $cnext $tref scalar$$ $rnext 56 | $code sum$$ $cnext $tref sum$$ $rnext 57 | $code rand$$ $cnext $tref rand$$ $rnext 58 | $code randn$$ $cnext $tref randn$$ $rnext 59 | $tend 60 | 61 | $subhead Fortran Linkage$$ 62 | The $code mat2cpp.hpp$$ include also 63 | defines the following types: 64 | $table 65 | $code fortran_integer$$ $cnext 66 | the type in C++ corresponding to Fortran integer $rnext 67 | $code fortran_double$$ $cnext 68 | the type in C++ corresponding to Fortran double precision $rnext 69 | $code fortran_subroutine$$ $cnext 70 | the return type in C++ corresponding to Fortran subroutine 71 | $tend 72 | 73 | 74 | $end 75 | */ 76 | 77 | # include 78 | # include 79 | namespace mat2cpp 80 | { // linking certain identifiers from ublas ---------- 81 | using boost::numeric::ublas::matrix; 82 | using boost::numeric::ublas::scalar_matrix; 83 | using boost::numeric::ublas::zero_matrix; 84 | using boost::numeric::ublas::slice; 85 | using boost::numeric::ublas::matrix_slice; 86 | 87 | // define the matrix_slice_double type 88 | typedef matrix_slice< matrix > matrix_slice_double; 89 | // ------------------------------------------------- 90 | 91 | // element_unary functions ------------------------- 92 | extern matrix abs (const matrix &x); 93 | extern matrix log (const matrix &x); 94 | // ------------------------------------------------- 95 | 96 | extern matrix cholesky 97 | (const matrix &x, size_t &rank); 98 | 99 | extern matrix diag_prod 100 | (const matrix &x, const matrix &y); 101 | 102 | extern matrix matrix_div 103 | (const matrix &x, const matrix &y, size_t &rank); 104 | 105 | extern double max 106 | (const matrix &x); 107 | 108 | extern double min 109 | (const matrix &x); 110 | 111 | extern double scalar 112 | (const matrix &x); 113 | 114 | extern double sum 115 | (const matrix &x); 116 | 117 | extern matrix rand 118 | (size_t m, size_t n); 119 | 120 | extern matrix randn 121 | (size_t m, size_t n); 122 | 123 | // fortran linkage types ------------- 124 | typedef int fortran_integer; 125 | typedef double fortran_double; 126 | typedef void fortran_subroutine; 127 | // ----------------------------------- 128 | } 129 | 130 | # endif 131 | -------------------------------------------------------------------------------- /mztuni.hpp: -------------------------------------------------------------------------------- 1 | # ifndef MZTUNI_INCLUDED 2 | # define MZTUNI_INCLUDED 3 | 4 | extern size_t mztuni_seed(size_t seed); 5 | extern double mztuni(size_t seedm1 = 0); 6 | 7 | # endif 8 | -------------------------------------------------------------------------------- /omh/add.omh: -------------------------------------------------------------------------------- 1 | /* 2 | $begin add$$ 3 | $index element, add$$ 4 | $index add, element$$ 5 | $spell 6 | ublas 7 | Elementwise 8 | Matlab or Octave 9 | C++ 10 | $$ 11 | 12 | $section Elementwise Addition$$ 13 | 14 | $head Syntax$$ 15 | $table 16 | $bold Matlab or Octave$$ $cnext 17 | $syntax%%z% = %x% + %y%$$ 18 | $rnext 19 | $bold C++$$ $cnext 20 | $syntax%%z% = %x% + %y%$$ 21 | $tend 22 | 23 | $head Matlab or Octave$$ 24 | If $italic x$$ and $italic y$$ are a $latex m \times n$$ matrices, 25 | $syntax% 26 | %z% = %x% + %y% 27 | %$$ 28 | sets $italic z$$ to the $latex m \times n$$ matrix with 29 | the $latex (i,j)$$ element given by 30 | $latex \[ 31 | z_{i,j} = x_{i,j} + y_{i,j} 32 | \] $$ 33 | 34 | $subhead Example$$ 35 | $code 36 | $verbatim%mat/add_ok.m%5%$$ 37 | $$ 38 | 39 | 40 | $head C++$$ 41 | The corresponding C++ syntax is 42 | $syntax% 43 | %z% = %x% + %y% 44 | %$$ 45 | where $italic x$$, $italic y$$, and $italic z$$ are 46 | ublas $code matrix$$ objects 47 | with size $latex m \times n$$. 48 | 49 | $subhead Example$$ 50 | $code 51 | $verbatim%cpp/add_ok.cpp%5%$$ 52 | $$ 53 | 54 | $end 55 | */ 56 | -------------------------------------------------------------------------------- /omh/cholesky.omh: -------------------------------------------------------------------------------- 1 | /* 2 | $begin cholesky$$ 3 | $latex \newcommand{\T}{{\rm T}}$$ 4 | $spell 5 | cholesky 6 | Matlab 7 | chol 8 | ublas 9 | $$ 10 | $index cholesky$$ 11 | $index matrix, cholesky$$ 12 | $index cholesky, matrix$$ 13 | 14 | $section Cholesky Factor a Matrix Division$$ 15 | 16 | $head Syntax$$ 17 | $table 18 | $bold Matlab or Octave$$ $cnext 19 | $syntax%[%y%, %r%] = chol(%x%)%$$ 20 | $rnext 21 | $bold C++$$ $cnext 22 | $syntax%%y% = cholesky(%x%, %r%)%$$ 23 | $tend 24 | 25 | $head Matlab or Octave$$ 26 | If $italic x$$ is an $latex m \times m$$ matrix 27 | $syntax% 28 | %y% = chol(%x%) 29 | %$$ 30 | sets $italic z$$ to the 31 | upper triangular $latex m \times m$$ matrix such that 32 | $latex \[ 33 | x = y^\T * y 34 | \] $$ 35 | If $italic r$$ is not zero, then the matrix $italic x$$ 36 | is singular and $italic r$$ is the rank of the largest non-singular 37 | principal minor. 38 | 39 | $subhead Example$$ 40 | $code 41 | $verbatim%mat/cholesky_ok.m%5%$$ 42 | $$ 43 | 44 | 45 | $head C++$$ 46 | The corresponding C++ syntax is 47 | $syntax% 48 | %z% = cholesky(%x%, %y%, %rank%) 49 | %$$ 50 | where $italic x$$ and $italic y$$ are 51 | ublas $code matrix$$ objects. 52 | The size of $italic x$$ and $italic y$$ are $latex m \times m$$, 53 | The return value of the $code size_t$$ argument $italic rank$$ is the rank 54 | of the largest principal minor of $italic x$$. 55 | 56 | $subhead Example$$ 57 | $code 58 | $verbatim%cpp/cholesky_ok.cpp%5%$$ 59 | $$ 60 | 61 | $children% 62 | lib/cholesky.cpp 63 | %$$ 64 | 65 | $subhead Source$$ 66 | The file $xref/cholesky.cpp/$$ contains the 67 | C++ source code for these functions. 68 | 69 | $end 70 | */ 71 | -------------------------------------------------------------------------------- /omh/diag_prod.omh: -------------------------------------------------------------------------------- 1 | $begin diag_prod$$ 2 | $index diagonal, product$$ 3 | $index product, diagonal$$ 4 | $index diag_prod$$ 5 | $spell 6 | ublas 7 | cpp 8 | cassert 9 | hpp 10 | namespace 11 | const 12 | Matlab or Octave 13 | C++ 14 | $$ 15 | 16 | $section Diagonal Matrix Product with General Matrix$$ 17 | 18 | $head Syntax$$ 19 | $table 20 | $bold Matlab or Octave$$ $cnext 21 | $syntax%%z% = diag(%x%) * %y%$$ 22 | $rnext 23 | $bold C++$$ $cnext 24 | $syntax%%z% = diag_prod(%x%, %y%)%$$ 25 | $tend 26 | 27 | $head Matlab or Octave$$ 28 | If $italic x$$ is an $latex 1 \times m$$ or $latex m \times 1$$ matrix, 29 | and $italic y$$ are a $latex m \times n$$ matrix, 30 | $syntax% 31 | %z% = diag(%x%) * %y% 32 | %$$ 33 | sets $italic z$$ to the $latex m \times n$$ matrix with 34 | the $latex (i,j)$$ element given by 35 | $latex \[ 36 | z_{i,j} = x_i * y_{i,j} 37 | \] $$ 38 | 39 | $subhead Example$$ 40 | $code 41 | $verbatim%mat/diag_prod_ok.m%5%$$ 42 | $$ 43 | 44 | 45 | $head C++$$ 46 | The following is a C++ implementation of the operation above 47 | with the syntax 48 | $syntax% 49 | %z% = diag_prod(%x%, %y%) 50 | %$$ 51 | where $italic x$$ is an $latex 1 \times m$$ or $latex m \times 1$$ 52 | ublas $code matrix$$ object, 53 | and $italic y$$, $italic z$$ are ublas $code matrix$$ objects 54 | with size $latex m \times n$$. 55 | 56 | $subhead Example$$ 57 | $code 58 | $verbatim%cpp/diag_prod_ok.cpp%5%$$ 59 | $$ 60 | 61 | $children% 62 | lib/diag_prod.cpp 63 | %$$ 64 | 65 | $subhead Source$$ 66 | The file $xref/diag_prod.cpp/$$ contains the 67 | C++ source code for these functions. 68 | 69 | $end 70 | -------------------------------------------------------------------------------- /omh/element_div.omh: -------------------------------------------------------------------------------- 1 | $begin element_div$$ 2 | $spell 3 | ublas 4 | cassert 5 | div 6 | hpp 7 | namespace 8 | const 9 | Elementwise 10 | Matlab or Octave 11 | C++ 12 | $$ 13 | $index element_div$$ 14 | $index element, division$$ 15 | $index division, element$$ 16 | 17 | $section Elementwise Division$$ 18 | 19 | $head Syntax$$ 20 | $table 21 | $bold Matlab or Octave$$ $cnext 22 | $syntax%%z% = %x% ./ %y%%$$ 23 | $rnext 24 | $bold C++$$ $cnext 25 | $syntax%%z% = element_div(%x%, %y%)%$$ 26 | $tend 27 | 28 | $head Matlab or Octave$$ 29 | If $italic x$$ and $italic y$$ are a $latex m \times n$$ matrices, 30 | $syntax% 31 | %z% = %x% ./ %y% 32 | %$$ 33 | sets $italic z$$ to the $latex m \times n$$ matrix with 34 | the $th (i,j)$$ element given by 35 | $latex \[ 36 | z_{i,j} = x_{i,j} / y_{i,j} 37 | \] $$ 38 | 39 | $subhead Example$$ 40 | $code 41 | $verbatim%mat/element_div_ok.m%5%$$ 42 | $$ 43 | 44 | $head C++$$ 45 | The corresponding C++ syntax is 46 | $syntax% 47 | %z% = element_div(%x%, %y%) 48 | %$$ 49 | where $italic x$$, $italic y$$ and $italic z$$ 50 | ublas $code matrix$$ objects with size 51 | $latex m \times n$$. 52 | 53 | $subhead Example$$ 54 | $code 55 | $verbatim%cpp/element_div_ok.cpp%5%$$ 56 | $$ 57 | 58 | $end 59 | -------------------------------------------------------------------------------- /omh/element_prod.omh: -------------------------------------------------------------------------------- 1 | $begin element_prod$$ 2 | $index element, product$$ 3 | $index product, element$$ 4 | $index element_prod$$ 5 | $spell 6 | ublas 7 | cassert 8 | hpp 9 | namespace 10 | const 11 | Elementwise 12 | Matlab or Octave 13 | C++ 14 | $$ 15 | 16 | $section Elementwise Product$$ 17 | 18 | $head Syntax$$ 19 | $table 20 | $bold Matlab or Octave$$ $cnext 21 | $syntax%%z% = %x% .* %y%$$ 22 | $rnext 23 | $bold C++$$ $cnext 24 | $syntax%%z% = element_prod(%x%, %y%)%$$ 25 | $tend 26 | 27 | $head Matlab or Octave$$ 28 | If $italic x$$ and $italic y$$ are a $latex m \times n$$ matrices, 29 | $syntax% 30 | %z% = %x% .* %y% 31 | %$$ 32 | sets $italic z$$ to the $latex m \times n$$ matrix with 33 | the $th (i,j)$$ element given by 34 | $latex \[ 35 | z_{i,j} = x_{i,j} * y_{i,j} 36 | \] $$ 37 | 38 | $subhead Example$$ 39 | $code 40 | $verbatim%mat/element_prod_ok.m%5%$$ 41 | $$ 42 | 43 | 44 | $head C++$$ 45 | The corresponding C++ syntax is 46 | $syntax% 47 | %z% = element_prod(%x%, %y%) 48 | %$$ 49 | where $italic x$$, $italic y$$ and $italic z$$ 50 | ublas $code matrix$$ objects with size 51 | $latex m \times n$$. 52 | 53 | $subhead Example$$ 54 | $code 55 | $verbatim%cpp/element_prod_ok.cpp%5%$$ 56 | $$ 57 | 58 | $end 59 | -------------------------------------------------------------------------------- /omh/element_unary.omh: -------------------------------------------------------------------------------- 1 | $begin element_unary$$ 2 | $spell 3 | Matlab 4 | Ublas 5 | elementwise 6 | mat2cpp 7 | $$ 8 | $index elementwise, unary$$ 9 | $index unary, elementwise$$ 10 | $index math, function$$ 11 | 12 | $section Matlab or Octave Elementwise Unary Functions$$ 13 | 14 | $head Syntax$$ 15 | $table 16 | $bold Matlab or Octave$$ $cnext 17 | $syntax%%y% = %f%(%x%)%$$ 18 | $rnext 19 | $bold C++$$ $cnext 20 | $syntax%%y% = %f%(%x%)%$$ 21 | $tend 22 | 23 | $head Matlab or Octave$$ 24 | If $italic x$$ is an $latex m \times n$$ matrix, 25 | $syntax% 26 | %y% = %f%(%x%) 27 | %$$ 28 | sets $italic y$$ to the $latex m \times n$$ matrix with 29 | $latex \[ 30 | y_{i,j} = f( x_{i,j} ) 31 | \] $$ 32 | where $italic f$$ can be any of the following functions: 33 | $table 34 | $bold f$$ $cnext $bold Description$$ $rnext 35 | $code abs$$ $cnext absolute value $rnext 36 | $code log$$ $cnext logarithm 37 | $tend 38 | 39 | $subhead Example$$ 40 | $code 41 | $verbatim%mat/element_unary_ok.m%5%$$ 42 | $$ 43 | 44 | 45 | $head C++$$ 46 | The following is a C++ implementation of the elementwise 47 | unary functions above with the syntax: 48 | $syntax% 49 | %y% = %f%(%x%) 50 | %$$ 51 | where $italic x$$ and $italic y$$ 52 | ublas $code matrix$$ objects with size 53 | $latex m \times n$$. 54 | 55 | $subhead Example$$ 56 | $code 57 | $verbatim%cpp/element_unary_ok.cpp%5%$$ 58 | $$ 59 | 60 | $children% 61 | lib/element_unary.cpp 62 | %$$ 63 | 64 | $subhead Source$$ 65 | The file $xref/element_unary.cpp/$$ contains the 66 | C++ source code for these functions. 67 | 68 | $end 69 | -------------------------------------------------------------------------------- /omh/elementwise.omh: -------------------------------------------------------------------------------- 1 | $begin elementwise$$ 2 | $spell 3 | elementwise 4 | $$ 5 | 6 | 7 | $section Elementwise Operations$$ 8 | 9 | $childtable% 10 | omh/add.omh% 11 | omh/element_div.omh% 12 | omh/element_prod.omh% 13 | omh/element_unary.omh% 14 | omh/scalar_div.omh% 15 | omh/scalar_prod.omh% 16 | omh/sub.omh 17 | %$$ 18 | 19 | 20 | $end 21 | -------------------------------------------------------------------------------- /omh/extend.omh: -------------------------------------------------------------------------------- 1 | $begin extend$$ 2 | $index extend$$ 3 | $spell 4 | Matlab 5 | resize 6 | mat2cpp 7 | ublas 8 | $$ 9 | 10 | $section Extending a Vector$$ 11 | 12 | $head Syntax$$ 13 | $table 14 | $bold Matlab or Octave$$ $cnext 15 | $syntax%%x%(%i%, %n% + 1) = %y%$$ 16 | $rnext 17 | $bold C++$$ $cnext 18 | $syntax%%x%.resize(%m%, %n% + 1)%$$ 19 | $rnext $cnext 20 | $syntax%%x%(%i%, %n%) = %y%$$ 21 | $tend 22 | 23 | $head Matlab or Octave$$ 24 | Suppose that $italic x$$ is a $latex m \times n$$ matrix, 25 | $italic i$$ is an index between one and $italic m$$, 26 | and $italic y$$ is a scalar, 27 | the Matlab or Octave syntax 28 | $syntax% 29 | %x%(%i%, %n% + 1) = %y% 30 | %$$ 31 | extends $italic x$$ to an $latex m \times (n+1)$$ matrix 32 | and sets the element with index $latex (i, n+1)$$ 33 | to have value $italic y$$. 34 | 35 | $subhead Example$$ 36 | $code 37 | $verbatim%mat/extend_ok.m%5%$$ 38 | $$ 39 | 40 | 41 | $head C++$$ 42 | Suppose that $italic x$$ is an $latex m \times n$$ 43 | ublas $code matrix$$, 44 | $italic i$$ is a $code size_t$$ index between zero and $latex m - 1$$, 45 | and $italic y$$ has type $code double$$, 46 | the C++ syntax 47 | $syntax% 48 | %x%.resize(%m%, %n% + 1) 49 | %x%(%i%, %n%) = %y% 50 | %$$ 51 | extends the matrix $italic x$$ to have size $latex m \times (n + 1)$$ 52 | and sets the element with index $latex (i, n)$$ 53 | to have value $italic y$$. 54 | 55 | $subhead Example$$ 56 | $code 57 | $verbatim%cpp/extend_ok.cpp%5%$$ 58 | $$ 59 | 60 | $end 61 | -------------------------------------------------------------------------------- /omh/getstarted.omh: -------------------------------------------------------------------------------- 1 | $begin getstarted$$ 2 | $spell 3 | Matlab or Octave 4 | getstarted 5 | cpp 6 | hpp 7 | dir 8 | llapack 9 | libmat2cpp 10 | lmat2cpp 11 | ublas 12 | $$ 13 | 14 | $section Getting Started$$ 15 | 16 | $head Purpose$$ 17 | This is a quick introduction by example to the 18 | conversion of Matlab or Octave to C++ using 19 | $href%http://www.boost.org/libs/numeric/ublas/doc/overview.htm%ublas%$$ 20 | and this package. 21 | 22 | $head Problem Definition$$ 23 | Suppose that we want to solve the following set of linear equations: 24 | $latex \[ 25 | \left( \begin{array}{cc} 26 | 1 & 2 \\ 27 | 3 & 4 28 | \end{array} \right) 29 | \left( \begin{array}{cc} 30 | x_1 \\ 31 | x_2 32 | \end{array} \right) 33 | = 34 | \left( \begin{array}{cc} 35 | 1 \\ 36 | 2 37 | \end{array} \right) 38 | \] $$ 39 | 40 | $head Matlab or Octave Solution$$ 41 | 42 | $subhead Program$$ 43 | $code 44 | $verbatim%mat/getstarted.m%$$ 45 | $$ 46 | 47 | $subhead Output$$ 48 | Executing this program in Octave yields the following output: 49 | $codep 50 | x = 51 | 52 | 0.00000 53 | 0.50000 54 | $$ 55 | 56 | $head Conversion to C++ using C++$$ 57 | 58 | $subhead Program$$ 59 | $code 60 | $verbatim%cpp/getstarted.cpp%$$ 61 | $$ 62 | 63 | $subhead Compile and Link$$ 64 | The following command compiles and links this program 65 | using the GNU C++ compiler: 66 | $syntax% 67 | g++ getstarted.cpp -o getstarted \ 68 | -I%boost_dir% -L%prefix_dir%/lib -lmat2cpp -llapack 69 | %$$ 70 | where $italic boost_dir$$ is the directory where the 71 | $syntax% 72 | %boost_dir%/boost/numeric/ublas/matrix.hpp 73 | %$$ 74 | include file is installed 75 | (see the $xref/mat2cpp/Test and Install/mat2cpp install/$$ instructions). 76 | 77 | $subhead Output$$ 78 | $index output, matrix$$ 79 | $index matrix, output$$ 80 | $codep 81 | x =[2,1]((0),(0.5)) 82 | $$ 83 | 84 | 85 | $end 86 | -------------------------------------------------------------------------------- /omh/library.omh: -------------------------------------------------------------------------------- 1 | $begin library$$ 2 | $spell 3 | OMhelp 4 | autoconf 5 | automake 6 | cpp 7 | dd 8 | dist 9 | elementwise 10 | gz 11 | hpp 12 | makefile 13 | matlab 14 | omhelp 15 | mm 16 | namespace 17 | omh 18 | yy 19 | $$ 20 | 21 | 22 | 23 | 24 | $section Adding a New Routine to the Mat2cpp Library$$ 25 | 26 | $head Purpose$$ 27 | This guide lists the steps you must go through to add a new feature 28 | to the $code mat2cpp$$ library. 29 | For the sake of presentation, we will call it $italic new_routine$$. 30 | 31 | $head Include File$$ 32 | In the file $code mat2cpp/mat2cpp.hpp$$: 33 | In the comments, 34 | under the heading mat2cpp Functions, 35 | add $italic new_routine$$ 36 | to the list of functions in the $code mat2cpp$$ namespace. 37 | In the source code, 38 | an external prototype for $italic new_routine$$. 39 | 40 | 41 | $head Library$$ 42 | Create the file $syntax%mat2cpp/lib/%new_routine%.cpp%$$. 43 | This file should contain the source for the routine 44 | together with the proper comments so OMhelp can 45 | include the source code in the documentation. 46 | In addition, add 47 | $syntax%mat2cpp/lib/%new_routine%.cpp%$$ 48 | to the file $code mat2cpp/lib/Makefile.am$$. 49 | 50 | 51 | $head Documentation$$ 52 | Create the file $syntax%mat2cpp/omh/%new_routine%.omh%$$. 53 | This file should contain the OMhelp documentation for the routine. 54 | In addition, 55 | decide which documentation file should be the parent 56 | of this routine; for example, 57 | should it come under $code mat2cpp/omh/elementwise.omh$$ 58 | or perhaps $code mat2cpp/omh/other.omh$$. 59 | Then add a reference to 60 | $syntax%mat2cpp/omh/%new_routine%.omh%$$ 61 | in the parent documentation file. 62 | You should also add a link to the documentation 63 | for $italic new_routine$$ in $code mat2cpp/NEWS$$. 64 | 65 | $head Octave Test$$ 66 | Create the file 67 | $syntax%mat2cpp/mat/%new_routine%_ok.m%$$ 68 | This file should contain a test of the feature 69 | you are adding in the context of Matlab or Octave. 70 | You will also need to add 71 | $syntax%mat2cpp/cpp/%new_routine%_ok%$$ 72 | to the file $code mat2cpp/cpp/mat2cpp_ok.m$$ 73 | The execute the tests in Octave or Matlab 74 | to make sure that it is correct 75 | (before going ahead with a similar test in C++ 76 | using the new code). 77 | 78 | $head C++ Test$$ 79 | Create the file 80 | $syntax%mat2cpp/cpp/%new_routine%_ok.cpp%$$. 81 | This file should contain a test of $italic new_routine$$. 82 | In addition, add 83 | $syntax%mat2cpp/cpp/%new_routine%_ok.cpp%$$ 84 | to the file $code mat2cpp/cpp/Makefile.am$$. 85 | You will also need to add 86 | $syntax%mat2cpp/cpp/%new_routine%_ok%$$ 87 | to the file $code mat2cpp/cpp/mat2cpp_ok.cpp$$ 88 | 89 | $head Build$$ 90 | In the $code mat2cpp$$ directory, 91 | execute the command 92 | $codep 93 | ./Build version 94 | ./Build autoconf 95 | ./Build automake 96 | $$ 97 | This will make sure that your $code Makefile.am$$ files 98 | can be processed by $code automake$$. 99 | If no errors result, 100 | continue on with the command 101 | $codep 102 | ./Build configure 103 | $$ 104 | If the boost directory is not proper for your system, 105 | then edit the $code Build$$ script so that it is and 106 | repeat this command. 107 | Then execute the commands 108 | $codep 109 | ./Build make 110 | cpp/mat2cpp_ok 111 | $$ 112 | This should execute the complete set of tests including 113 | the one you just added. 114 | The next step is to execute 115 | $codep 116 | ./Build omhelp 117 | $$ 118 | This will build you new documentation 119 | (and you may have to correct OMhelp errors before continuing). 120 | You can complete the changes by executing the commands: 121 | $codep 122 | ./Build dist 123 | ./Build test 124 | $$ 125 | The change into the directory and test the distribution 126 | $syntax% 127 | cd mat2cpp-%yy%-%mm%-%dd% 128 | cpp/mat2cpp_ok 129 | %$$ 130 | This should run the C++ version of the tests program. 131 | $codep 132 | cd mat 133 | octave 134 | mat2cpp_ok 135 | $$ 136 | This should run the Octave version of the test program 137 | (running the Matlab is similar). 138 | 139 | $head Distribution$$ 140 | The final distribution is 141 | $syntax% 142 | mat2cpp-%yy%-%mm%-%dd%.tar.gz 143 | %$$ 144 | 145 | 146 | $end 147 | -------------------------------------------------------------------------------- /omh/matrix_div.omh: -------------------------------------------------------------------------------- 1 | /* 2 | $begin matrix_div$$ 3 | $spell 4 | ublas 5 | cpp 6 | C++ 7 | Lapack 8 | dgesv 9 | nrhs 10 | lda 11 | ipv 12 | ldb 13 | fm 14 | fn 15 | cassert 16 | div 17 | hpp 18 | namespace 19 | const 20 | Elementwise 21 | Matlab or Octave 22 | C++ 23 | $$ 24 | $index matrix_div$$ 25 | $index matrix, division$$ 26 | $index division, matrix$$ 27 | 28 | $section Matrix Division$$ 29 | 30 | $head Syntax$$ 31 | $table 32 | $bold Matlab or Octave$$ $cnext 33 | $syntax%%z% = %x% \ %y%$$ 34 | $rnext 35 | $bold C++$$ $cnext 36 | $syntax%%z% = matrix_div(%x%, %y%, %rank%)%$$ 37 | $tend 38 | 39 | $head Matlab or Octave$$ 40 | If $italic x$$ is an $latex m \times m$$ matrix 41 | and $italic y$$ is an $latex m \times n$$ matrix, 42 | $syntax% 43 | %z% = %x% \ %y% 44 | %$$ 45 | sets $italic z$$ to the $latex m \times n$$ matrix such that 46 | $latex \[ 47 | x * z = y 48 | \] $$ 49 | 50 | $subhead Example$$ 51 | $code 52 | $verbatim%mat/matrix_div_ok.m%5%$$ 53 | $$ 54 | 55 | 56 | $head C++$$ 57 | The corresponding C++ syntax is 58 | $syntax% 59 | %z% = matrix_div(%x%, %y%, %rank%) 60 | %$$ 61 | where $italic x$$, $italic y$$ and $italic z$$ are 62 | ublas $code matrix$$ objects. 63 | The size of $italic x$$ is $latex m \times m$$, 64 | the size of $italic y$$ is $latex m \times n$$, 65 | and the size of $italic z$$ is $latex m \times n$$ matrix. 66 | The return value of the $code size_t$$ argument $italic rank$$ is the rank 67 | of the matrix $italic x$$. 68 | 69 | $subhead Example$$ 70 | $code 71 | $verbatim%cpp/matrix_div_ok.cpp%5%$$ 72 | $$ 73 | 74 | $children% 75 | lib/matrix_div.cpp 76 | %$$ 77 | 78 | $subhead Source$$ 79 | The file $xref/matrix_div.cpp/$$ contains the 80 | C++ source code for these functions. 81 | 82 | $end 83 | */ 84 | -------------------------------------------------------------------------------- /omh/matrix_prod.omh: -------------------------------------------------------------------------------- 1 | /* 2 | $begin matrix_prod$$ 3 | 4 | $index matrix, product$$ 5 | $index product, matrix$$ 6 | 7 | $index matrix, multiply$$ 8 | $index multiply, matrix$$ 9 | 10 | $index matrix, times$$ 11 | $index times, matrix$$ 12 | 13 | $spell 14 | ublas 15 | Matlab or Octave 16 | C++ 17 | $$ 18 | 19 | $section Matrix Product$$ 20 | 21 | $head Syntax$$ 22 | $table 23 | $bold Matlab or Octave$$ $cnext 24 | $syntax%%z% = %x% * %y%$$ 25 | $rnext 26 | $bold C++$$ $cnext 27 | $syntax%%z% = prod(%x%, %y%)%$$ 28 | $tend 29 | 30 | $head Matlab or Octave$$ 31 | If $italic x$$ is an $latex m \times n$$ matrix 32 | and $italic y$$ is an $latex n \times p$$ matrix, 33 | $syntax% 34 | %z% = %x% * %y% 35 | %$$ 36 | sets $italic z$$ to the $latex m \times p$$ matrix with 37 | the $latex (i,j)$$ element given by 38 | $latex \[ 39 | z_{i,j} = \sum_{k=1}^n x_{i,k} y_{k,j} 40 | \] $$ 41 | 42 | $subhead Example$$ 43 | $code 44 | $verbatim%mat/matrix_prod_ok.m%5%$$ 45 | $$ 46 | 47 | 48 | $head C++$$ 49 | The corresponding C++ syntax is 50 | $syntax% 51 | %z% = prod(%x%, %y%) 52 | %$$ 53 | where $italic x$$, $italic y$$ and $italic z$$ are 54 | ublas $code matrix$$ objects with sizes 55 | $latex m \times n$$, $latex n \times p$$ and 56 | $latex m \times p$$ respectively. 57 | 58 | $subhead Example$$ 59 | $code 60 | $verbatim%cpp/matrix_prod_ok.cpp%5%$$ 61 | $$ 62 | 63 | $end 64 | */ 65 | -------------------------------------------------------------------------------- /omh/matrixwise.omh: -------------------------------------------------------------------------------- 1 | $begin matrixwise$$ 2 | $spell 3 | matrixwise 4 | $$ 5 | 6 | 7 | $section Matrixwise Operations$$ 8 | 9 | $childtable% 10 | omh/diag_prod.omh% 11 | omh/cholesky.omh% 12 | omh/matrix_div.omh% 13 | omh/matrix_prod.omh% 14 | omh/transpose.omh 15 | %$$ 16 | 17 | 18 | $end 19 | -------------------------------------------------------------------------------- /omh/max.omh: -------------------------------------------------------------------------------- 1 | /* 2 | $begin max$$ 3 | $index max$$ 4 | $spell 5 | ublas 6 | Matlab 7 | $$ 8 | 9 | $section Maximum Element of a Matrix$$ 10 | 11 | $head Syntax$$ 12 | $table 13 | $bold Matlab or Octave$$ $cnext 14 | $syntax%%y% = max( max(%x%) )%$$ 15 | $rnext 16 | $bold C++$$ $cnext 17 | $syntax%%y% = max(%x%)%$$ 18 | $tend 19 | 20 | $head Matlab or Octave$$ 21 | If $italic x$$ is an $latex m \times n$$ matrix, 22 | $syntax% 23 | %y% = max( max(%x%) ) 24 | %$$ 25 | sets $italic y$$ to the maximum element in the matrix $italic x$$. 26 | If either $italic m$$ or $italic n$$ is equal to one, 27 | $syntax% 28 | %y% = max(%x%) 29 | %$$ 30 | also sets $italic y$$ to the maximum element in the matrix $italic x$$. 31 | 32 | $subhead Example$$ 33 | $code 34 | $verbatim%mat/max_ok.m%5%$$ 35 | $$ 36 | 37 | 38 | $head C++$$ 39 | The following is a C++ implementation of the 40 | Matlab or Octave syntax above: 41 | $syntax% 42 | %y% = max(%x%) 43 | %$$ 44 | where $italic x$$ is a non-empty 45 | (non-zero row and column size) 46 | ublas $code matrix$$ object 47 | and $italic y$$ is a $code double$$. 48 | 49 | $subhead Example$$ 50 | $code 51 | $verbatim%cpp/max_ok.cpp%5%$$ 52 | $$ 53 | 54 | $children% 55 | lib/max.cpp 56 | %$$ 57 | 58 | $subhead Source$$ 59 | The file $xref/max.cpp/$$ contains the 60 | C++ source code for this operation. 61 | 62 | $end 63 | -------------------------------------------------------------------------------- /omh/min.omh: -------------------------------------------------------------------------------- 1 | /* 2 | $begin min$$ 3 | $index min$$ 4 | $spell 5 | Matlab 6 | ublas 7 | $$ 8 | 9 | $section Minimum Element of a Matrix$$ 10 | 11 | $head Syntax$$ 12 | $table 13 | $bold Matlab or Octave$$ $cnext 14 | $syntax%%y% = min( min(%x%) )%$$ 15 | $rnext 16 | $bold C++$$ $cnext 17 | $syntax%%y% = min(%x%)%$$ 18 | $tend 19 | 20 | $head Matlab or Octave$$ 21 | If $italic x$$ is an $latex m \times n$$ matrix, 22 | $syntax% 23 | %y% = min( min(%x%) ) 24 | %$$ 25 | sets $italic y$$ to the minimum element in the matrix $italic x$$. 26 | If either $italic m$$ or $italic n$$ is equal to one, 27 | $syntax% 28 | %y% = min(%x%) 29 | %$$ 30 | also sets $italic y$$ to the minimum element in the matrix $italic x$$. 31 | 32 | $subhead Example$$ 33 | $code 34 | $verbatim%mat/min_ok.m%5%$$ 35 | $$ 36 | 37 | 38 | $head C++$$ 39 | The following is a C++ implementation of the 40 | Matlab or Octave syntax above: 41 | $syntax% 42 | %y% = min(%x%) 43 | %$$ 44 | where $italic x$$ is a non-empty 45 | (non-zero row and column size) 46 | ublas $code matrix$$ object 47 | and $italic y$$ is a $code double$$. 48 | 49 | $subhead Example$$ 50 | $code 51 | $verbatim%cpp/min_ok.cpp%5%$$ 52 | $$ 53 | 54 | 55 | $children% 56 | lib/min.cpp 57 | %$$ 58 | 59 | $subhead Source$$ 60 | The file $xref/min.cpp/$$ contains the 61 | C++ source code for this operation. 62 | 63 | $end 64 | */ 65 | -------------------------------------------------------------------------------- /omh/mztuni.omh: -------------------------------------------------------------------------------- 1 | $begin mztuni$$ 2 | $spell 3 | hpp 4 | cpp 5 | namespace 6 | Marsaglia 7 | Zaman 8 | Tsang 9 | mztuni 10 | uni 11 | $$ 12 | $index C++, uniform random number generator$$ 13 | $index random, C++ uniform generator$$ 14 | $index uniform, C++ random generator$$ 15 | $index generator, C++ uniform random$$ 16 | 17 | $section C++ Marsaglia Zaman Tsang Uniform Random Number Generator$$ 18 | 19 | $head Syntax$$ 20 | $codei%include 21 | %$$ 22 | $icode%s% = mztuni_seed(%seed%) 23 | %$$ 24 | $icode%u% = mztuni()%$$ 25 | 26 | $head Purpose$$ 27 | This C++ random number generator is portable; to be specific 28 | the same seed gives same results for different computers. 29 | In addition, it is easy to implement and thereby avoids having to link 30 | in a large random number library; see 31 | $cref mztuni.cpp$$. 32 | 33 | $head Reference$$ 34 | $index Marsaglia$$ 35 | $index Zaman$$ 36 | $index Tsang$$ 37 | G. Marsaglia, A. Zaman, and W.W. Tsang, 38 | Toward a universal random number generator, 39 | Statistics & Probability Letters, 40 | Volume 8, 41 | pages 35-39, 42 | 1990. 43 | 44 | $subhead Typo$$ 45 | $index typo$$ 46 | The Fortran version in Table 2 of the reference above contains the following 47 | line of code: 48 | $codep 49 | CD = 7654321./167777216. 50 | $$ 51 | Using this value in $code mztuni$$ 52 | does not produce the values in Table 4 of the reference. 53 | Replacing the value above 54 | by the value below does produce the values in Table 4: 55 | $codep 56 | CD = 7654321./16777216. 57 | $$ 58 | 59 | 60 | $head include$$ 61 | Because it can be used without the rest of $code mat2cpp$$, this routine 62 | is not in the $code mat2cpp$$ namespace and has its own include file. 63 | 64 | $head seed$$ 65 | 66 | $subhead seed >= 2$$ 67 | The argument $icode seed$$ has prototype 68 | $codei% 69 | size_t %seed% 70 | %$$ 71 | and is used to seed the random number generator. 72 | The function call 73 | $codei% 74 | CALL RSTART(%I%, %J%, %K%, %L%) 75 | %$$ 76 | in the reference above corresponds to 77 | $codei% 78 | %seed% = (((%L%-1)*168 + %K%-1)*168 + %J%-1)*168 + %I% + 1 79 | %$$ 80 | Note the restriction that $icode I$$, $icode J$$, $icode K$$ and $icode L$$ 81 | are between $code 1$$ and $code 168$$ and not all equal to $code 1$$; i.e., 82 | $codei% 83 | 3 <= %seed% <= 796594177 84 | %$$ 85 | 86 | $subhead seed == 1$$ 87 | If the special case where $icode seed$$ is equal to one, 88 | the current time is used to choose the actual seed between three 89 | and 796594177. 90 | This actual seed is then used to seed the random number generator. 91 | 92 | $subhead seed == 0$$ 93 | In the special case where $icode seed$$ is equal to zero, 94 | no action is taken. 95 | This case is useful for retrieving the previous seed used 96 | to seed the random number generator. 97 | 98 | 99 | $head s$$ 100 | The return value $icode s$$ has prototype 101 | $codei% 102 | size_t %s% 103 | %$$ 104 | It is the previous seed that was used to seed the random number generator. 105 | If $icode seed > 0$$, 106 | the $icode s$$ corresponds to this call to $code mztuni_seed$$. 107 | 108 | $head u$$ 109 | The return value $icode u$$ has prototype 110 | $codei% 111 | double %u% 112 | %$$ 113 | It is sample of a random variable that is uniformly distributed between 114 | the values zero and one. 115 | 116 | $children% 117 | cpp/mztuni_ok.cpp% 118 | lib/mztuni.cpp 119 | %$$ 120 | $head Example$$ 121 | The file $cref mztuni_ok.cpp$$ 122 | contains an example and test of this routine. 123 | It returns true if it succeeds and false otherwise. 124 | 125 | $subhead Source$$ 126 | The file $cref mztuni.cpp$$ contains the 127 | C++ source code that implements $code mztuni$$. 128 | 129 | $end 130 | -------------------------------------------------------------------------------- /omh/ones.omh: -------------------------------------------------------------------------------- 1 | $begin ones$$ 2 | $index ones$$ 3 | $spell 4 | ublas 5 | Matlab 6 | mat2cpp 7 | $$ 8 | 9 | $section Matrix of Ones$$ 10 | 11 | $head Syntax$$ 12 | $table 13 | $bold Matlab or Octave$$ $cnext 14 | $syntax%%x% = ones(%m%, %n%)%$$ 15 | $rnext 16 | $bold C++$$ $cnext 17 | $syntax%scalar_matrix %x%(%m%, %n%, 1.)%$$ 18 | $tend 19 | 20 | $head Matlab or Octave$$ 21 | If $italic m$$ and $italic n$$ are integer scalars, 22 | the Matlab or Octave syntax 23 | $syntax% 24 | %x% = ones(%m%, %n%) 25 | %$$ 26 | sets $italic x$$ to an $latex m \times n$$ matrix with all 27 | its elements equal to one. 28 | 29 | $subhead Example$$ 30 | $code 31 | $verbatim%mat/ones_ok.m%5%$$ 32 | $$ 33 | 34 | 35 | $head C++$$ 36 | The corresponding C++ syntax is 37 | $syntax% 38 | scalar_matrix %x%(%m%, %n%, 1.) 39 | %$$ 40 | where $italic m$$ and $italic n$$ are $code size_t$$ objects 41 | and $italic x$$ is an $latex m \times n$$ ublas $code matrix$$ object. 42 | 43 | $subhead Example$$ 44 | $code 45 | $verbatim%cpp/ones_ok.cpp%5%$$ 46 | $$ 47 | 48 | $end 49 | -------------------------------------------------------------------------------- /omh/operation.omh: -------------------------------------------------------------------------------- 1 | $begin operation$$ 2 | 3 | $section Operations That Have Conversions$$ 4 | 5 | $head Grouped by Type$$ 6 | $childtable% 7 | omh/elementwise.omh% 8 | omh/matrixwise.omh% 9 | omh/scalar_valued.omh% 10 | omh/other.omh 11 | %$$ 12 | 13 | $head Alphabetical Order$$ 14 | $table 15 | $rref add$$ 16 | $rref cholesky$$ 17 | $rref diag_prod$$ 18 | $rref element_div$$ 19 | $rref element_prod$$ 20 | $rref element_unary$$ 21 | $rref extend$$ 22 | $rref matrix_div$$ 23 | $rref matrix_prod$$ 24 | $rref max$$ 25 | $rref min$$ 26 | $rref ones$$ 27 | $rref rand$$ 28 | $rref randn$$ 29 | $rref scalar_div$$ 30 | $rref scalar_prod$$ 31 | $rref size$$ 32 | $rref slice$$ 33 | $rref sub$$ 34 | $rref sum$$ 35 | $rref transpose$$ 36 | $rref zeros$$ 37 | $tend 38 | 39 | $end 40 | -------------------------------------------------------------------------------- /omh/other.omh: -------------------------------------------------------------------------------- 1 | $begin other$$ 2 | $spell 3 | $$ 4 | 5 | 6 | $section Other Operations$$ 7 | 8 | $childtable% 9 | omh/extend.omh% 10 | omh/ones.omh% 11 | omh/rand.omh% 12 | omh/randn.omh% 13 | omh/size.omh% 14 | omh/slice.omh% 15 | omh/zeros.omh 16 | %$$ 17 | 18 | 19 | $end 20 | -------------------------------------------------------------------------------- /omh/rand.omh: -------------------------------------------------------------------------------- 1 | /* 2 | $begin rand$$ 3 | $index rand, matrix$$ 4 | $index matrix, rand$$ 5 | $spell 6 | mztuni 7 | ublas 8 | std 9 | srand 10 | Matlab 11 | $$ 12 | 13 | $section Uniform Random Matrix$$ 14 | 15 | $head Syntax$$ 16 | $table 17 | $bold Matlab or Octave$$ $cnext 18 | $syntax%%x% = rand(%m%, %n%)%$$ 19 | $rnext 20 | $bold C++$$ $cnext 21 | $syntax%%x% = rand(%m%, %n%)%$$ 22 | $tend 23 | 24 | $head Matlab or Octave$$ 25 | If $italic m$$ and $italic n$$ are integer scalars, 26 | the Matlab or Octave syntax 27 | $syntax% 28 | %x% = rand(%m%, %n%) 29 | %$$ 30 | sets $italic x$$ to an $latex m \times n$$ matrix each entry 31 | drawn from an independent uniform distribution on the interval 32 | $latex [0,1]$$. 33 | 34 | $subhead Example$$ 35 | $code 36 | $verbatim%mat/rand_ok.m%5%$$ 37 | $$ 38 | 39 | $head C++$$ 40 | The following is a C++ implementation of the 41 | Matlab or Octave $code rand$$ function with the syntax: 42 | $syntax% 43 | %x% = rand(%m%, %n%) 44 | %$$ 45 | where $italic m$$ and $italic n$$ are $code size_t$$ values 46 | and $italic x$$ is an $latex m \times n$$ 47 | ublas $code matrix$$ object. 48 | 49 | $subhead std::rand$$ 50 | If you want to use $code std::rand$$ to generate the random values, 51 | you must first call the standard library function 52 | $syntax% 53 | std::srand(%seed%) 54 | %$$ 55 | where $italic seed$$ is an $code unsigned int$$ 56 | to initialize the random number generator. 57 | 58 | $subhead mztuni$$ 59 | If there is a previous call of the form 60 | $cref/mztuni(seed)/mztuni/seed/$$ with $icode%seed% > 1%$$, 61 | the $cref mztuni$$ random number generator will be used 62 | (instead of $code std::rand$$). 63 | 64 | $subhead Example$$ 65 | $code 66 | $verbatim%cpp/rand_ok.cpp%5%$$ 67 | $$ 68 | 69 | $children% 70 | lib/rand.cpp 71 | %$$ 72 | 73 | $subhead Source$$ 74 | The file $xref/rand.cpp/$$ contains the 75 | C++ source code for this operation. 76 | 77 | $end 78 | */ 79 | -------------------------------------------------------------------------------- /omh/randn.omh: -------------------------------------------------------------------------------- 1 | /* 2 | $begin randn$$ 3 | $spell 4 | randn 5 | ublas 6 | std 7 | srand 8 | Matlab 9 | $$ 10 | $index randn, matrix$$ 11 | $index matrix, randn$$ 12 | $index normal, simulate$$ 13 | $index Gaussian, simulate$$ 14 | $index simulate, normal$$ 15 | 16 | $section Normal Random Matrix$$ 17 | 18 | $head Syntax$$ 19 | $table 20 | $bold Matlab or Octave$$ $cnext 21 | $syntax%%x% = randn(%m%, %n%)%$$ 22 | $rnext 23 | $bold C++$$ $cnext 24 | $syntax%%x% = randn(%m%, %n%)%$$ 25 | $tend 26 | 27 | $head Matlab or Octave$$ 28 | If $italic m$$ and $italic n$$ are integer scalars, 29 | the Matlab or Octave syntax 30 | $syntax% 31 | %x% = randn(%m%, %n%) 32 | %$$ 33 | sets $italic x$$ to an $latex m \times n$$ matrix each entry 34 | drawn from an independent normally distribution with mean 35 | zero and variance one. 36 | 37 | $subhead Example$$ 38 | $code 39 | $verbatim%mat/randn_ok.m%5%$$ 40 | $$ 41 | 42 | $head C++$$ 43 | The following is a C++ implementation of the 44 | Matlab or Octave $code randn$$ function with the syntax: 45 | $syntax% 46 | %x% = randn(%m%, %n%) 47 | %$$ 48 | where $italic m$$ and $italic n$$ are $code size_t$$ values 49 | and $italic x$$ is an $latex m \times n$$ 50 | ublas $code matrix$$ object. 51 | You must first call the standard library function 52 | $syntax% 53 | std::srand(%seed%) 54 | %$$ 55 | where $italic seed$$ is an $code unsigned int$$ 56 | to initialize the random number generator $code std::rand()$$. 57 | 58 | $subhead Example$$ 59 | $code 60 | $verbatim%cpp/randn_ok.cpp%5%$$ 61 | $$ 62 | 63 | $children% 64 | lib/randn.cpp 65 | %$$ 66 | 67 | $subhead Source$$ 68 | The file $xref/randn.cpp/$$ contains the 69 | C++ source code for this operation. 70 | 71 | $end 72 | */ 73 | -------------------------------------------------------------------------------- /omh/scalar.omh: -------------------------------------------------------------------------------- 1 | $begin scalar$$ 2 | $index scalar$$ 3 | $spell 4 | ublas 5 | Matlab 6 | $$ 7 | 8 | $section Convert Matrix to Scalar$$ 9 | 10 | $head Syntax$$ 11 | $table 12 | $bold Matlab or Octave$$ $cnext 13 | $syntax%%x%$$ 14 | $rnext 15 | $bold C++$$ $cnext 16 | $syntax%%y% = scalar(%x%)%$$ 17 | $tend 18 | 19 | $head Matlab or Octave$$ 20 | If $italic x$$ is an $latex 1 \times 1$$ matrix, 21 | $syntax% 22 | %x% 23 | %$$ 24 | behaves as if it were a scalar. 25 | 26 | $subhead Example$$ 27 | $code 28 | $verbatim%mat/scalar_ok.m%5%$$ 29 | $$ 30 | 31 | 32 | $head C++$$ 33 | The following is a C++ implementation of 34 | conversion from a matrix to a scalar: 35 | $syntax% 36 | %y% = scalar(%x%) 37 | %$$ 38 | where $italic x$$ is a $latex 1 \times 1$$ 39 | ublas $code matrix$$ 40 | and $italic y$$ is a $code double$$. 41 | 42 | $subhead Example$$ 43 | $code 44 | $verbatim%cpp/scalar_ok.cpp%5%$$ 45 | $$ 46 | 47 | $children% 48 | lib/scalar.cpp 49 | %$$ 50 | 51 | $subhead Source$$ 52 | The file $xref/scalar.cpp/$$ contains the 53 | C++ source code for this operation. 54 | 55 | $end 56 | -------------------------------------------------------------------------------- /omh/scalar_div.omh: -------------------------------------------------------------------------------- 1 | $begin scalar_div$$ 2 | $spell 3 | ublas 4 | div 5 | Matlab or Octave 6 | C++ 7 | $$ 8 | 9 | $index scalar, division$$ 10 | $index division, scalar$$ 11 | $index scalar_div$$ 12 | 13 | $section Scalar Division$$ 14 | 15 | $head Syntax$$ 16 | $table 17 | $bold Matlab or Octave$$ $cnext 18 | $syntax%%z% = %x% / %y%$$ 19 | $rnext 20 | $bold C++$$ $cnext 21 | $syntax%%z% = %x% / %y%$$ 22 | $rnext $cnext 23 | $syntax%%z% /= %y%$$ 24 | $tend 25 | 26 | $head Matlab or Octave$$ 27 | $italic x$$ is an $latex m \times n$$ matrix 28 | and $italic y$$ is a scalar, 29 | $syntax% 30 | %z% = %x% / %y% 31 | %$$ 32 | set $italic z$$ to the $latex m \times n$$ matrix with 33 | the $latex (i,j)$$ element given by 34 | $latex \[ 35 | z_{i,j} = x_{i,j} / y 36 | \] $$ 37 | 38 | $subhead Example$$ 39 | $code 40 | $verbatim%mat/scalar_div_ok.m%5%$$ 41 | $$ 42 | 43 | 44 | $head C++$$ 45 | The corresponding C++ syntax is 46 | $syntax% 47 | %z% = %x% / %y% 48 | %$$ 49 | where $italic x$$, $italic z$$ are 50 | ublas $code matrix$$ objects of size $latex m \times n$$ and 51 | $italic y$$ is a $code double$$ object. 52 | In the case where $italic x$$ and $italic z$$ are the same matrix, 53 | you should use 54 | $syntax% 55 | %z% /= %y% 56 | %$$ 57 | 58 | $subhead Example$$ 59 | $code 60 | $verbatim%cpp/scalar_div_ok.cpp%5%$$ 61 | $$ 62 | 63 | $end 64 | -------------------------------------------------------------------------------- /omh/scalar_prod.omh: -------------------------------------------------------------------------------- 1 | $begin scalar_prod$$ 2 | $index scalar, product$$ 3 | $index product, scalar$$ 4 | $index scalar_prod$$ 5 | $spell 6 | ublas 7 | Matlab or Octave 8 | C++ 9 | $$ 10 | 11 | $section Scalar Product$$ 12 | 13 | $head Syntax$$ 14 | $table 15 | $bold Matlab or Octave$$ $cnext 16 | $syntax%%z% = %x% * %y%$$ 17 | $rnext 18 | $bold C++$$ $cnext 19 | $syntax%%z% = %x% * %y%$$ 20 | $rnext $cnext 21 | $syntax%%z% *= %y%$$ 22 | $tend 23 | 24 | $head Matlab or Octave$$ 25 | If $italic x$$ is an $latex m \times n$$ matrix 26 | and $italic y$$ is a scalar 27 | $syntax% 28 | %z% = %x% * %y% 29 | %$$ 30 | set $italic z$$ to the $latex m \times n$$ matrix with 31 | the $latex (i,j)$$ element given by 32 | $latex \[ 33 | z_{i,j} = x_{i,j} * y 34 | \] $$ 35 | 36 | $subhead Example$$ 37 | $code 38 | $verbatim%mat/scalar_prod_ok.m%5%$$ 39 | $$ 40 | 41 | 42 | $head C++$$ 43 | The corresponding C++ syntax is 44 | $syntax% 45 | %z% = %x% * %y% 46 | %$$ 47 | where $italic x$$, $italic z$$ are 48 | ublas $code matrix$$ objects of size $latex m \times n$$ and 49 | $italic y$$ is a $code double$$ object. 50 | In the case where $italic x$$ and $italic z$$ are the same matrix, 51 | you should use 52 | $syntax% 53 | %z% *= %y% 54 | %$$ 55 | 56 | $subhead Example$$ 57 | $code 58 | $verbatim%cpp/scalar_prod_ok.cpp%5%$$ 59 | $$ 60 | 61 | $end 62 | -------------------------------------------------------------------------------- /omh/scalar_valued.omh: -------------------------------------------------------------------------------- 1 | $begin scalar_valued$$ 2 | $spell 3 | matrixwise 4 | $$ 5 | 6 | 7 | $section Scalar Valued Operations$$ 8 | 9 | $childtable% 10 | omh/max.omh% 11 | omh/min.omh% 12 | omh/scalar.omh% 13 | omh/sum.omh 14 | %$$ 15 | 16 | 17 | $end 18 | -------------------------------------------------------------------------------- /omh/size.omh: -------------------------------------------------------------------------------- 1 | $begin size$$ 2 | $index size$$ 3 | $spell 4 | ublas 5 | Matlab 6 | mat2cpp 7 | $$ 8 | 9 | $section Matrix Size$$ 10 | 11 | $head Syntax$$ 12 | $table 13 | $bold Matlab or Octave$$ $cnext 14 | $syntax%[%m%, %n%] = size(%x%)%$$ 15 | $rnext 16 | $bold C++$$ $cnext 17 | $syntax%%m% = %x%.size1()%$$ 18 | $rnext $cnext 19 | $syntax%%n% = %x%.size2()%$$ 20 | $tend 21 | 22 | $head Matlab or Octave$$ 23 | If $italic x$$ is a matrix, 24 | the Matlab or Octave syntax 25 | $syntax% 26 | [%m%, %n%] = size(%x%) 27 | %$$ 28 | returns the row dimension $italic m$$ and column dimension $italic n$$ 29 | of the matrix $italic x$$. 30 | 31 | $subhead Example$$ 32 | $code 33 | $verbatim%mat/size_ok.m%5%$$ 34 | $$ 35 | 36 | $head C++$$ 37 | The corresponding C++ syntax is 38 | $syntax% 39 | %m% = %x%.size1() 40 | %n% = %x%.size2() 41 | %$$ 42 | where $italic x$$ is a ublas $code matrix$$ and 43 | $italic m$$, $italic n$$ are $code size_t$$ objects. 44 | 45 | $subhead Example$$ 46 | $code 47 | $verbatim%cpp/size_ok.cpp%5%$$ 48 | $$ 49 | 50 | $end 51 | -------------------------------------------------------------------------------- /omh/slice.omh: -------------------------------------------------------------------------------- 1 | $begin slice$$ 2 | $index slice$$ 3 | $index sub-matrix$$ 4 | $spell 5 | ublas 6 | Matlab 7 | len 8 | mat2cpp 9 | $$ 10 | 11 | $section Matrix Slices$$ 12 | 13 | $head Syntax$$ 14 | $table 15 | $bold Matlab or Octave$$ $cnext 16 | $syntax%%r% = %r_start% : %r_stride% : %r_limit%$$ 17 | $rnext $cnext 18 | $syntax%%c% = %c_start% : %c_stride% : %c_limit%$$ 19 | $rnext $cnext 20 | $syntax%%x%(%r%, %c%)%$$ 21 | 22 | $rnext 23 | $bold C++$$ $cnext 24 | $syntax%slice %r% = slice(%r_start%, %r_stride%, %r_len%)%$$ 25 | $rnext $cnext 26 | $syntax%slice %c% = slice(%c_start%, %c_stride%, %c_len%)%$$ 27 | $rnext $cnext 28 | $syntax%matrix_slice_double (%x%, %r%, %c%)%$$ 29 | $tend 30 | 31 | $head Matlab or Octave$$ 32 | If $italic x$$, $italic y$$ are matrices, 33 | $italic r$$, $italic c$$ are vectors, 34 | and the other values below are integer scalars, 35 | $syntax% 36 | %r% = %r_start% : %r_stride% : %r_limit% 37 | %c% = %c_start% : %c_stride% : %c_limit% 38 | %x%(%r%, %c%) = %y% 39 | %$$ 40 | Sets $italic r$$ to the vector of integers starting at 41 | $italic r_start$$, incrementing by $italic r_stride$$ and all of which 42 | are less than or equal $italic r_limit$$. 43 | It sets $italic c$$ in a similar fashion and then sets 44 | the corresponding sub-set of rows and columns of $italic x$$ to the 45 | matrix in $italic y$$. 46 | 47 | $subhead Example$$ 48 | $code 49 | $verbatim%mat/slice_ok.m%5%$$ 50 | $$ 51 | 52 | 53 | $head C++$$ 54 | The corresponding C++ syntax is 55 | $syntax% 56 | slice %r% = slice(%r_start%, %r_stride%, %r_len%) 57 | slice %c% = slice(%c_start%, %c_stride%, %c_len%) 58 | matrix_slice_double (%x%, %r%, %c%) = %y% 59 | %$$ 60 | where $italic x$$ is a ublas $code matrix$$ object, 61 | $italic r$$, $italic c$$ are 62 | $code mat2cpp$$ $code matrix_slice_double$$ objects, 63 | and the other values are $code size_t$$ scalars. 64 | 65 | 66 | $subhead Example$$ 67 | $code 68 | $verbatim%cpp/slice_ok.cpp%5%$$ 69 | $$ 70 | 71 | $end 72 | -------------------------------------------------------------------------------- /omh/sub.omh: -------------------------------------------------------------------------------- 1 | /* 2 | $begin sub$$ 3 | $index element, subtract$$ 4 | $index subtract, element$$ 5 | $spell 6 | ublas 7 | Elementwise 8 | Matlab or Octave 9 | C++ 10 | $$ 11 | 12 | $section Elementwise Subtraction$$ 13 | 14 | $head Syntax$$ 15 | $table 16 | $bold Matlab or Octave$$ $cnext 17 | $syntax%%z% = %x% - %y%$$ 18 | $rnext 19 | $bold C++$$ $cnext 20 | $syntax%%z% = %x% - %y%$$ 21 | $tend 22 | 23 | $head Matlab or Octave$$ 24 | If $italic x$$ and $italic y$$ are a $latex m \times n$$ matrices, 25 | $syntax% 26 | %z% = %x% - %y% 27 | %$$ 28 | sets $italic z$$ to the $latex m \times n$$ matrix with 29 | the $latex (i,j)$$ element given by 30 | $latex \[ 31 | z_{i,j} = x_{i,j} - y_{i,j} 32 | \] $$ 33 | 34 | $subhead Example$$ 35 | $code 36 | $verbatim%mat/sub_ok.m%5%$$ 37 | $$ 38 | 39 | 40 | $head C++$$ 41 | The corresponding C++ syntax is 42 | $syntax% 43 | %z% = %x% - %y% 44 | %$$ 45 | where $italic x$$, $italic y$$, and $italic z$$ are 46 | ublas $code matrix$$ objects 47 | with size $latex m \times n$$. 48 | 49 | $subhead Example$$ 50 | $code 51 | $verbatim%cpp/sub_ok.cpp%5%$$ 52 | $$ 53 | 54 | $end 55 | */ 56 | -------------------------------------------------------------------------------- /omh/sum.omh: -------------------------------------------------------------------------------- 1 | /* 2 | $begin sum$$ 3 | $index sum$$ 4 | $spell 5 | ublas 6 | Matlab 7 | $$ 8 | 9 | $section Sum The Elements of a Matlab$$ 10 | 11 | $head Syntax$$ 12 | $table 13 | $bold Matlab or Octave$$ $cnext 14 | $syntax%%y% = sum( sum(%x%) )%$$ 15 | $rnext 16 | $bold C++$$ $cnext 17 | $syntax%%y% = sum(%x%)%$$ 18 | $tend 19 | 20 | $head Matlab or Octave$$ 21 | If $italic x$$ is an $latex m \times n$$ matrix, 22 | $syntax% 23 | %y% = sum( sum(%x%) ) 24 | %$$ 25 | sets $italic y$$ to the sum of the elements in the matrix $italic x$$. 26 | If either $italic m$$ or $italic n$$ is equal to one, 27 | $syntax% 28 | %y% = sum(%x%) 29 | %$$ 30 | also sets $italic y$$ to the sum of the elements in the matrix $italic x$$. 31 | 32 | $subhead Example$$ 33 | $code 34 | $verbatim%mat/sum_ok.m%5%$$ 35 | $$ 36 | 37 | 38 | $head C++$$ 39 | The following is a C++ implementation of the 40 | Matlab or Octave syntax above: 41 | $syntax% 42 | %y% = sum(%x%) 43 | %$$ 44 | where $italic x$$ is an $latex m \times n$$ ublas $code matrix$$ 45 | and $italic y$$ is a $code double$$. 46 | 47 | $subhead Example$$ 48 | $code 49 | $verbatim%cpp/sum_ok.cpp%5%$$ 50 | $$ 51 | 52 | $children% 53 | lib/sum.cpp 54 | %$$ 55 | 56 | $subhead Source$$ 57 | The file $xref/sum.cpp/$$ contains the 58 | C++ source code for this operation. 59 | 60 | $end 61 | */ 62 | -------------------------------------------------------------------------------- /omh/transpose.omh: -------------------------------------------------------------------------------- 1 | /* 2 | $begin transpose$$ 3 | $index transpose$$ 4 | $spell 5 | ublas 6 | Matlab or Octave 7 | C++ 8 | trans 9 | $$ 10 | 11 | $section Matrix Transpose$$ 12 | 13 | $head Syntax$$ 14 | $table 15 | $bold Matlab or Octave$$ $cnext 16 | $syntax%%y% = %x%'%$$ 17 | $rnext 18 | $bold C++$$ $cnext 19 | $syntax%%y% = trans(%x%)%$$ 20 | $tend 21 | 22 | $head Matlab or Octave$$ 23 | If $italic x$$ is an $latex m \times n$$ matrix, 24 | $syntax% 25 | %y% = %x%' 26 | %$$ 27 | sets $italic y$$ to the $latex n \times m$$ matrix with 28 | $latex \[ 29 | y_{j,i} = x_{i,j} 30 | \] $$ 31 | 32 | $subhead Example$$ 33 | $code 34 | $verbatim%mat/transpose_ok.m%5%$$ 35 | $$ 36 | 37 | 38 | $head C++$$ 39 | The corresponding C++ syntax is 40 | $syntax% 41 | %y% = trans(%x%) 42 | %$$ 43 | where $italic x$$ and $italic y$$ are 44 | ublas $code matrix$$ objects with sizes 45 | $latex m \times n$$ and $latex n \times m$$ respectively. 46 | 47 | $subhead Example$$ 48 | $code 49 | $verbatim%cpp/transpose_ok.cpp%5%$$ 50 | $$ 51 | 52 | $end 53 | */ 54 | -------------------------------------------------------------------------------- /omh/zeros.omh: -------------------------------------------------------------------------------- 1 | $begin zeros$$ 2 | $index zeros$$ 3 | $spell 4 | ublas 5 | Matlab 6 | mat2cpp 7 | $$ 8 | 9 | $section Matrix of Zeros$$ 10 | 11 | $head Syntax$$ 12 | $table 13 | $bold Matlab or Octave$$ $cnext 14 | $syntax%%x% = zeros(%m%, %n%)%$$ 15 | $rnext 16 | $bold C++$$ $cnext 17 | $syntax%zero_matrix %x%(%m%, %n%)%$$ 18 | $tend 19 | 20 | $head Matlab or Octave$$ 21 | If $italic m$$ and $italic n$$ are integer scalars, 22 | the Matlab or Octave syntax 23 | $syntax% 24 | %x% = zeros(%m%, %n%) 25 | %$$ 26 | sets $italic x$$ to an $latex m \times n$$ matrix with all 27 | its elements equal to zero. 28 | 29 | $subhead Example$$ 30 | $code 31 | $verbatim%mat/zeros_ok.m%5%$$ 32 | $$ 33 | 34 | 35 | $head C++$$ 36 | The corresponding C++ syntax is 37 | $syntax% 38 | zero_matrix %x%(%m%, %n%) 39 | %$$ 40 | where $italic m$$ and $italic n$$ are $code size_t$$ objects 41 | and $italic x$$ is an $latex m \times n$$ ublas $code matrix$$ object. 42 | 43 | 44 | $subhead Example$$ 45 | $code 46 | $verbatim%cpp/zeros_ok.cpp%5%$$ 47 | $$ 48 | 49 | $end 50 | -------------------------------------------------------------------------------- /run_omhelp.sh: -------------------------------------------------------------------------------- 1 | #! /bin/bash -e 2 | # ----------------------------------------------------------------------------- 3 | if [ "$0" != './run_omhelp.sh' ] 4 | then 5 | echo './run_omhelp.sh: must be run from top source directory' 6 | exit 1 7 | fi 8 | # ----------------------------------------------------------------------------- 9 | project_home_page='https://github.com/bradbell/mat2cpp' 10 | if [ -e doc ] 11 | then 12 | rm -r doc 13 | fi 14 | mkdir doc 15 | cd doc 16 | if ! omhelp ../doc.omh \ 17 | -noframe -debug -image_link "$project_home_page" 18 | then 19 | echo 'Aborting, see OMhelp error message in omhelp.log' 20 | exit 1 21 | fi 22 | if grep 'OMhelp Warning:' ../omhelp.log 23 | then 24 | echo 'See the complete warning message in omhelp.log' 25 | exit 1 26 | fi 27 | # ----------------------------------------------------------------------------- 28 | echo 'run_omhelp.sh: OK' 29 | exit 0 30 | --------------------------------------------------------------------------------