GSL: how to get LDLT decomposition in GSL - c

I want to get the matrix l and d of matrix Q from LDLT decomposition. The same result of scipy.linalg.ldl(),here is the code:
#include <gsl/gsl_math.h> #include <gsl/gsl_sf.h>
#include <gsl/gsl_rng.h>
#include <gsl/gsl_randist.h>
#include <gsl/gsl_vector.h>
#include <gsl/gsl_matrix.h>
#include <gsl/gsl_blas.h>
#include <gsl/gsl_linalg.h>
#include <gsl/gsl_cdf.h>
#include <gsl/gsl_errno.h>
int main() {
const int dim = 3;
double pars[3] = { 0, 0, 0 };
double Q[9] = {
2,-1,0,
-1,3,-1,
0,-1,4
};
int i=0,j=0;
// Gaussian Multivariate distribution
gsl_matrix *L = gsl_matrix_calloc(dim, dim);
gsl_vector *S = gsl_vector_calloc(dim);
gsl_permutation * perm = gsl_permutation_calloc(dim);
for(i=0;i<dim*dim;i++) L->data[i]=Q[i];
>> gsl_linalg_ldlt_decomp(L);
for(i=0;i<3;i++){
for(j=0;j<3;j++){
printf("%.4f ", L->data[i*3+j]);
}
printf("\n");
}
printf("\n S=");
for(i=0;i<3;i++)
printf("%.4f ", S->data[i]);
printf("\n");
}
my compile args is gcc ldl.c -lm -llapack -lblas -lgsl
But it returns;
ldl.c: In function ‘main’:
ldl.c:39:5: warning: implicit declaration of function ‘gsl_linalg_ldlt_decomp’; did you mean ‘gsl_linalg_PTLQ_decomp’? [-Wimplicit-function-declaration]
39 | gsl_linalg_ldlt_decomp(L);
| ^~~~~~~~~~~~~~~~~~~~~~
| gsl_linalg_PTLQ_decomp
/usr/bin/ld: /tmp/ccSWXMMb.o: in function `main':
ldl.c:(.text+0x170): undefined reference to `gsl_linalg_ldlt_decomp'
collect2: error: ld returned 1 exit status
WHy ? What shoud I do?

Perhaps the best way of compiling GSL programs via the command-line is by using the flags provided by GSL itself. They can be read via the
gsl-config
utility.
In your case use it as follows (providing your shell can properly process the backquoteses):
gcc ldl.c `gsl-config --libs`
If not, use the output of gsl-config --libs directly as the command line arguments.
Anyway, your code compiles and runs on my system without any problem.

Related

How to fix compilation error "WinMain" in a C program?

