How to create a 2D matrix of integer in c# (mvvm) - visual-studio-2010

I want to create my own matrix class of integers.
in this class, i would fill automatically one part of my matrixes (all my matrixes will be symetric), update their size (add or remove), get their size.
Actually my code is :
public class MachineMatrix
{
private int size;
private int[,] matrix;
public MachineMatrix(int size)
{
this.size = size;
this.matrix=new int[this.size,this.size];
}
public void FillMatrix()
{
for (int i = 0; i < this.getSize()-1; i++)
{
for (int j = i; j < this.getSize() - 1; j++)
{
if (this.matrix[i, j] == 0)
matrix[i, j] = matrix[j, i];
else
matrix[j, i] = matrix[i, j];
}
matrix[i, i] = 0;
}
}
public MachineMatrix UpgradeSize(MachineMatrix mat, int nbSize)
{
int newSize = mat.getSize() + nbSize - 1;
MachineMatrix m = new MachineMatrix(newSize);
for (int i = 0; i < mat.getSize() - 1; i++)
{
for (int j = 0; j < mat.getSize() - 1; j++)
{
m[i, j] = mat[i, j];
}
for (int j = mat.getSize(); j<m.getSize()-1 ; j++)
{
m[i, j] = 0;
}
}
for (int i = mat.getSize(); i < m.getSize() - 1; i++)
{
for (int j = mat.getSize(); j < m.getSize() - 1; j++)
{
m[i, j] = 0;
}
}
return m;
}
public int getSize() { return this.size; }
}
My probleme is here :
Cannot apply indexing with int[] to an expression of type 'my-project.Models.MachineMatrix'
m[i, j] = mat[i, j];
m[i, j] = 0;
m[i, j] = 0;

I found the solution :-D
m.matrix[i, j] = mat.matrix[i, j];
m.matrix[i, j] = 0;
m.matrix[i, j] = 0;
But now, my problem is to display it...

Related

Simplex solver - issues with getting it working

I'm trying to write a simple simplex solver for linear optimization problems, but I'm having trouble getting it working. Every time I run it I get a vector subscript out of range (which is quite easy to find), but I think that its probably a core issue somewhere else in my impl.
Here is my simplex solver impl:
bool pivot(vector<vector<double>>& tableau, int row, int col) {
int n = tableau.size();
int m = tableau[0].size();
double pivot_element = tableau[row][col];
if (pivot_element == 0) return false;
for (int j = 0; j < m; j++) {
tableau[row][j] /= pivot_element;
}
for (int i = 0; i < n; i++) {
if (i != row) {
double ratio = tableau[i][col];
for (int j = 0; j < m; j++) {
tableau[i][j] -= ratio * tableau[row][j];
}
}
}
return true;
}
int simplex(vector<vector<double>>& tableau, vector<double>& basic, vector<double>& non_basic) {
int n = tableau.size() - 1;
int m = tableau[0].size() - 1;
while (true) {
int col = -1;
for (int j = 0; j < m; j++) {
if (tableau[n][j] > 0) {
col = j;
break;
}
}
if (col == -1) break;
int row = -1;
double min_ratio = numeric_limits<double>::infinity();
for (int i = 0; i < n; i++) {
if (tableau[i][col] > 0) {
double ratio = tableau[i][m] / tableau[i][col];
if (ratio < min_ratio) {
row = i;
min_ratio = ratio;
}
}
}
if (row == -1) return -1;
if (!pivot(tableau, row, col)) return -1;
double temp = basic[row];
basic[row] = non_basic[col];
non_basic[col] = temp;
}
return 1;
}

Which is faster, if inside a loop, or a just the loop?

I am trying to understand some concepts of performance in code.
If there is, for instance, a 2d boolean array with 5 trues inside it,
if we want to change all the elements in the array to false, which code would be faster?
number1:
for (i = 0; i < arr.length; i++) {
for (j = 0; j < arr[0].length; j++) {
arr[i][j] = false;
}
}
number2:
for (i = 0; i < arr.length; i++) {
for (j = 0; j < arr[0].length; j++) {
if (arr[i][j])
arr[i][j] = false;
}
}
You can measure it by using console.time() and console.timeEnd():
Number 1:
console.time('loop');
for (i = 0; i < arr.length; i++) {
for (j = 0; j < arr[0].length; j++) {
arr[i][j] = false;
}
}
console.timeEnd('loop');
loop: 0.046142578125ms
Number 2:
console.time('loop');
for (i = 0; i < arr.length; i++) {
for (j = 0; j < arr[0].length; j++) {
if (arr[i][j])
arr[i][j] = false;
}
}
console.timeEnd('loop');
loop: 0.031982421875ms
The array I used was arr[1][135] big and had 5 values in the second array that were true.
As you can see the loop with the if clause is faster.

