Lincoln’s blog

CS 371P - Object Oriented Programming

Week #12

Posted in Weekly Post on November 15, 2009 by Lincoln

Inheritance, inheritance, inheritance…

I feel like this week was very important in my object oriented education.  We are learning about inheritance between parent and child classes.    I am surprised that it has taken this long to really indepth discuss the subject, for it is the primary motive of Object Oriented programming.

I like the virtual keyword.  It is quite useful when building objects with inheritance, and was quite useful in the Darwin project we just turned in.

Week #11

Posted in Weekly Post on November 8, 2009 by Lincoln

I felt like this week was spent mostly talking about the Darwin project that is due next week.

I like that we are being forced to draw the UML model for this project.  I have been looking at job postings and a lot of the software ones ask for knowledge of UML modeling of which I have little experience with.

Week #10

Posted in Weekly Post on November 8, 2009 by Lincoln

I realized today that I have fallen behind a week somewhere, so this will be about Sudoku.

The Sudoku program we had to write was actually a little harder to implement than I expected.  I think my partner and I simply got into a snowball where we were writing code to try and fix bad code, when we should have just gone back and rethought our whole implementation.

As far as class topics, the whole concept of implicit and explicit conversion is kind of interesting.  I am not very familiar with the concept, yet I can see how it can be pretty useful to manipulate, yet kinda difficult to control if you aren’t sure about what you are doing in your class implementations.

Week #9

Posted in Weekly Post on November 2, 2009 by Lincoln

Pretty slow week.

Got my test grade back.  Did not do nearly as well as I had hoped, but I had felt I had dropped the ball anyways. I definitely do not like that the tests are not returned in class and the solutions are not covered in class time.

Week #8

Posted in Weekly Post on October 18, 2009 by Lincoln

So week 8 brought us the second test.  I was a little curious as to why we were given two weeks to do Project 5.  It seems that the two weeks was alloted to give us time to study for the test, but it seems like it would have made more sense to just have the project due the week before.  Instead I felt like I was having to worry about the test and the project all week.  Also, I wasn’t too keen on having a test the day before the Texas-OU game, haha.

The test itself was another good one.  I felt like I didn’t have enough time for this one, but that was mostly because I didn’t read one of the problems correctly and had to basically redo it.  Then the last problem had a few parts that I was writing at the last second that I feel like I probably made some dumb mistakes on that I probably could have avoided if I had a few more minutes to read the code I was writing.

Overall good week.

Project 5 (Allocator)

Posted in Uncategorized on October 18, 2009 by Lincoln
  • predicted number of hours to complete: 4
  • actual number of hours to complete: 4
  • TestAllocator.out

Week #7

Posted in Weekly Post on October 12, 2009 by Lincoln

This past week was rather slow.  We had no program due this week, although I have started the Allocator program.

Lectures are starting to get more and more involved.  We are now needing to think about the allocation and deallocation of variables within different scopes, and what happens on the heap when data structures are copied.  I may need to start studying more, because I am starting to think the second test on Friday will be quite a bit harder than the last one…

That’s about it for this week!

Week #6

Posted in Weekly Post on October 11, 2009 by Lincoln

Week 6 was pretty straight forward.  The project this week was to implement a few functions from MatLab.  The skeleton given by Downing was pretty nice since we simply had to fill in the meat of the functions.  It was very simple to just split the functions up between myself and my partner and finish the project that way.

I enjoyed this project because of that.  I like well defined problems.  The only functions that we felt were not well defined were the concat_rows and concat_cols, which was pretty easy to figure out.

Anyways, that’s about it for this week.  I need to keep up better with the readings…

Project 4 (MatLab)

Posted in Projects on October 4, 2009 by Lincoln

This week we had to implement a few functions from Matlab.  I actually kind of liked this project because it was very easy to work on with a partner.  My partner this week as John Barry and we were both able to finish the coding in a relatively short amount of time once we knew what was going on.

  • predicted number of hours to complete: 5
  • actual number of hours to complete: 5
  • TestMatLab.out

TestMatLab.h

Posted in Uncategorized on October 2, 2009 by Lincoln

// ——————————–
// projects/c++/matlab/TestMatLab.h
// Copyright (C) 2009
// Glenn P. Downing
// ——————————–

#ifndef TestMatLab_h
#define TestMatLab_h

// ——–
// includes
// ——–

#include <algorithm> // equal

//#include “cppunit/TestFixture.h”             // TestFixture
//#include “cppunit/extensions/HelperMacros.h” // CPPUNIT_TEST, CPPUNIT_TEST_SUITE, CPPUNIT_TEST_SUITE

//#include “/opt/local/var/macports/software/cppunit/1.12.1_0/opt/local/include/cppunit/TestFixture.h”             // TestFixture
//#include “/opt/local/var/macports/software/cppunit/1.12.1_0/opt/local/include/cppunit/extensions/HelperMacros.h” // CPPUNIT_TEST, CPPUNIT_TEST_SUITE, CPPUNIT_TEST_SUITE

