how to generate Random matrix needed

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
#include <stdio.h>
#include <stdlib.h>
#include <omp.h>
#include <time.h>

double mul_vector_transpose_vector(double* v1, double* v2, int n) {
	int i;
	double result = 0;
#pragma omp parallel for reduction(+:result)
	for (i = 0; i < n; i++) {
		result += v1[i] * v2[i];
	}
	return result;
}

double* matrix_mul_vector(double** A, double* x, int n) {
	int i, j;
	double* result = (double*)malloc(n * sizeof(double));
#pragma omp parallel for
	for (i = 0; i < n; i++) {
		result[i] = mul_vector_transpose_vector(A[i], x, n);
	}
	return result;
}

double* negate_vector(double* g, int n) {
	int i;
	double* s = (double*)malloc(n * sizeof(double));
#pragma omp parallel for
	for (i = 0; i < n; i++) {
		s[i] = (-1) * g[i];
	}
	return s;
}

void copy_vector(double* v1, double* v2, int n) {
	int i;
#pragma omp parallel for
	for (i = 0; i < n; i++) {
		v1[i] = v2[i];
	}
}

double* scalar_mul_vector(double* s, double c, int n) {
	int i;
	double* result = (double*)malloc(n * sizeof(double));
#pragma omp parallel for
	for (i = 0; i < n; i++) {
		result[i] = c * s[i];
	}
	return result;
}

double* vector_add_vector(double* v1, double* v2, int n) {
	int i;
	double* result = (double*)malloc(n * sizeof(double));
#pragma omp parallel for
	for (i = 0; i < n; i++) {
		result[i] = v1[i] + v2[i];
	}
	return result;
}

double* vector_sub_vector(double* v1, double* v2, int n) {
	int i;
	double* result = (double*)malloc(n * sizeof(double));
#pragma omp parallel for
	for (i = 0; i < n; i++) {
		result[i] = v1[i] - v2[i];
	}
	return result;
}

int main() {
	int n, i, j, t = 20;
	printf("Enter the size of equation \n");
	scanf_s("%d", &n);
	double** A = (double**)malloc(n * sizeof(double*));
	double* b = (double*)malloc(n * sizeof(double));
	double* x = (double*)malloc(n * sizeof(double));
	for (i = 0; i < n; i++) {
		A[i] = (double*)malloc(n * sizeof(double));
	}

	printf("Enter the elements A \n");
	for (i = 0; i < n; i++) {
		for (j = 0; j < n; j++) {
			scanf_s("%lf", &A[i][j]);
		}
	}
	printf("Enter the elements of b \n");
	for (i = 0; i < n; i++) {
		scanf_s("%lf", &b[i]);
	}
	printf("Enter initial guess x \n");
	for (i = 0; i < n; i++) {
		scanf_s("%lf", &x[i]);
	}
	clock_t start, end;
	start = clock();
	double* Ax = matrix_mul_vector(A, x, n);
	double* g = vector_sub_vector(Ax, b, n);
	double* s = negate_vector(g, n);
	double numerator = (-1) * mul_vector_transpose_vector(s, g, n);
	double* tmp = matrix_mul_vector(A, s, n);
	double denominator = mul_vector_transpose_vector(s, tmp, n);
	double gamma = (numerator) / (denominator);
	double* s1 = scalar_mul_vector(s, gamma, n);
	x = vector_add_vector(x, s1, n);
	while (t--) {
		Ax = matrix_mul_vector(A, x, n);
		double* gnew = vector_sub_vector(Ax, b, n);
		numerator = mul_vector_transpose_vector(gnew, gnew, n);
		denominator = mul_vector_transpose_vector(g, g, n);
		double beta = (numerator) / (denominator);
		s1 = scalar_mul_vector(s, beta, n);
		double* neggnew = negate_vector(gnew, n);
		s = vector_add_vector(s1, neggnew, n);
		numerator = mul_vector_transpose_vector(s, neggnew, n);
		tmp = matrix_mul_vector(A, s, n);
		denominator = mul_vector_transpose_vector(s, tmp, n);
		gamma = (numerator) / (denominator);
		s1 = scalar_mul_vector(s, gamma, n);
		x = vector_add_vector(x, s1, n);
		copy_vector(g, gnew, n);
	}
	end = clock();
	printf("Solution  x = [");
	for (i = 0; i < n - 1; i++) {
		printf(" %lf,", x[i]);
	}
	printf(" %lf ]\n", x[n - 1]);
	printf("Parallel execution time %f \n", (double)(end - start) / CLOCKS_PER_SEC);
	return 0;
}


I want the matrix elements to be generated after the size of matrix has been entered please help
Last edited on
Please edit your post and put your code in code tags to preserve indentation:
http://www.cplusplus.com/forum/articles/16853/

You have a matrix A and a vector b.
Do they both need to be filled with random values?
What range of random values?
Yes both A and b should be randomly generated ,The matrix size till a 1000(1000x1000)
if you can help with these too
maxrows of matrix= 20
maxcols of matrix=30
Even when you say "please" they don't do it.
*sigh*
I give up.
Please dont give up i did now
Okay. :-)
But what should be the range of the values?
E.g.,
-1.0 to 1.0 ?
0.0 to 1000.0?

And is it different for A and b?

I don't understand what you need help with regarding maxrows and maxcols.
Last edited on
you see its an mxn matrix where m=columns and n=rows, range 0 - 1000
1
2
3
4
5
6
7
8
    srand(time(NULL)); // put this as the first line in main
    // ...

    // then when filling the matrix:
    for (int i = 0; i < n; i++)
        for (int j = 0; j < n; j++)
            // Values from 0.0 to 1000.0
            A[i][j] = (rand() / (double)RAND_MAX) * 1000.0;

I still don't get the mxn thing.
It's an nxn matrix (i.e., a square matrix) not mxn.
yes sorry its an nxn
instead of the matrix showing like
1
2
3
2 3 4
2 5 1
3 2 1


it is showing only in columns like
1
2
3
4
5
6
7
8
9
10
11
2
3
4

2
5
1

3
2
1

please how do i fix
Don't print a newline (endl or '\n') within the inner loop.
dutch and Ganado,thank you so much for the help,I really appreciate,and your patience,awesome.
Topic archived. No new replies allowed.