Dividing n numbers into two groups each having sum less than equals k

There are n numbers, ranging 1-100. n ranges in 1-1000.
Another number k. Its bounds are 1 <= k <= 10^6
How to check if its possible to divide the given n numbers in two sets such that the sum of both group numbers is <=k.
I am looking for a high level implementation approach or an algorithm which will return true if the division is possible.
A DP based solution with a complexity of O(n*sum)
for (int i = 0; i < n; i++) {
sum += a[i];
}
int[][] dp = new int[n + 1][sum + 1];
for (int i = 0; i <= n; i++) {
dp[i][0] = 1;
}
for (int i = 0; i < n; i++) {
for (int j = 0; j <= sum; j++) {
dp[i + 1][j] = dp[i][j];
if (a[i] <= j) {
dp[i + 1][j] |= dp[i][j - a[i]];
}
}
}
for (int i = sum / 2; i >= 0; i--) {
if (dp[n][i] == 1) {
int x = sum - i;
if (x > k || i > k) {
System.out.println("NO");
} else {
System.out.println("YES");
}
break;
}
}

How to sort the boundary elements of a matrix in ascending order?

I've worked on this but when I'm entering the matrix, all the elements in the matrix are getting sorted! But I want to sort only the boundary elements in ascending order. Can some body please tell me my mistake?
int k,temp=0,sum=0;
k=n;
boolean b=true;
do
{
for(i=0;i<m;i++)
{
for(j=0;j<k-1;j++)
{
if(i!=0||j!=0)
{
if(A[i][j]>A[i][j+1])
{
temp=A[i][j];
A[i][j]=A[i][j+1];
A[i][j+1]=temp;
}
}
}
}
k-=1;
if(k<0)
b=false;
}while(b);
k=m;
do
{
for(i=0;i<k-1;i++)
{
for(j=0;j<n;j++)
{
if(i!=0||j!=0)
{
if(A[j][i]>A[j][i+1])
{
temp=A[j][i];
A[j][i]=A[j][i+1];
A[j][i+1]=temp;
}
}
}
}
k-=1;
if(k<0)
b=false;
}while(b);
System.out.println("REARRANGED MATRIX:");
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
{
System.out.print(A[i][j]+" ");
}
System.out.println();
}
Instead of using the condition 'if(i!=0||j!=0)' use 'if(i==0||i==2||j==0||j==2)'.This may solve the ambiguity you are having.Your mistake was that you have taken the number of rows and columns both to be greater than zero.the boundary elements are those where number of rows is 0 or 2 or number of columns is 0 or 2(by this I mean that only those elements which have coordinates with either i=0 or i=2 or j=0 or j=2 will be considered as boundary elements.matrix coordinates
I have one solution of this. I have used selection sort for doing this.At first I have sorted the matrix then displaying the boundary of the sorted array
import java.io.*;
class Boundary_Sorting
{
public static void main(String args[])throws IOException
{
InputStreamReader isr = new InputStreamReader(System.in);
BufferedReader br = new BufferedReader(isr);
System.out.println("Enter the rows of the matrix=");
int m = Integer.parseInt(br.readLine());
System.out.println("Enter the column of the matrix=");
int n = Integer.parseInt(br.readLine());
int a[][] = new int[m][n];
int i,j;
System.out.println("Enter the elements of the matrix: ");
for(i = 0; i < m; i++)
{
for(j = 0; j < n; j++)
{
a[i][j]=Integer.parseInt(br.readLine());
}
}
System.out.println("**********************");
System.out.println("The original matrix is");
System.out.println("**********************");
for(i = 0; i < m; i++)
{
for(j = 0; j < n; j++)
{
System.out.print(a[i][j]+"\t");
}
System.out.println();
}
int B[] = new int[m*n]; //creating a 1D Array of size 'r*c'
int x = 0;
for(i = 0; i < m; i++)
{
for(j = 0; j < n; j++)
{
B[x] = a[i][j];
x++;
}
}
/*Sorting the 1D Array in Ascending Order*/
int t = 0;
for(i = 0; i < (m * n) - 1; i++)
{
for(j = i + 1; j < (m * n); j++)
{
if(B[i] > B[j])
{
t = B[i];
B[i] = B[j];
B[j] = t;
}
}
}
/*Saving the sorted 1D Array back into the 2D Array */
x = 0;
for(i = 0; i < m; i++)
{
for(j = 0; j < n; j++)
{
a[i][j] = B[x];
x++;
}
}
/* Printing the sorted 2D Array */
System.out.println("**********************");
System.out.println("The Sorted Array:");
System.out.println("**********************");
for(i = 0; i < m; i++)
{
for(j = 0; j < n; j++)
{
System.out.print(a[i][j]+"\t");
}
System.out.println();
}
System.out.println("**********************");
System.out.println("The boundary elements of the matrix is=");
System.out.println("**********************");
for(i = 0; i < m; i++)
{
for(j = 0; j < n; j++)
{
if(i==0 || j==0 || i == m-1 || j == n-1) //condition for accessing boundary elements
System.out.print(a[i][j]+"\t");
else
System.out.print(" \t");
}
System.out.println();
}
}
}

Number of binary trees with equal values

There is array of values:
1 - n_1 times
2 - n_2 times
...
k - n_k times
How many trees with this nodes exist?
I create simple algorythm:
int get_count(const vector<int> n_i) {
if (n_i.size() <= 1) {
return 1;
} else {
int total_count = 0;
for (int i = 0; i < n_i.size(); ++i) {
vector<int> first;
vector<int> second;
for (int j = 0; j < i; ++j) {
first.push_back(n_i[j]);
}
if (n_i[i] != 1) {
second.push_back(n_i[i] - 1);
}
for (int j = i + 1; j < n_i.size(); ++j) {
second.push_back(n_i[j]);
}
total_count += (get_count(first) * get_count(second));
}
return total_count;
}
}
Because
#(n_1, n_2, ... n_k) = #(n_1 - 1, n_2, ..., n_k) + #(n_1) #(n_2 - 1, ... n_k) + ... + #(n_1, ..., n_k - 1)
and
#(0, n_i, n_j, ...) = #(n_i, n_j, ...)
But my code is so slow.
Is there a final formula via Cathalan's numbers, for example?
I guess that the problem can be split into calculating the number of permutations and calculating the number of binary trees of given size. I converted my initial recursive Java code (which gives up on n1=10,n2=10,n3=10) into this iterative one:
static int LIMIT = 1000;
static BigInteger[] numberOfBinaryTreesOfSize = numberOfBinaryTreesBelow(LIMIT);
static BigInteger[] numberOfBinaryTreesBelow(int m) {
BigInteger[] arr = new BigInteger[m];
arr[0] = BigInteger.ZERO;
arr[1] = arr[2] = BigInteger.ONE;
for (int n = 3; n < m; n++) {
BigInteger s = BigInteger.ZERO;
for (int i = 1; i < n; i++)
s = s.add(arr[i].multiply(arr[n - i]));
arr[n] = s;
}
return arr;
}
static BigInteger[] fac = facBelow(LIMIT);
static BigInteger[] facBelow(int m) {
BigInteger[] arr = new BigInteger[m];
arr[0] = arr[1] = BigInteger.ONE;
for (int i = 2; i < m; i++)
arr[i] = arr[i - 1].multiply(BigInteger.valueOf(i));
return arr;
}
static BigInteger getCountFast(int[] arr) {
// s: sum of n_i
int s = 0; for (int i = 0; i < arr.length; i++) { s += arr[i]; }
// p: number of permutations
BigInteger p = fac[s]; for (int i = 0; i < arr.length; i++) { p = p.divide(fac[arr[i]]); }
BigInteger count = p.multiply(numberOfBinaryTreesOfSize[s]);
return count;
}
public static void main(String[] args) {
System.out.println(getCountFast(new int[]{ 150, 150, 150, 150, 150 }));
}
The LIMIT limits the sum of the n_i. The above example takes about two seconds on my machine. Maybe it helps you with a C++ solution.

Resources