#include “MatLab.h”
#include “cppunit/TestFixture.h”
#include “cppunit/extensions/HelperMacros.h”

// ———-
// TestMatLab
// ———-

struct TestMatLab : CppUnit::TestFixture {

/**
* pretty_print - prints matrix. remove prior to hand-in
*/

void pretty_print( const ml::matrix_type& m )
{
using namespace std;
const ml::size_type rows = m.size();
const ml::size_type cols = m[0].size();
cout << endl;
cout << endl;
for (ml::size_type i = 0; i != rows; ++i)
{
for (ml::size_type j = 0; j != cols; ++ j)
cout << m[i][j] << ” ” ;
cout << endl;
}
cout << endl;
cout << endl;

}

// —–
// tests
// —–

// —————————
// operator + (matrix, int)
// —————————

void test_plus_int_1 () {
using namespace ml;
const ml::size_type r    = 2;
const ml::size_type c    = 3;
const ml::size_type s    = r * c;
const int           a[s] = {2, 3, 4, 5, 6, 7};
const int           b[s] = {3, 4, 5, 6, 7, 8};
ml::matrix_type     m    = ml::make(a, r, c);
ml::matrix_type     n    = ml::make(b, r, c);
m = m + 1;
CPPUNIT_ASSERT(m == n);
}

// —————————
// operator + (matrix, matrix)
// —————————
void test_plus_mat_1 () {
using namespace ml;
const ml::size_type r    = 2;
const ml::size_type c    = 3;
const ml::size_type s    = r * c;
const int       a[s] = {2, 3, 4, 5, 6, 7};
const int       b[s] = {3, 4, 5, 6, 7, 8};
const int    d[s] = {5, 7, 9, 11, 13, 15};
ml::matrix_type     m    = ml::make(a, r, c);
ml::matrix_type     n    = ml::make(b, r, c);
ml::matrix_type     o    = ml::make(d, r, c);
m = m + n;
CPPUNIT_ASSERT(m == o);
}

// —————————
// operator - (matrix, int)
// —————————
void test_minus_int_1 () {
using namespace ml;
const ml::size_type r    = 2;
const ml::size_type c    = 3;
const ml::size_type s    = r * c;
const int           a[s] = {2, 3, 4, 5, 6, 7};
const int           b[s] = {1, 2, 3, 4, 5, 6};
ml::matrix_type     m    = ml::make(a, r, c);
ml::matrix_type     n    = ml::make(b, r, c);
m = m - 1;
CPPUNIT_ASSERT(m == n);
}
// —————————
// operator - (matrix, matrix)
// —————————
void test_minus_mat_1 () {
using namespace ml;
const ml::size_type r    = 2;
const ml::size_type c    = 3;
const ml::size_type s    = r * c;
const int       a[s] = {3, 4, 5, 6, 7, 8};
const int       b[s] = {2, -3, 1, 6, 6, 9};
const int    d[s] = {1, 7, 4, 0, 1, -1};
ml::matrix_type     m    = ml::make(a, r, c);
ml::matrix_type     n    = ml::make(b, r, c);
ml::matrix_type     o    = ml::make(d, r, c);
m = m - n;
CPPUNIT_ASSERT(m == o);
}
// —————————
// operator * (matrix, int)
// —————————
void test_mult_int_1 () {
using namespace ml;
const ml::size_type r    = 2;
const ml::size_type c    = 3;
const ml::size_type s    = r * c;
const int       a[s] = {3, 4, 2, 6, 0, 1};
const int       b[s] = {9, 12, 6, 18, 0, 3};
ml::matrix_type     m    = ml::make(a, r, c);
ml::matrix_type     n    = ml::make(b, r, c);
m = m * 3;
CPPUNIT_ASSERT(m == n);
}
// —————————
// operator * (matrix, matrix)
// —————————
void test_mult_mat_1 () {
using namespace ml;
const ml::size_type rm = 4;
const ml::size_type cm = 4;
const ml::size_type sm = rm * cm;
const ml::size_type rn = 4;
const ml::size_type cn = 4;
const ml::size_type sn = rn * cn;
const ml::size_type sa = rm * cn;
const int a[sm] = {1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16};
const int b[sn] = {16,15,14,13,12,11,10,9,8,7,6,5,4,3,2,1};
const int ans[sa] = {80,70,60,50,240,214,188,162,400,358,316,274,560,502,444,386};
ml::matrix_type m = ml::make(a, rm, cm);
ml::matrix_type n = ml::make(b, rn, cn);
ml::matrix_type answer = ml::make(ans, rm, cn);
m = m * n;
CPPUNIT_ASSERT(m == answer);
}

void test_mult_mat_2 () {
using namespace ml;
const ml::size_type rm = 2;
const ml::size_type cm = 4;
const ml::size_type sm = rm * cm;
const ml::size_type rn = 4;
const ml::size_type cn = 3;
const ml::size_type sn = rn * cn;
const ml::size_type sa = rm * cn;
const int a[sm] = {2, 0, -1, 1, 1, 2, 0, 1};
const int b[sn] = {1, 5, -7, 1, 1, 0, 0, -1, 1, 2, 0, 0};
const int ans[sa] = {4, 11, -15, 5, 7, -7};
ml::matrix_type m = ml::make(a, rm, cm);
ml::matrix_type n = ml::make(b, rn, cn);
ml::matrix_type answer = ml::make(ans, rm, cn);
m = m * n;
CPPUNIT_ASSERT(m == answer);
}

void test_mult_mat_3 () {
using namespace ml;
const ml::size_type rm = 7;
const ml::size_type cm = 3;
const ml::size_type sm = rm * cm;
const ml::size_type rn = 3;
const ml::size_type cn = 5;
const ml::size_type sn = rn * cn;
const ml::size_type sa = rm * cn;
const int a[sm] = {-10,-10,-12,5,-3,18,7,6,5,3,2,1,4,5,20,43,-1,0,-6,3,21};
const int b[sn] = {1,-4,3,5,9,8,-6,-3,0,60,15,-20,4,4,5};
const int ans[sa] ={    -270,340,-48,-98,-750,
251,-362,96,97,-45,
130,-164,23,55,448,
34,-44,7,19,152,
344,-446,77,100,436,
35,-166,132,215,327,
333,-414,57,54,231};
ml::matrix_type m = ml::make(a, rm, cm);
ml::matrix_type n = ml::make(b, rn, cn);
ml::matrix_type answer = ml::make(ans, rm, cn);
m = m * n;
CPPUNIT_ASSERT(m == answer);
}

// ————–
// concat rows
// ————–
void test_concat_rows_1 () {
using namespace ml;
const ml::size_type r    = 2;
const ml::size_type c    = 3;
const ml::size_type s    = r * c;
const int       a[s] = {3, 4, 5, 6, 7, 8};
const int       b[s] = {2, -3, 1, 6, 6, 9};
const int    d[s*2] = {3, 4, 5, 6, 7, 8, 2, -3, 1, 6, 6, 9};
ml::matrix_type     m    = ml::make(a, r, c);
ml::matrix_type     n    = ml::make(b, r, c);
ml::matrix_type     o    = ml::make(d, r*2, c);
m = concat_rows(m, n);

CPPUNIT_ASSERT(m == o);
}

// ————–
// concat columns
// ————–
void test_concat_cols_1 () {
using namespace ml;
const ml::size_type r    = 2;
const ml::size_type c    = 3;
const ml::size_type s    = r * c;
const int       a[s] = {3, 4, 5, 6, 7, 8};
const int       b[s] = {2, -3, 1, 6, 6, 9};
const int    d[s*2] = {3,4,5,2,-3,1,6,7,8,6,6,9};
ml::matrix_type     m    = ml::make(a, r, c);
ml::matrix_type     n    = ml::make(b, r, c);
ml::matrix_type     o    = ml::make(d, r, c*2);
m = concat_cols(m, n);
CPPUNIT_ASSERT(m == o);
}

// —-
// diag
// —-
void test_diag_1 () {
using namespace ml;
const ml::size_type r    = 3;
const ml::size_type c    = 3;
const ml::size_type s    = r * c;
const int       a[s] = {3, 4, 5, 6, 7, 8, 9, 10, 11};
vector_type b(3);
b[0] = 3; b[1] = 7; b[2] = 11;
ml::matrix_type     m    = ml::make(a, r, c);
CPPUNIT_ASSERT(b == diag(m));
}
// —-
// eye
// —-

void test_eye_1 () {
using namespace ml;
const ml::size_type r    = 3;
const ml::size_type c    = 3;
const ml::size_type s    = r * c;
const int       a[s] = {1, 0, 0, 0, 1, 0, 0, 0, 1};
ml::matrix_type     m    = ml::make(a, r, c);

CPPUNIT_ASSERT(m == eye(r, c));
}

// —-
// make
// —-

void test_make () {
const ml::size_type r    = 2;
const ml::size_type c    = 3;
const ml::size_type s    = r * c;
const int           a[s] = {2, 3, 4, 5, 6, 7};
ml::matrix_type     m    = ml::make(a, r, c);
for (ml::size_type i = 0; i != r; ++i)
for (ml::size_type j = 0; j != c; ++ j)
CPPUNIT_ASSERT(m[i][j] == a[(i * c) + j]);}

// —-
// ones
// —-
void test_ones_1 () {
const ml::size_type r    = 2;
const ml::size_type c    = 3;
const ml::size_type s    = r * c;
const int           a[s] = {1, 1, 1, 1, 1, 1};
ml::matrix_type     m    = ml::ones(r, c);
for (ml::size_type i = 0; i != r; ++i)
for (ml::size_type j = 0; j != c; ++ j)
CPPUNIT_ASSERT(m[i][j] == a[(i * c) + j]);

//pretty_print(m);

}

void test_ones_2 () {
const ml::size_type r    = 1;
const ml::size_type c    = 1;
const ml::size_type s    = r * c;
const int           a[s] = {1};
ml::matrix_type     m    = ml::ones(r, c);
for (ml::size_type i = 0; i != r; ++i)
for (ml::size_type j = 0; j != c; ++ j)
CPPUNIT_ASSERT(m[i][j] == a[(i * c) + j]);

}

// case: requested matrix is zero
void test_ones_3 () {
ml::matrix_type     result    = ml::ones(0, 0);
CPPUNIT_ASSERT( result.size() == 0 );
}

// —-
// rand
// —-

// —–
// slice
// —–

/**
* cth column
*/

void test_slice_col_1 () {
const ml::size_type r    = 4;
const ml::size_type c    = 3;
const ml::size_type s    = r * c;
const int           a[s] = {2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13};
const int           b[r] = {2, 5, 8, 11};
ml::matrix_type     m    = ml::make(a, r, c);
ml::vector_type     v    = ml::slice_col(m, 0);

CPPUNIT_ASSERT( v.size() == r );
CPPUNIT_ASSERT(std::equal(v.begin(), v.end(), b));
}
void test_slice_col_2 () {
const ml::size_type r    = 4;
const ml::size_type c    = 3;
const ml::size_type s    = r * c;
const int           a[s] = {2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13};
const int           b[r] = {3, 6, 9, 12};
ml::matrix_type     m    = ml::make(a, r, c);
ml::vector_type     v    = ml::slice_col(m, 1);

CPPUNIT_ASSERT( v.size() == r );
CPPUNIT_ASSERT(std::equal(v.begin(), v.end(), b));
}

void test_slice_col_3 () {
const ml::size_type r    = 4;
const ml::size_type c    = 3;
const ml::size_type s    = r * c;
const int           a[s] = {2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13};
const int           b[r] = {4, 7, 10, 13};
ml::matrix_type     m    = ml::make(a, r, c);
ml::vector_type     v    = ml::slice_col(m, 2);

CPPUNIT_ASSERT( v.size() == r );
CPPUNIT_ASSERT(std::equal(v.begin(), v.end(), b));
}

void test_slice_col_4 () {
const ml::size_type r    = 1;
const ml::size_type c    = 1;
const ml::size_type s    = r * c;
const int           a[s] = {13};
const int           b[r] = {13};
ml::matrix_type     m    = ml::make(a, r, c);
ml::vector_type     v    = ml::slice_col(m, 0);

CPPUNIT_ASSERT( v.size() == r );
CPPUNIT_ASSERT(std::equal(v.begin(), v.end(), b));
}

// array is empty
void test_slice_col_5 () {

ml::matrix_type     m;
ml::vector_type     v    = ml::slice_col(m, 0);

CPPUNIT_ASSERT( v.size() == 0 );
}

/**
* rth row
*/

void test_slice_row_1 () {
const ml::size_type r    = 4;
const ml::size_type c    = 3;
const ml::size_type s    = r * c;
const int           a[s] = {2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13};
const int           b[c] = {2, 3, 4};
ml::matrix_type     m    = ml::make(a, r, c);
ml::vector_type     v    = ml::slice_row(m, 0);

CPPUNIT_ASSERT( v.size() == c );
CPPUNIT_ASSERT(std::equal(v.begin(), v.end(), b));
}

void test_slice_row_2 () {
const ml::size_type r    = 4;
const ml::size_type c    = 3;
const ml::size_type s    = r * c;
const int           a[s] = {2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13};
const int           b[c] = {5, 6, 7};
ml::matrix_type     m    = ml::make(a, r, c);
ml::vector_type     v    = ml::slice_row(m, 1);

CPPUNIT_ASSERT( v.size() == c );
CPPUNIT_ASSERT(std::equal(v.begin(), v.end(), b));
}

void test_slice_row_3 () {
const ml::size_type r    = 4;
const ml::size_type c    = 3;
const ml::size_type s    = r * c;
const int           a[s] = {2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13};
const int           b[c] = {8, 9, 10};
ml::matrix_type     m    = ml::make(a, r, c);
ml::vector_type     v    = ml::slice_row(m, 2);

CPPUNIT_ASSERT( v.size() == c );
CPPUNIT_ASSERT(std::equal(v.begin(), v.end(), b));
}

void test_slice_row_4 () {
const ml::size_type r    = 4;
const ml::size_type c    = 3;
const ml::size_type s    = r * c;
const int           a[s] = {2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13};
const int           b[c] = {11, 12, 13};
ml::matrix_type     m    = ml::make(a, r, c);
ml::vector_type     v    = ml::slice_row(m, 3);

CPPUNIT_ASSERT( v.size() == c );
CPPUNIT_ASSERT(std::equal(v.begin(), v.end(), b));
}

void test_slice_row_5 () {
const ml::size_type r    = 1;
const ml::size_type c    = 10;
const ml::size_type s    = r * c;
const int           a[s] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
const int           b[c] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
ml::matrix_type     m    = ml::make(a, r, c);
ml::vector_type     v    = ml::slice_row(m, 0);

CPPUNIT_ASSERT( v.size() == c );
CPPUNIT_ASSERT(std::equal(v.begin(), v.end(), b));
}

void test_slice_row_6 () {
const ml::size_type r    = 1;
const ml::size_type c    = 1;
const ml::size_type s    = r * c;
const int           a[s] = {37};
const int           b[c] = {37};
ml::matrix_type     m    = ml::make(a, r, c);
ml::vector_type     v    = ml::slice_row(m, 0);

CPPUNIT_ASSERT( v.size() == c );
CPPUNIT_ASSERT(std::equal(v.begin(), v.end(), b));
}

// ——–
// sum_cols
// ——–

void test_sum_cols_1 () {
const ml::size_type r    = 2;
const ml::size_type c    = 3;
const ml::size_type s    = r * c;
const int           a[s] = {2, 3, 4, 5, 6, 7};
const int           b[c] = {7, 9, 11};
ml::matrix_type     m    = ml::make(a, r, c);
ml::vector_type     v    = ml::sum_cols(m);

CPPUNIT_ASSERT(std::equal(v.begin(), v.end(), b));
}
void test_sum_cols_2 () {
const ml::size_type r    = 1;
const ml::size_type c    = 1;
const ml::size_type s    = r * c;
const int           a[s] = {7};
const int           b[c] = {7};
ml::matrix_type     m    = ml::make(a, r, c);
ml::vector_type     v    = ml::sum_cols(m);

CPPUNIT_ASSERT(std::equal(v.begin(), v.end(), b));
}
void test_sum_cols_3 () {
const ml::size_type r    = 3;
const ml::size_type c    = 1;
const ml::size_type s    = r * c;
const int           a[s] = {8, 11, 13};
const int           b[c] = {(8+11+13)};
ml::matrix_type     m    = ml::make(a, r, c);
ml::vector_type     v    = ml::sum_cols(m);

CPPUNIT_ASSERT(std::equal(v.begin(), v.end(), b));
}
void test_sum_cols_4 () {
const ml::size_type r    = 4;
const ml::size_type c    = 3;
const ml::size_type s    = r * c;
const int           a[s] = {2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13};
const int           b[c] = {(7+8+11), (9+9+12), (11+10+13)};
ml::matrix_type     m    = ml::make(a, r, c);
ml::vector_type     v    = ml::sum_cols(m);

CPPUNIT_ASSERT(std::equal(v.begin(), v.end(), b));
}
// ——–
// sum_rows
// ——–

void test_sum_rows_1 () {
const ml::size_type r    = 2;
const ml::size_type c    = 3;
const ml::size_type s    = r * c;
const int           a[s] = {2, 3, 4, 5, 6, 7};
const int           b[r] = {9, 18};
ml::matrix_type     m    = ml::make(a, r, c);
ml::vector_type     v    = ml::sum_rows(m);

CPPUNIT_ASSERT(std::equal(v.begin(), v.end(), b));

}
void test_sum_rows_2 () {
const ml::size_type r    = 1;
const ml::size_type c    = 1;
const ml::size_type s    = r * c;
const int           a[s] = {9};
const int           b[r] = {9};
ml::matrix_type     m    = ml::make(a, r, c);
ml::vector_type     v    = ml::sum_rows(m);

CPPUNIT_ASSERT(std::equal(v.begin(), v.end(), b));
}
void test_sum_rows_3 () {
const ml::size_type r    = 4;
const ml::size_type c    = 3;
const ml::size_type s    = r * c;
const int           a[s] = {2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13};
const int           b[r] = {9, 18, 27, 36};
ml::matrix_type     m    = ml::make(a, r, c);
ml::vector_type     v    = ml::sum_rows(m);

CPPUNIT_ASSERT(std::equal(v.begin(), v.end(), b));
}
void test_sum_rows_4 () {
const ml::size_type r    = 1;
const ml::size_type c    = 5;
const ml::size_type s    = r * c;
const int           a[s] = {2, 3, 4, 5, 6};
const int           b[r] = {(2+3+4+5+6)};
ml::matrix_type     m    = ml::make(a, r, c);
ml::vector_type     v    = ml::sum_rows(m);

CPPUNIT_ASSERT(std::equal(v.begin(), v.end(), b));
}

// ———
// transpose
// ———

void test_transpose_1 () {
const ml::size_type r    = 2;
const ml::size_type c    = 3;
const ml::size_type s    = r * c;

const int           a[s] = {2, 3, 4, 5, 6, 7};
const int                b[s] = {2, 5, 3, 6, 4, 7};

ml::matrix_type     m    = ml::make(a, r, c);
ml::matrix_type result      = ml::transpose(m);

for (ml::size_type i = 0; i != c; ++i)
for (ml::size_type j = 0; j != r; ++ j)
CPPUNIT_ASSERT(result[i][j] == b[(i * r) + j]);
CPPUNIT_ASSERT((  m.size() == result[0].size() ) && ( m[0].size() == result.size()  ));
}
void test_transpose_2 () {
const ml::size_type r    = 1;
const ml::size_type c    = 1;
const ml::size_type s    = r * c;

const int           a[s] = {7};
const int                b[s] = {7};

ml::matrix_type     m    = ml::make(a, r, c);
ml::matrix_type result      = ml::transpose(m);

for (ml::size_type i = 0; i != c; ++i)
for (ml::size_type j = 0; j != r; ++ j)
CPPUNIT_ASSERT(result[i][j] == b[(i * r) + j]);
CPPUNIT_ASSERT((  m.size() == result[0].size() ) && ( m[0].size() == result.size()  ));
}

void test_transpose_3 () {
const ml::size_type r    = 3;
const ml::size_type c    = 4;
const ml::size_type s    = r * c;

const int           a[s] = {2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13};
const int                b[s] = {2, 6, 10, 3, 7, 11, 4, 8, 12, 5, 9, 13};

ml::matrix_type     m    = ml::make(a, r, c);
ml::matrix_type result      = ml::transpose(m);

for (ml::size_type i = 0; i != c; ++i)
for (ml::size_type j = 0; j != r; ++ j)
CPPUNIT_ASSERT(result[i][j] == b[(i * r) + j]);
CPPUNIT_ASSERT((  m.size() == result[0].size() ) && ( m[0].size() == result.size()  ));
}

void test_transpose_4 () {
const ml::size_type r    = 1;
const ml::size_type c    = 12;
const ml::size_type s    = r * c;

const int           a[s] = {2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13};

ml::matrix_type     m    = ml::make(a, r, c);
ml::matrix_type result      = ml::transpose(m);

for (ml::size_type i = 0; i != c; ++i)
for (ml::size_type j = 0; j != r; ++ j)
CPPUNIT_ASSERT(result[i][j] == a[(i * r) + j]);
CPPUNIT_ASSERT((  m.size() == result[0].size() ) && ( m[0].size() == result.size()  ));
}

// case of empty matrix
void test_transpose_5 () {

ml::matrix_type m;
ml::matrix_type result      = ml::transpose(m);

CPPUNIT_ASSERT((  m.size() == result.size() ) );
}

// ———
// tri
// ———

/**
* lower triangular
*/

void test_tril_1 () {
const ml::size_type r    = 2;
const ml::size_type c    = 2;
const ml::size_type s    = r * c;
const int           a[s] = {2, 3, 4, 5};
const int             b[s] = {2, 0, 4, 5};
ml::matrix_type     m    = ml::make(a, r, c);
ml::matrix_type     result    = ml::tril(m);

for (ml::size_type i = 0; i != r; ++i)
for (ml::size_type j = 0; j != c; ++ j)
CPPUNIT_ASSERT(result[i][j] == b[(i * c) + j]);
}

void test_tril_2 () {
const ml::size_type r    = 1;
const ml::size_type c    = 1;
const ml::size_type s    = r * c;
const int           a[s] = {99};
const int             b[s] = {99};
ml::matrix_type     m    = ml::make(a, r, c);
ml::matrix_type     result    = ml::tril(m);

for (ml::size_type i = 0; i != r; ++i)
for (ml::size_type j = 0; j != c; ++ j)
CPPUNIT_ASSERT(result[i][j] == b[(i * c) + j]);
}
void test_tril_3 () {
const ml::size_type r    = 3;
const ml::size_type c    = 3;
const ml::size_type s    = r * c;
const int           a[s] = {2, 3, 4, 5, 6, 7, 8, 9, 1};
const int             b[s] = {2, 0, 0, 5, 6, 0, 8, 9, 1};
ml::matrix_type     m    = ml::make(a, r, c);
ml::matrix_type     result    = ml::tril(m);

for (ml::size_type i = 0; i != r; ++i)
for (ml::size_type j = 0; j != c; ++ j)
CPPUNIT_ASSERT(result[i][j] == b[(i * c) + j]);
}
//matlab doesn’t require a square matrix…
void test_tril_4 () {
const ml::size_type r    = 2;
const ml::size_type c    = 3;
const ml::size_type s    = r * c;
const int           a[s] = {2, 3, 4, 5, 6, 7};
const int             b[s] = {2, 0, 0, 5, 6, 0};
ml::matrix_type     m    = ml::make(a, r, c);
ml::matrix_type     result    = ml::tril(m);

for (ml::size_type i = 0; i != r; ++i)
for (ml::size_type j = 0; j != c; ++ j)
CPPUNIT_ASSERT(result[i][j] == b[(i * c) + j]);
}
//matlab doesn’t require a square matrix…
void test_tril_5 () {
const ml::size_type r    = 3;
const ml::size_type c    = 1;
const ml::size_type s    = r * c;
const int           a[s] = {2, 3, 4};
const int             b[s] = {2, 3, 4};
ml::matrix_type     m    = ml::make(a, r, c);
ml::matrix_type     result    = ml::tril(m);

for (ml::size_type i = 0; i != r; ++i)
for (ml::size_type j = 0; j != c; ++ j)
CPPUNIT_ASSERT(result[i][j] == b[(i * c) + j]);
}
//matlab doesn’t require a square matrix…
void test_tril_6 () {
const ml::size_type r    = 1;
const ml::size_type c    = 3;
const ml::size_type s    = r * c;
const int           a[s] = {2, 3, 4};
const int             b[s] = {2, 0, 0};
ml::matrix_type     m    = ml::make(a, r, c);
ml::matrix_type     result    = ml::tril(m);

for (ml::size_type i = 0; i != r; ++i)
for (ml::size_type j = 0; j != c; ++ j)
CPPUNIT_ASSERT(result[i][j] == b[(i * c) + j]);
}
// test case: passed matrix is empty
void test_tril_7 () {

ml::matrix_type m;
ml::matrix_type result    = ml::tril(m);

CPPUNIT_ASSERT( result.size() == 0 );
}

/**
* upper triangular
*/

void test_triu_1 () {
const ml::size_type r    = 2;
const ml::size_type c    = 2;
const ml::size_type s    = r * c;
const int           a[s] = {2, 3, 4, 5};
const int             b[s] = {2, 3, 0, 5};
ml::matrix_type     m    = ml::make(a, r, c);
ml::matrix_type     result    = ml::triu(m);

for (ml::size_type i = 0; i != r; ++i)
for (ml::size_type j = 0; j != c; ++ j)
CPPUNIT_ASSERT(result[i][j] == b[(i * c) + j]);
}

void test_triu_2 () {
const ml::size_type r    = 1;
const ml::size_type c    = 1;
const ml::size_type s    = r * c;
const int           a[s] = {2};
const int             b[s] = {2};
ml::matrix_type     m    = ml::make(a, r, c);
ml::matrix_type     result    = ml::triu(m);

for (ml::size_type i = 0; i != r; ++i)
for (ml::size_type j = 0; j != c; ++ j)
CPPUNIT_ASSERT(result[i][j] == b[(i * c) + j]);
}
void test_triu_3 () {
const ml::size_type r    = 3;
const ml::size_type c    = 3;
const ml::size_type s    = r * c;
const int           a[s] = {2, 3, 4, 5, 6, 7, 8, 9, 1};
const int             b[s] = {2, 3, 4, 0, 6, 7, 0, 0, 1};
ml::matrix_type     m    = ml::make(a, r, c);
ml::matrix_type     result    = ml::triu(m);

for (ml::size_type i = 0; i != r; ++i)
for (ml::size_type j = 0; j != c; ++ j)
CPPUNIT_ASSERT(result[i][j] == b[(i * c) + j]);
}

void test_triu_4 () {
const ml::size_type r    = 2;
const ml::size_type c    = 3;
const ml::size_type s    = r * c;
const int           a[s] = {2, 3, 4, 5, 6, 7};
const int             b[s] = {2, 3, 4, 0, 6, 7};
ml::matrix_type     m    = ml::make(a, r, c);
ml::matrix_type     result    = ml::triu(m);

for (ml::size_type i = 0; i != r; ++i)
for (ml::size_type j = 0; j != c; ++ j)
CPPUNIT_ASSERT(result[i][j] == b[(i * c) + j]);
}
void test_triu_5 () {
const ml::size_type r    = 1;
const ml::size_type c    = 3;
const ml::size_type s    = r * c;
const int           a[s] = {2, 3, 4};
const int             b[s] = {2, 3, 4};
ml::matrix_type     m    = ml::make(a, r, c);
ml::matrix_type     result    = ml::triu(m);

for (ml::size_type i = 0; i != r; ++i)
for (ml::size_type j = 0; j != c; ++ j)
CPPUNIT_ASSERT(result[i][j] == b[(i * c) + j]);
}
void test_triu_6 () {
const ml::size_type r    = 3;
const ml::size_type c    = 1;
const ml::size_type s    = r * c;
const int           a[s] = {2, 3, 4};
const int             b[s] = {2, 0, 0};
ml::matrix_type     m    = ml::make(a, r, c);
ml::matrix_type     result    = ml::triu(m);

for (ml::size_type i = 0; i != r; ++i)
for (ml::size_type j = 0; j != c; ++ j)
CPPUNIT_ASSERT(result[i][j] == b[(i * c) + j]);
}
// case: matrix is empty
void test_triu_7 () {

ml::matrix_type     m;
ml::matrix_type     result    = ml::triu(m);

assert(m.size() == 0);
}
// —–
// zeros
// —–

void test_zeros_1 () {
const ml::size_type r    = 2;
const ml::size_type c    = 3;
const ml::size_type s    = r * c;
const int           a[s] = {0, 0, 0, 0, 0, 0};
ml::matrix_type     m    = ml::zeros(r, c);
for (ml::size_type i = 0; i != r; ++i)
for (ml::size_type j = 0; j != c; ++ j)
CPPUNIT_ASSERT(m[i][j] == a[(i * c) + j]);

// pretty_print(m);

}

void test_zeros_2 () {
const ml::size_type r    = 1;
const ml::size_type c    = 1;
const ml::size_type s    = r * c;
const int           a[s] = {0};
ml::matrix_type     m    = ml::zeros(r, c);
for (ml::size_type i = 0; i != r; ++i)
for (ml::size_type j = 0; j != c; ++ j)
CPPUNIT_ASSERT(m[i][j] == a[(i * c) + j]);

}
// case: requested matrix is zero
void test_zeros_3 () {
ml::matrix_type     result    = ml::zeros(0, 0);
CPPUNIT_ASSERT( result.size() == 0 );
}

// —–
// suite
// —–

CPPUNIT_TEST_SUITE(TestMatLab);

CPPUNIT_TEST(test_plus_int_1);
CPPUNIT_TEST(test_plus_mat_1);
CPPUNIT_TEST(test_minus_int_1);
CPPUNIT_TEST(test_minus_mat_1);
CPPUNIT_TEST(test_mult_int_1);
CPPUNIT_TEST(test_mult_mat_1);
CPPUNIT_TEST(test_mult_mat_2);
CPPUNIT_TEST(test_mult_mat_3);
CPPUNIT_TEST(test_diag_1);
CPPUNIT_TEST(test_eye_1);
CPPUNIT_TEST(test_concat_rows_1);
CPPUNIT_TEST(test_concat_cols_1);

CPPUNIT_TEST(test_make);

CPPUNIT_TEST(test_slice_col_1);
CPPUNIT_TEST(test_slice_col_2);
CPPUNIT_TEST(test_slice_col_3);
CPPUNIT_TEST(test_slice_col_4);

CPPUNIT_TEST(test_slice_row_1);
CPPUNIT_TEST(test_slice_row_2);
CPPUNIT_TEST(test_slice_row_3);
CPPUNIT_TEST(test_slice_row_4);
CPPUNIT_TEST(test_slice_row_5);
CPPUNIT_TEST(test_slice_row_6);

CPPUNIT_TEST(test_sum_cols_1);
CPPUNIT_TEST(test_sum_cols_2);
CPPUNIT_TEST(test_sum_cols_3);
CPPUNIT_TEST(test_sum_cols_4);

CPPUNIT_TEST(test_sum_rows_1);
CPPUNIT_TEST(test_sum_rows_2);
CPPUNIT_TEST(test_sum_rows_3);
CPPUNIT_TEST(test_sum_rows_4);

CPPUNIT_TEST(test_zeros_1);
CPPUNIT_TEST(test_zeros_2);
CPPUNIT_TEST(test_zeros_3);

CPPUNIT_TEST(test_ones_1);
CPPUNIT_TEST(test_ones_2);
CPPUNIT_TEST(test_ones_3);

CPPUNIT_TEST(test_triu_1);
CPPUNIT_TEST(test_triu_2);
CPPUNIT_TEST(test_triu_3);
CPPUNIT_TEST(test_triu_4);
CPPUNIT_TEST(test_triu_5);
CPPUNIT_TEST(test_triu_6);
CPPUNIT_TEST(test_triu_7);

CPPUNIT_TEST(test_tril_1);
CPPUNIT_TEST(test_tril_2);
CPPUNIT_TEST(test_tril_3);
CPPUNIT_TEST(test_tril_4);
CPPUNIT_TEST(test_tril_5);
CPPUNIT_TEST(test_tril_6);
CPPUNIT_TEST(test_tril_7);

CPPUNIT_TEST(test_transpose_1);
CPPUNIT_TEST(test_transpose_2);
CPPUNIT_TEST(test_transpose_3);
CPPUNIT_TEST(test_transpose_4);
CPPUNIT_TEST(test_transpose_5);

CPPUNIT_TEST_SUITE_END();};

#endif // TestMatLab_h