I’m learning linear algebra, and thought I’d transform some of my new found knowledge into a small C++ program. For the numbers, I stumbled across CLN, a Class Library for Numbers, which I’m trying to use.
LinearEquation.h:
#include <iostream>
#include <cln/real.h>
#include <cln/rational.h>
using namespace std;
using namespace cln;
class LinearEquation{
public:
// constructor
LinearEquation(cl_R* coefficients, int numFactors, cl_R constant);
// copy constructor
LinearEquation(const LinearEquation& le);
// assignment constructor
LinearEquation& operator=(const LinearEquation& rhs);
// destructor
~LinearEquation();
/*
isSolution returns true if a provided set of rational numbers is a solution
to this linear Equation, and false otherwise.
No error checking is done, thus s and num is expected to match the number
of items in _coefficients;
*/
bool isSolution(const cl_R* s, int num);
private:
int _numFactors;
cl_R* _coefficients;
cl_R _constant;
};
LinearEquation.cpp:
#include "LinearEquation.h"
// constructor
LinearEquation::LinearEquation(cl_R* coefficients,
int numFactors,
cl_R constant){
_numFactors = numFactors;
_coefficients = new cl_R[numFactors];
for(int i=0; i<numFactors; i++){
_coefficients[i] = coefficients[i];
}
_constant = constant;
}
// copy constructor
LinearEquation::LinearEquation(const LinearEquation& obj){
_numFactors = obj._numFactors;
_coefficients = new cl_R[_numFactors];
for(int i=0; i<_numFactors; i++){
_coefficients[i] = obj._coefficients[i];
}
_constant = obj._constant;
}
// assignment constructor
LinearEquation& LinearEquation::operator=(const LinearEquation& le){
if(this != &le){
_numFactors = le._numFactors;
delete [] _coefficients;
_coefficients = new cl_R[_numFactors];
for(int i=0; i<_numFactors; i++){
_coefficients[i] = le._coefficients[i];
}
_constant = le._constant;
}
}
// destructor
LinearEquation::~LinearEquation(){
delete [] _coefficients;
}
bool LinearEquation::isSolution(const cl_R* s, int num){
cl_R sum = 0;
for(int i=0; i<_numFactors; i++){
sum = sum + (_coefficients[i] * s[i]);
}
return sum == _constant;
}
and finally, main.cpp
#include <cln/integer.h>
#include <cln/real.h>
#include "LinearEquation.h"
#include <iostream>
using namespace cln;
using namespace std;
int main(){
int le1NumFactors = 2;
cl_R* le1Coefficients = new cl_R[le1NumFactors];
le1Coefficients[0] = 3;
le1Coefficients[1] = 2;
cl_R le1Constant = 7;
LinearEquation le1(le1Coefficients, le1NumFactors, le1Constant);
int le2NumFactors = 2;
cl_R* le2Coefficients = new cl_R[le2NumFactors];
le2Coefficients[0] = -1;
le2Coefficients[1] = 1;
cl_R le2Constant = 6;
LinearEquation le2(le2Coefficients, le2NumFactors, le2Constant);
cl_R* solution = new cl_R[le2NumFactors];
solution[0] = -1;
solution[1] = 5;
cout << "(-1, 5) is " << (le1.isSolution(solution, 2) ? "" : "not ")
<< "a solution to le1." << endl;
cout << "(-1, 5) is " << (le2.isSolution(solution, 2) ? "" : "not ")
<< "a solution to le2." << endl;
delete [] le1Coefficients;
delete [] le2Coefficients;
delete [] solution;
return 0;
}
g++ LinearEquation.h LinearEquation.cpp -c
works fine, but
g++ main.cpp LinearEquation.o
doesn’t. The following error message is produced:
lowerkey@cassiopeia:~/Desktop/math$ g++ main.cpp LinearEquation.o
/tmp/ccMceM9l.o: In function
__static_initialization_and_destruction_0(int, int)':cln::cl_random_def_init_helper::cl_random_def_init_helper()’
main.cpp:(.text+0x4ab): undefined reference to
main.cpp:(.text+0x4b0): undefined reference to
cln::cl_random_def_init_helper::~cl_random_def_init_helper()'cln::cl_FF_globals_init_helper::cl_FF_globals_init_helper()’
main.cpp:(.text+0x4e0): undefined reference to
main.cpp:(.text+0x4e5): undefined reference to
cln::cl_FF_globals_init_helper::~cl_FF_globals_init_helper()'cln::cl_DF_globals_init_helper::cl_DF_globals_init_helper()’
main.cpp:(.text+0x509): undefined reference to
main.cpp:(.text+0x50e): undefined reference to
cln::cl_DF_globals_init_helper::~cl_DF_globals_init_helper()'cln::cl_LF_globals_init_helper::cl_LF_globals_init_helper()’
main.cpp:(.text+0x532): undefined reference to
main.cpp:(.text+0x537): undefined reference to
cln::cl_LF_globals_init_helper::~cl_LF_globals_init_helper()'cln::cl_gc_dec_pointer_refcount(cln::cl_heap*)’:
/tmp/ccMceM9l.o: In function
main.cpp:(.text._ZN3cln26cl_gc_dec_pointer_refcountEPNS_7cl_heapE[cln::cl_gc_dec_pointer_refcount(cln::cl_heap*)]+0x28):
undefined reference tocln::cl_free_heap_object(cln::cl_heap*)'cln::cl_I_classes_dummy::cl_I_classes_dummy()’:
/tmp/ccMceM9l.o: In function
main.cpp:(.text._ZN3cln18cl_I_classes_dummyC1Ev[cln::cl_I_classes_dummy::cl_I_classes_dummy()]+0x9):
undefined reference tocln::cl_class_fixnum' LinearEquation.o: InLinearEquation::isSolution(cln::cl_R const*, int)’:
function
LinearEquation.cpp:(.text+0x61a): undefined reference to
cln::operator*(cln::cl_R const&, cln::cl_R const&)'cln::operator+(cln::cl_R const&, cln::cl_R const&)’ LinearEquation.o:
LinearEquation.cpp:(.text+0x636): undefined reference to
In function__static_initialization_and_destruction_0(int, int)':cln::cl_random_def_init_helper::cl_random_def_init_helper()’
LinearEquation.cpp:(.text+0x741): undefined reference to
LinearEquation.cpp:(.text+0x746): undefined reference to
cln::cl_random_def_init_helper::~cl_random_def_init_helper()'cln::cl_FF_globals_init_helper::cl_FF_globals_init_helper()’
LinearEquation.cpp:(.text+0x76a): undefined reference to
LinearEquation.cpp:(.text+0x76f): undefined reference to
cln::cl_FF_globals_init_helper::~cl_FF_globals_init_helper()'cln::cl_DF_globals_init_helper::cl_DF_globals_init_helper()’
LinearEquation.cpp:(.text+0x793): undefined reference to
LinearEquation.cpp:(.text+0x798): undefined reference to
cln::cl_DF_globals_init_helper::~cl_DF_globals_init_helper()'cln::cl_LF_globals_init_helper::cl_LF_globals_init_helper()’
LinearEquation.cpp:(.text+0x7bc): undefined reference to
LinearEquation.cpp:(.text+0x7c1): undefined reference to
cln::cl_LF_globals_init_helper::~cl_LF_globals_init_helper()'cln::operator==(cln::cl_R const&,
LinearEquation.o: In function
cln::cl_R const&)’:
LinearEquation.cpp:(.text._ZN3clneqERKNS_4cl_RES2_[cln::operator==(cln::cl_R
const&, cln::cl_R const&)]+0x14): undefined reference to
`cln::equal(cln::cl_R const&, cln::cl_R const&)’ collect2: ld returned
1 exit status
Looks like you aren’t linking the cln library. Try the following (assuming the library is installed correctly):
See the documentation for more details on compiling:
http://www.ginac.de/CLN/cln_11.html#SEC64