I have the following code for calculating the Eigen Vectors (left and right) of a Stochastic matrix in C (with gcc compiler):
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main()
{
double A[6][6] = {{0.2,.200000,0.2000000,0.000000,0.200000,0.200000},
{0.200000,0.000000,0.000000,0.000000,0.000000,0.800000},
{0.200000,0.000000,0.000000,0.800000,0.000000,0.000000},
{0.200000,0.000000,0.400000,0.000000,0.400000,0.000000},
{0.200000,0.000000,0.000000,0.800000,0.000000,0.000000},
{0.200000,0.800000,0.000000,0.000000,0.000000,0.000000}};
int n=6;
char Lchar='V';
char Rchar='V';
double *eigReal=(double *)malloc(n*sizeof(double *));
double *eigImag=(double *)malloc(n*sizeof(double *));
double vl[6][6], vr[6][6];
int one=1, two=6;
int lwork=6*n;
double *work=(double *)malloc(lwork*sizeof(double *));
int info;
dgeev_(&Lchar,&Rchar,&n,A,&n,eigReal,eigImag,
vl,&two,vr,&two,
work,&lwork,&info);
for(int i=0; i<6; i++)
{
printf("\n");
for(int j=0; j<6; j++)
printf("%f ", vr[i][j]);
}
for(int i=0; i<6; i++)
{
printf("\n");
for(int j=0; j<6; j++)
printf("%f ", vr[i][j]);
}
return 0;
}
Then I print vr and vl to get the following matrices (none of which seems to be correct)
(the ordering of the rows and columns might be wrong i.e it might be the eigen vectors are actually transpose).
0.476331 0.476331 0.264628 0.423405 0.264628 0.476331
0.852803 -0.213201 0.000000 -0.426401 -0.000000 -0.213201
0.000000 0.000000 -0.408248 0.816497 -0.408248 0.000000
0.000000 -0.534522 0.267261 0.534522 0.267261 -0.534522
0.000000 -0.706706 -0.013739 0.027477 -0.013739 0.706706
0.550263 -0.137566 0.540215 -0.275132 -0.540215 -0.137566
0.408248 0.408248 0.408248 0.408248 0.408248 0.408248
0.686304 -0.171576 -0.608494 -0.171576 0.265343 -0.171576
0.243660 -0.051773 -0.578693 0.517778 -0.578693 -0.009142
0.000000 -0.447214 0.447214 0.447214 0.447214 -0.447214
0.000000 -0.707107 -0.000000 -0.000000 -0.000000 0.707107
0.000000 0.000000 0.707107 0.000000 -0.707107 0.000000
So where is the mistake in my code?
Related
Same matrix should be printed but here outside the function its not printing any value of the matrix. What is the issue here?(I dont want the argument name in function and name of variable passed to be same.)
0.000000 0.000000 0.000000 0.000000 0.000000
1.000000 1.000000 1.000000 1.000000 1.000000
2.000000 2.000000 2.000000 2.000000 2.000000
3.000000 3.000000 3.000000 3.000000 3.000000
0.000000 0.000000 0.000000 0.000000 0.000000
0.000000 0.000000 0.000000 0.000000 0.000000
0.000000 0.000000 0.000000 0.000000 0.000000
0.000000 0.000000 0.000000 0.000000 0.000000
#include<stdio.h>
#include<stdlib.h>
void tryn(double *a)
{
int i,j;
a=(double *)calloc(20,sizeof(double));
for(i=0;i<4;i++)
{
for(j=0;j<5;j++)
{
*(a+i*5+j)=i;
}
}
for(i=0;i<4;i++)
{
for(j=0;j<5;j++)
{
printf("%lf ",*(a+i*5+j));
}
printf("\n");
}
}
int main()
{
int i,j;
double *arr;
tryn(arr);
for(i=0;i<4;i++)
{
for(j=0;j<5;j++)
{
printf("%lf ",(arr+i*5+j));
}
printf("\n");
}
free(arr);
}
the output its giving
Parameters to functions in C are pass by value. That means that changes to a in tryn are not reflected in the calling function, so arr in main remains uninitialized.
You need to pass the address of arr to your function:
tryn(&arr);
And change the parameter type in the function accordingly:
void tryn(double **arr)
{
double *a=calloc(20,sizeof(double));
...
*arr = a;
}
I have a question about some odd behaviour with my program.
I have two arrays data and ind_array. Both arrays are initialized in main function. ind_array is filled with some values and data is filled with values using function loadData().
But output of the program depends on where I print values of data array. Before inputting values to ind_array or after.
Look at the first tree numbers of output.
Thanks in advance.
Code
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#include<time.h>
#include<math.h>
#define FILE_NAME "DataValues.csv"
#define NUM_ROWS 40
#define NUM_COLUMS 2
#define COMA " ,"
void loadData(double (*data)[2]){
//double data[NUM_ROWS][NUM_COLUMS];
FILE* data_file = fopen(FILE_NAME, "r");
char line[NUM_ROWS];
int i = 0;
while(fgets(line, sizeof(line), data_file)){
char* tok = strtok(line, COMA);
int j = 0;
while(tok != NULL){
char *ptr;
data[i][j] = atof(tok); //const char to double
tok = strtok(NULL, COMA);
j++;
}
i++;
}
}
int main(){
double data[NUM_ROWS][NUM_COLUMS];
double ind_array[0][5];
loadData(data);
for(int j = 0; j < NUM_ROWS; j++){
printf(" %f\n", data[j][0]);
}
printf("\n");
ind_array[0][0] = 2;
ind_array[0][1] = 5;
ind_array[0][2] = 0;
ind_array[0][3] = 3;
ind_array[0][4] = 0;
for(int j = 0; j < NUM_ROWS; j++){
printf(" %f\n", data[j][0]);
}
return 0;
}
Output
1.000000 2.000000 3.000000 4.000000 5.000000 6.000000 7.000000 8.000000
9.000000 10.000000 11.000000 12.000000 13.000000 14.000000 15.000000
16.000000 17.000000 18.000000 19.000000 20.000000 21.000000 22.000000
23.000000 24.00000025.000000 26.000000 27.000000 28.000000 29.000000
30.000000 31.000000 32.000000 33.000000 34.000000 35.000000 36.000000
37.000000 38.000000 39.000000 40.000000
2.000000 0.000000 0.000000 4.000000 5.000000 6.000000 7.000000
8.000000 9.000000 10.000000 11.000000 12.000000 13.000000 14.000000
15.000000 16.000000 17.000000 18.000000 19.000000 20.000000 21.000000
22.000000 23.000000 24.000000 25.000000 26.000000 27.000000 28.000000
29.000000 30.000000 31.000000 32.000000 33.000000 34.000000 35.000000
36.000000 37.000000 38.000000 39.000000 40.000000
Well you are declaring a 0 X 5 array on this line:
double ind_array[0][5];
The total amount of cells in that array is 0 x 5 = 0. You are printing uninitialized memory which is undefined behaviour, switch the 0 for 1.
I have a Lagrange interpolation algorithm that begins to diverge after many time steps and I can't seem to figure out why. As a quick review, if I had two arrays
int x[11] = {0, 10, 20, 30, 40, 50, 60, 70, 80, 90, 100}
int y[11] = {0, 2, 4, 6, 8, 10, 12, 14, 16, 18, 20}
and I input an x-value of 15 into the algorithm, the output (i.e. interpolated y-value) should be 3. The following algorithm gets the interpolated value correct, but as I cycle through incremented inputs eventually the outputs begin to diverge. I am not sure what is causing the divergence. The code creates two arrays of integers going from -100 to +100 and interpolates the values based on an incremented x-input. The values begin matching as they should, but around 55 or so the interpolated y-value begins to diverge. The code is below. Any insight would be greatly appreciated.
#include <stdio.h>
#define SIZE 201
int main()
{
double x[SIZE], y[SIZE], value, sum, factor[SIZE];
for (int i = 0; i < SIZE; i++)
{
x[i] = -100 + i;
}
for (int i = 0; i < SIZE; i++)
{
y[i] = -100 + i;
}
value = 0.0;
while (1)
{
sum = 0.0;
printf("Input is: %lf\n", value);
for(int i = 0; i < SIZE; i++)
{
factor[i] = 1.0;
for(int j = 0; j < SIZE; j++)
{
if(i != j)
{
factor[i] = factor[i] * (value - x[j])/(x[i] - x[j]);
}
}
sum = sum + factor[i] * y[i];
}
printf("Output is: %lf\n", sum);
// if ((value - sum) > 0.01) break;
if (value < 100) value += 0.001;
else break;
}
return 0;
}
Given N samples, the Lagrange polynomial is of the degree of N, in your case, 200. It is a pretty large degree, and for a value large enough the intermediate results (i.e. factor) starts behaving quite erratically. I printed factor after each iteration of the outer loop, and this is what I have on my machine (where the code diverges at value 62.1):
Input is: 62.100000
0.000000
-0.000000
0.000000
-0.000000
0.000000
-0.000000
0.000000
-0.000000
0.000000
-0.000000
0.000000
-0.000000
0.000000
-0.000000
0.000000
-0.000000
0.000000
-0.000000
0.000000
-0.000000
0.000000
-0.000000
0.000000
-0.000000
0.000000
-0.000000
0.000000
-0.000000
0.000000
-0.000000
0.000000
-0.000000
0.000000
-0.000000
0.000002
-0.000010
0.000047
-0.000212
0.000916
-0.003834
0.015558
-0.061214
0.233667
-0.865800
3.115490
-10.892598
37.019453
-122.351630
393.413839
-1231.176112
3751.320027
-11132.603776
32188.920849
-90709.929863
249216.884601
-667734.556134
1745251.075381
-4451006.398268
11079451.201015
-26924437.939740
63892139.532124
-148088119.614110
335320733.480250
-741923910.135582
1604370277.576735
-3391407192.476863
7009154161.161494
-14165714298.372702
28000907070.092331
-54142322716.578796
102423636122.796417
-189594810732.400879
343460854643.929565
-608991796878.604858
1057024952593.679688
-1796190002106.606201
2988571833231.810547
-4869319260810.958008
7769844431032.450195
-12143392562153.164062
18590594785582.515625
-27881222667878.292969
40966915113033.500000
-58978430272092.585938
83200365623915.609375
-115016713573880.578125
155822462931701.031250
-206899948714767.906250
269263745978734.968750
-343484172924072.000000
429506076055356.812500
-526485323131795.875000
632668952077638.500000
-745344926690223.250000
860883054405210.375000
-974879663742953.625000
1082405867199472.750000
-1178344322529343.250000
1257784740974879.500000
-1316436663535827.500000
1351011674779421.000000
-1359527880018181.000000
1341497535715305.750000
-1297973187760748.750000
1231446261994579.000000
-1145611653890577.250000
1045029162299372.250000
-934724762872858.125000
819779917571950.250000
-704954924193421.250000
594383648437451.875000
-491363855983359.125000
398252366806871.062500
-316460003024013.937500
246529925760965.156250
-188275766805651.375000
140953330916004.437500
-103441104978326.546875
74409294797142.390625
-52463275136077.218750
36253867741005.359375
-24552698830046.890625
16295359517452.095703
-10597930155882.712891
6753701701870.307617
-4216931244837.877441
2579609371842.192871
-1545902253614.920410
907502140862.699341
-521815325006.697205
293869684950.453308
-162078786010.080200
87537719445.039368
-46294138480.248749
23970808358.990040
-12151499571.568396
6030219319.167710
-2929269302.567177
1392763710.430415
-648125926.103312
295175902.544337
-131559694.388017
57381635.324659
-24492187.039684
10230449.673919
-4182126.956840
1673313.368037
-655394.261464
251347.530884
-94414.677824
34753.964878
-12544.686326
4444.407825
-1547.546444
530.612164
-179.651152
60.317931
-20.218974
6.844738
-2.391284
0.904560
-0.429040
1.136162
0.029430
-0.003145
0.000450
-0.000070
0.000011
-0.000002
0.000000
-0.000000
0.000000
-0.000000
0.000000
-0.000000
0.000000
-0.000000
0.000000
-0.000000
0.000000
-0.000000
0.000000
-0.000000
0.000000
-0.000000
0.000000
-0.000000
0.000000
-0.000000
0.000000
-0.000000
0.000000
-0.000000
0.000000
-0.000000
0.000000
-0.000000
0.000000
-0.000000
0.000000
-0.000000
Output is: 67.164298
Sorry for the volume of paste.
You can see that the code operates with very large factors (e.g. 155822462931701.031250) while sum stays around 1. This leads to loss of precision due to normalization. And the loss of precision amplifies as value grows.
Bottom line is, the naive Lagrange interpolation is numerically unstable. Check out Notes.
I'm currently coding a basic neural network that is supposed to calculate a XOR, using backpropagation. However, it instead outputs the average of its target outputs. (A XOR returning {0,1,1,0}, that is 0.5).
I followed both the following articles [1][2] and can't find my error. That guy supposedly had the same problem, but never found an answer.
Anyway, here's my code:
network.c
void initialise_network(Network *network)
{
assert(network != NULL);
network->inputs[network->num_inputs] = 1.0;
network->hidden[network->num_hidden] = 1.0;
for (int i = 0; i < network->num_inputs+1; i++)
{
for (int j = 0; j < network->num_hidden; j++)
{
network->ithw[i][j] = rnd_double(-1, 1);
network->delta_hidden[i][j] = rnd_double(0, 0);
printf("ithw[%d][%d]: %f\n", i, j, network->ithw[i][j]);
}
}
for (int i = 0; i < network->num_hidden+1; i++)
{
for (int j = 0; j < network->num_outputs; j++)
{
network->htow[i][j] = rnd_double(-1, 1);
network->delta_output[i][j] = rnd_double(0, 0);
// printf("htow[%d][%d]: %f\n", i, j, network->htow[i][j]);
}
}
}
void pass_forward(double* inputs, Network *network)
{
log_info("pass_forward() !");
printf("Inputs: \n");
for (int i = 0; i < network->num_inputs; i++)
{
network->inputs[i] = inputs[i];
printf("%f, ", network->inputs[i]);
}
for (int i = 0; i < network->num_hidden; i++)
{
double sum = 0.0;
for (int j = 0; j < network->num_inputs+1; j++)
{
printf("\n inputs[%d]: %f", j, network->inputs[j]);
sum += network->inputs[j] * network->ithw[j][i];
printf("\nithw[%d][%d]: %f", j, i, network->ithw[j][i]);
printf("\n sum[%d]: %f", j, sum);
}
printf("\n hidden[%d]: %f", i, sum);
network->hidden[i] = sigmoid(sum);
printf("\n sigmoid(hidden[%d]): %f", i, network->hidden[i]);
}
for (int i = 0; i < network->num_outputs; i++)
{
double sum = 0.0;
for (int j = 0; j < network->num_hidden+1; j++)
{
sum += network->hidden[j] * network->htow[j][i];
}
printf("\n output[%d]: %f\n", i, network->outputs[i]);
network->outputs[i] = sigmoid(sum);
}
}
trainer_xor.c
void train_network(double *target_output, Network *network)
{
double *delta_hidden = malloc(sizeof(double) * network->num_hidden + 1);
double *delta_output = malloc(sizeof(double) * network->num_outputs);
double momentum = 0.1;
printf("Inputs: %f, %f\n", network->inputs[0], network->inputs[1]);
printf("Output: %f\n", network->outputs[0]);
printf("Target Output: %f\n", target_output[0]);
for (int i = 0; i < network->num_outputs; i++)
{
delta_output[i] = network->outputs[i] * (1.0 - network->outputs[i]) *
(target_output[i] - network->outputs[i]);
printf("delta_output: %f\n", delta_output[i]);
}
for (int i = 0; i < network->num_hidden + 1; i++)
{
double error = 0.0;
for (int j = 0; j < network->num_outputs; j++)
{
error += network->htow[i][j] * delta_output[j];
}
delta_hidden[i] = network->hidden[i] * (1.0 - network->hidden[i]) * error;
printf("hidden[%d]: %f\n", i, network->hidden[i]);
printf("delta_hidden[%d]: %f\n", i, delta_hidden[i]);
}
for (int i = 0; i < network->num_outputs; i++)
{
for (int j = 0; j < network->num_hidden + 1; j++)
{
double delta = network->learning_rate * delta_output[i] * network->hidden[j];
network->htow[j][i] += delta;
network->htow[j][i] += momentum * network->delta_output[j][i];
network->delta_output[j][i] = delta;
// printf("htow[%d][%d]: %f\n", i, j, network->htow[i][j]);
printf("htow[%d][%d]: %f\n", j, i, network->htow[j][i]);
}
}
for (int i = 0; i < network->num_hidden; i++)
{
for (int j = 0; j < network->num_inputs + 1; j++)
{
double delta = network->learning_rate * delta_hidden[i] * network->inputs[j];
network->ithw[j][i] += delta;
network->ithw[j][i] += momentum * network->delta_hidden[j][i];
network->delta_hidden[j][i] = delta;
printf("ithw[%d][%d]: %f\n", j, i, network->ithw[j][i]);
}
}
getchar();
}
void do_training(int training_times, Trainer *trainer)
{
trainer->training_times = training_times;
for (int i = 0; i < training_times; i++)
{
for (int j = 0; j < trainer->train_set_size; j++)
{
pass_forward(trainer->train_set[j], trainer->network);
train_network(get_target_values(trainer->train_set[j], trainer->train_set_size),
trainer->network);
}
}
}
main.c
int main()
{
initialize_utils();
Network *network = network_create(2, 2, 1);
initialise_network(network);
Trainer *trainer = trainer_create(network);
do_training(300, trainer);
return 0;
}
I train my network for 300 times. The train_set is as follows:
[0][0] = 0
[0][1] = 0
[1][0] = 1
[1][1] = 0
[2][0] = 0
[2][1] = 1
[3][0] = 1
[3][1] = 1
For more informations, here are my outputs at a certain time:
Gen 0:
=== Gen 0! ===
[INFO] (src/network.c:100) pass_forward() !
Inputs:
0.000000, 0.000000,
inputs[0]: 0.000000
ithw[0][0]: 0.316492
sum[0]: 0.000000
inputs[1]: 0.000000
ithw[1][0]: -0.028962
sum[1]: 0.000000
inputs[2]: 1.000000
ithw[2][0]: -0.915344
sum[2]: -0.915344
hidden[0]: -0.915344
sigmoid(hidden[0]): 0.285908
inputs[0]: 0.000000
ithw[0][1]: 0.089068
sum[0]: 0.000000
inputs[1]: 0.000000
ithw[1][1]: 0.176854
sum[1]: 0.000000
inputs[2]: 1.000000
ithw[2][1]: 0.958716
sum[2]: 0.958716
hidden[1]: 0.958716
sigmoid(hidden[1]): 0.722865
output[0]: 0.000000
train_network()!
Inputs: 0.000000, 0.000000
Output: 0.625586
Target Output: 0.000000
delta_output: -0.146530
hidden[0]: 0.285908
delta_hidden[0]: 0.002849
hidden[1]: 0.722865
delta_hidden[1]: 0.007222
hidden[2]: 1.000000
delta_hidden[2]: -0.000000
htow[0][0]: -0.107817
htow[1][0]: -0.277817
htow[2][0]: 0.674453
ithw[0][0]: 0.316492
ithw[1][0]: -0.028962
ithw[2][0]: -0.914489
ithw[0][1]: 0.089068
ithw[1][1]: 0.176854
ithw[2][1]: 0.960883
Gen 1:
=== Gen 1! ===
[INFO] (src/network.c:100) pass_forward() !
Inputs:
0.000000, 0.000000,
inputs[0]: 0.000000
ithw[0][0]: 0.316628
sum[0]: 0.000000
inputs[1]: 0.000000
ithw[1][0]: -0.028659
sum[1]: 0.000000
inputs[2]: 1.000000
ithw[2][0]: -0.914866
sum[2]: -0.914866
hidden[0]: -0.914866
sigmoid(hidden[0]): 0.286005
inputs[0]: 0.000000
ithw[0][1]: 0.089247
sum[0]: 0.000000
inputs[1]: 0.000000
ithw[1][1]: 0.177256
sum[1]: 0.000000
inputs[2]: 1.000000
ithw[2][1]: 0.959846
sum[2]: 0.959846
hidden[1]: 0.959846
sigmoid(hidden[1]): 0.723091
output[0]: 0.625643
train_network()
Inputs: 0.000000, 0.000000
Output: 0.613576
Target Output: 0.000000
delta_output: -0.145479
hidden[0]: 0.286005
delta_hidden[0]: 0.003118
hidden[1]: 0.723091
delta_hidden[1]: 0.007844
hidden[2]: 1.000000
delta_hidden[2]: -0.000000
htow[0][0]: -0.118963
htow[1][0]: -0.304226
htow[2][0]: 0.639053
ithw[0][0]: 0.316718
ithw[1][0]: -0.028568
ithw[2][0]: -0.913841
ithw[0][1]: 0.089431
ithw[1][1]: 0.177440
ithw[2][1]: 0.962383
Gen 10:
=== Gen 10! ===
[INFO] (src/network.c:100) pass_forward() !
Inputs:
0.000000, 0.000000,
inputs[0]: 0.000000
ithw[0][0]: 0.317382
sum[0]: 0.000000
inputs[1]: 0.000000
ithw[1][0]: -0.025525
sum[1]: 0.000000
inputs[2]: 1.000000
ithw[2][0]: -0.911555
sum[2]: -0.911555
hidden[0]: -0.911555
sigmoid(hidden[0]): 0.286682
inputs[0]: 0.000000
ithw[0][1]: 0.089229
sum[0]: 0.000000
inputs[1]: 0.000000
ithw[1][1]: 0.180321
sum[1]: 0.000000
inputs[2]: 1.000000
ithw[2][1]: 0.967483
sum[2]: 0.967483
hidden[1]: 0.967483
sigmoid(hidden[1]): 0.724618
output[0]: 0.547804
Inputs: 0.000000, 0.000000
Output: 0.539370
Target Output: 0.000000
delta_output: -0.134006
hidden[0]: 0.286682
delta_hidden[0]: 0.004474
hidden[1]: 0.724618
delta_hidden[1]: 0.010913
hidden[2]: 1.000000
delta_hidden[2]: -0.000000
htow[0][0]: -0.176218
htow[1][0]: -0.440373
htow[2][0]: 0.456051
ithw[0][0]: 0.317521
ithw[1][0]: -0.025386
ithw[2][0]: -0.910074
ithw[0][1]: 0.089499
ithw[1][1]: 0.180592
ithw[2][1]: 0.971027
Gen 100:
=== Gen 100! ===
[INFO] (src/network.c:100) pass_forward() !
Inputs:
0.000000, 0.000000,
inputs[0]: 0.000000
ithw[0][0]: 0.295665
sum[0]: 0.000000
inputs[1]: 0.000000
ithw[1][0]: -0.014208
sum[1]: 0.000000
inputs[2]: 1.000000
ithw[2][0]: -0.929113
sum[2]: -0.929113
hidden[0]: -0.929113
sigmoid(hidden[0]): 0.283105
inputs[0]: 0.000000
ithw[0][1]: 0.023758
sum[0]: 0.000000
inputs[1]: 0.000000
ithw[1][1]: 0.161541
sum[1]: 0.000000
inputs[2]: 1.000000
ithw[2][1]: 0.932629
sum[2]: 0.932629
hidden[1]: 0.932629
sigmoid(hidden[1]): 0.717608
output[0]: 0.512934
Inputs: 0.000000, 0.000000
Output: 0.505055
Target Output: 0.000000
delta_output: -0.126251
hidden[0]: 0.283105
delta_hidden[0]: 0.004697
hidden[1]: 0.717608
delta_hidden[1]: 0.011935
hidden[2]: 1.000000
delta_hidden[2]: -0.000000
htow[0][0]: -0.195365
htow[1][0]: -0.496565
htow[2][0]: 0.365162
ithw[0][0]: 0.295813
ithw[1][0]: -0.014059
ithw[2][0]: -0.927556
ithw[0][1]: 0.024074
ithw[1][1]: 0.161856
ithw[2][1]: 0.936526
Kino, do you know that you need to train the network in multiple backpropagation passes until it converges, that is, until the weights change so that the difference between your target outputs and the actual outputs becomes smaller than some tolerance?
train_network() only seems to make one pass, are you doing the rest of the training elsewhere?
Something like:
const double TOLERANCE = 0.001;
while( fabs(network->outputs[ 0 ] - target_output[ 0 ]) > TOLERANCE &&
fabs(network->outputs[ 1 ] - target_output[ 1 ]) > TOLERANCE ) {
train_network(target_output, network);
}
(But it's probably neater to do the loop in train_network() itself.)
I am trying to initialize two copies of the same two-dimensional arrays in C. However, only one stores the correct data:
//convert 1D array to 2D array
double** myA = malloc(n*sizeof(double*));
for (j=0;j<n;j++)
myA[j]=&a[j*n];
double** myS = (double **) malloc(n*sizeof(double*));
for (i=0; i<n; i++)
for (j=0; j<n; j++){
myS[i] = (double *) malloc(n*sizeof(double));
myS[i][j] = myA[i][j];
}
printf("A:\n");
print_matrix(myA,n);
printf("S:\n");
print_matrix(myS,n);
I want to initialize two copies of A. One as myA and the other as myS. However, here is my output:
A:
0.000000 1.000000 2.000000 3.000000 4.000000
1.000000 1.414214 2.236068 3.162278 4.123106
2.000000 2.236068 2.828427 3.605551 4.472136
3.000000 3.162278 3.605551 4.242641 5.000000
4.000000 4.123106 4.472136 5.000000 5.656854
S:
-0.000000 -0.000000 -0.000000 -0.000000 4.000000
-0.000000 -0.000000 -0.000000 -0.000000 4.123106
-0.000000 -0.000000 -0.000000 -0.000000 4.472136
-0.000000 -0.000000 -0.000000 -0.000000 5.000000
-0.000000 -0.000000 -0.000000 -0.000000 5.656854
Why are all the columns but the last -0.000000?
You're overwriting each row buffer N-1 times (and leaking memory like a sieve leaks water in the process).
Change this:
for (i=0; i<n; i++)
for (j=0; j<n; j++){
myS[i] = (double *) malloc(n*sizeof(double));
myS[i][j] = myA[i][j];
}
To this:
for (i=0; i<n; i++)
{
myS[i] = malloc(n*sizeof(double));
for (j=0; j<n; j++)
myS[i][j] = a[i*n+j];
}
Note two things in the above code:
Don't cast malloc.
The myA temp array is not needed. You can do the math with the index yourself. Throw that entire thing out.