Have 3 files-
1.atm.c(Source file)
2.transactions.h(function declarations)
3.transactions.c(defining the functions)
when i compile(GCC) this i am getting a WinMain Error.
And i tried all the ways i know, that i can compile the program.
Ex 1:
gcc -o atm.c transactions.c transactions.h //the atm.c is getting deleted in this way.
Ex 2:as i already included the file(.h) in source so i didn't give the .h in compile time :
gcc -o atm.c transactions.c //in this case file is not getting deleted but getting the WinMain Error.
** OUTPUT:**
gcc -o atm.c transactions.c transactions.h
C:/crossdev/src/mingw-w64-v4-git/mingw-w64-crt/crt/crt0_c.c:18: undefined reference to `WinMain'
collect2.exe: error: ld returned 1 exit status
atm.c:
#include "transactions.h"
int main(void) {
initializeAccount();
getBalance();
//Perform first transaction
askCustomer();
updateAccount(amount);
getBalance();
//Perform second transaction
askCustomer();
updateAccount(amount);
addGift(5.0);
getBalance();
//Perform third transaction
askCustomer();
updateAccount(amount);
addGift(2.0);
getBalance();
thankYou();
return EXIT_SUCCESS;
}
transactions.h:
#include <stdio.h>
#include <stdlib.h>
#ifndef TRANSACTIONS_H_
#define TRANSACTIONS_H_
float accountBalance, amount;
void initializeAccount();
void getBalance(void);
void askCustomer(void);
void updateAccount(float value);
void addGift(float giftAmount);
void thankYou(void);
#endif
transactions.c :
#include <stdio.h>
#include <stdlib.h>
float accountBalance, amount;
void initializeAccount(void){
accountBalance = 0.0;
}
void addGift(float giftAmount){
accountBalance += giftAmount;
printf("A gift of $%.2f has been added to your \n",giftAmount);
}
void askCustomer(void){
printf("Next transaction please...\n");
printf("Enter amount to credit (positive) or debit (negative):");
scanf("%f",&amount);
}
void getBalance(void){
printf("\ncurrent balance is $%.2f\n", accountBalance);
}
void updateAccount(float amount){
accountBalance += amount;
printf("The account was updated with $%.2f\n",amount);
}
void thankYou(void){
printf("------ Thank you! ------");
}
-o is used to name the binary executable which is the output of the program. It should be followed by a file name.
You tell gcc that the linked executable should be named atm.c. Which is incorrect, but also causes that file to not get compiled or linked.
One way to correctly compile:
gcc -std=c99 atm.c transactions.c -o atm.exe

CUDA linking Error undefined reference [duplicate]

This question already has answers here:
Cuda C - Linker error - undefined reference
(2 answers)
Closed 7 years ago.
I'm new to CUDA programming hence running into issues with compiling/ linking files. I'm trying to compile .c and .cu files.
Here are the files:
p3.c:
#include <stdlib.h>
#include <stdio.h>
extern void load_scheduler(int k, int j);
int blocks, threads;
int main(int argc, char* argv[])
{
if (argc > 1)
{
blocks = atoi(argv[1]);
threads = atoi(argv[2]);
}
else
exit(1);
load_scheduler(blocks, threads);
}
And scheduler.cu file:
#include <stdlib.h>
#include <stdio.h>
__global__ void sched_func()
{
int j = 6*5*threadIdx.x;
printf("%d\n",j);
}
void load_scheduler(int b, int n)
{
sched_func<<< b,n >>>();
}
I compile these two files using nvcc -c scheduler.cu p3.c and it seems fine
However, when I try to link these two files using nvcc -o cuda_proj scheduler.o p3.o, I get an error:
p3.o: In function `main':
p3.c:(.text+0x58): undefined reference to `load_scheduler'
collect2: ld returned 1 exit status
I may not be using the right steps to get this working, so if there's any other way I should try out, suggestions are welcome. I am also new to making Makefiles so want to stick to using nvcc commands on terminal.
Just added : extern "c" before load_scheduler definition. NVCC could not recognize the function definition as it belonged to .cu file, therefore the error.
extern "C"
void load_scheduler(int b, int n)
{
sched_func<<< b,n >>>();
}

Function is returning wrong type C

I have a problem with my function waveres. It is supposed to return a amplitude as a float, but does not. It return a random high number. I think it is the definition in my header file that is not "seen" by the main function. The other functions does work so I did not include them. When the waveres function runs, it prints correct values of amp.
Header file
#include <stdio.h>
#include <math.h>
#include <time.h> /* til random funksjonen */
#include <stdlib.h>
void faseforskyvning(float epsi[]);
float waveres(float S[],float w[],float *x, float *t, float epsi[]);
void lespar(float S[], float w[]);
Main program
#include "sim.h"
main()
{
float epsi[9], t = 1.0, x = 1.0;
float S[9], w[9];
float amp;
faseforskyvning(epsi);
lespar(S,w);
amp=waveres(S,w,&x,&t,epsi);
printf("%f\n", amp);
}
waveres:
#include "sim.h"
float waveres(float S[],float w[],float *x, float *t, float epsi[])
{
float amp = 0, k;
int i;
for(i=0;i<10;i++)
{
k = pow(w[i],2)/9.81;
amp = amp + sqrt(2*S[i]*0.2)*cos(w[i]*(*t)+k*(*x)+epsi[i]);
printf("%f\n",amp);
}
return(amp);
}
Sample output where the two last number are supposed to be the same.
0.000000
0.261871
3.750682
3.784552
3.741382
3.532950
3.759173
3.734213
3.418669
3.237864
1078933760.000000
A source to the error might be me compiling wrong. Here is a output from compiler:
make
gcc -c -o test.o test.c
gcc -c -o faseforskyvning.o faseforskyvning.c
gcc -c -o waveres.o waveres.c
gcc -c -o lespar.o lespar.c
gcc test.o faseforskyvning.o waveres.o lespar.o -o test -lm -E
gcc: warning: test.o: linker input file unused because linking not done
gcc: warning: faseforskyvning.o: linker input file unused because linking not done
gcc: warning: waveres.o: linker input file unused because linking not done
gcc: warning: lespar.o: linker input file unused because linking not done
You have undefined behavior, you iterate untill 10
for(i=0;i<10;i++)
But your arrays has size 9 which means the biggest index is 8
float epsi[9], t = 1.0, x = 1.0;
float S[9], w[9];
You need to change your loop to
for(i=0;i<9;i++)
Also your arrays are not initialized, this is also provokes undefined behavior. For example
float w[9]={0};
initializes all elements of array w with 0

"undefined reference to 'cblas_ddot'" when using cblas library

I was testing the cblas ddot, and the code I used is from the link and I fixed it as
#include <stdio.h>
#include <stdlib.h>
#include <cblas.h>
int main()
{
double m[10],n[10];
int i;
int result;
printf("Enter the elements into first vector.\n");
for(i=0;i<10;i++)
scanf("%lf",&m[i]);
printf("Enter the elements into second vector.\n");
for(i=0;i<10;i++)
scanf("%lf",&n[i]);
result = cblas_ddot(10, m, 1, n, 1);
printf("The result is %d\n",result);
return 0;
}
Then when I compiled it, it turned out to be:
/tmp/ccJIpqKH.o: In function `main':
test.c:(.text+0xbc): undefined reference to `cblas_ddot'
collect2: ld returned 1 exit status
I checked the cblas file in /usr/include/cblas.h, and noticed there is
double cblas_ddot(const int N, const double *X, const int incX,
const double *Y, const int incY);
I don't know where it is going wrong. Why does the compiler said the "cblas_ddot" is undefined reference?
You can't just include the header - that only tells the compiler that the functions exist somewhere. You need to tell the linker to link against the cblas library.
Assuming you have a libcblas.a file, you can tell GCC about it with -lcblas.
The web site for GNU Scientific Library tells you how to do this:
2.2 Compiling and Linking
My problem was just solved. The reason is that I made a mistake when inputed the link path. Thanks for Jonathon Reinhart's answers, they are really helpful when learning how to code in linux.
The compile commands are:
gcc -c test.c
gcc -L/usr/lib64 test.o -lgsl -lgslcblas -lm
Where "/usr/lib64" is the correct link path.

`clear` causes undefined reference error

I'm don't seem to be able to generate random number in C under Ubuntu 12.04.
I wrote the fallowing code:
#include <stdio.h>
#include <stdlib.h>
#include <curses.h>
int main (int argc,char* argv[])
{
int number;
clear();
number = rand() % 2; // want to get only 0 or 1
printf("%d",number);
getch();
return 0;
}
I named the file "test_gcc.c".
After that I compile it with:
$ sudo gcc -o test_gcc test_gcc.c
And i get the following message:
/tmp/ccT0s12v.o: In function `main':
test_gcc.c:(.text+0xa): undefined reference to `stdscr'
test_gcc.c:(.text+0x12): undefined reference to `wclear'
test_gcc.c:(.text+0x44): undefined reference to `stdscr'
test_gcc.c:(.text+0x4c): undefined reference to `wgetch'
collect2: ld returned 1 exit status
Can somebody tell me what did I do wrong?
And also how to generate random number in C on Ubuntu 12.04 using gcc?
Thanks in advance!
This has nothing to do with random numbers. The problem is that you're linking without the curses library.
You need to add -lncurses to your gcc command line:
$ gcc -o test_file test_file.c -lncurses
You didn't seed the random number generator. <-- Not the reason for errors
Use srand(time(0)); before calling rand().
Use srand ( time(NULL) ); before number = rand() % 2; to get different random number every time the executable is ran.
For errors:
remove clear() and use getchar() instead of getch() and then it
should worked fine.
getch() is used in compilers that support un-buffered input, but in
case of gcc it's buffered input so use getchar().
code:
#include <stdio.h>
#include <stdlib.h>
#include <curses.h>
int main (int argc,char* argv[])
{
int number;
srand(time(NULL));
number = rand() % 2; // want to get only 0 or 1
printf("%d",number);
getchar();
return 0;
}
Try :
gcc -o test_gcc test_gcc.c -lncurses

Resources