In C++, every function will have some return value, however, the number of permitted return value is one.
For example,
int myfunc()
{ int a, b;
a=3; b=4;
return a,b;
}
will cause trouble since the calling function will not know which value is returned...
Then how we return multiple values like described above??
We may define a structure data, for example,
typedef struct Mystruct
{ int a,
int b
} MYSTRUCT;
in the header file,
then in the cpp file,
we define the function as MYSTRUCT type:
MYSTRUCT myfunc()
{
MYSTRUCT mystruct;
mystruct.a=3;
mystruct.b=4;
return mystruct;
}
Here we go!
We may define another MYSTRUCT type data and the returned value would be transfered to that data by the calling function.
Cheers!
Monday, 30 June 2008
What's inline function in c++
When a function is declared inline, the function is expanded at the calling block. The function is not treated as a separate unit like other normal functions.
But a compiler is free to decide, if a function qualifies to be an inline function. If the inline function is found to have larger chunk of code, it will not be treated as an inline function, but as like other normal functions.
Inline functions are treated like macro definitions by the C++ compiler. They are declared with the keyword inline as follows.
//Declaration for C++ Tutorial inline sample:
int add(int x,int y);
//Definition for C++ Tutorial inline sample:
inline int add(int x,int y)
{
return x+y;
}
In fact, the keyword inline is not necessary. If the function is defined with its body directly and the function has a smaller block of code, it will be automatically treated as inline by the compiler.
As implied, inline functions are meant to be used if there is a need to repetitively execute a small block of code, which is smaller. When such functions are treated inline, it might result in a significant performance difference.
But a compiler is free to decide, if a function qualifies to be an inline function. If the inline function is found to have larger chunk of code, it will not be treated as an inline function, but as like other normal functions.
Inline functions are treated like macro definitions by the C++ compiler. They are declared with the keyword inline as follows.
//Declaration for C++ Tutorial inline sample:
int add(int x,int y);
//Definition for C++ Tutorial inline sample:
inline int add(int x,int y)
{
return x+y;
}
In fact, the keyword inline is not necessary. If the function is defined with its body directly and the function has a smaller block of code, it will be automatically treated as inline by the compiler.
As implied, inline functions are meant to be used if there is a need to repetitively execute a small block of code, which is smaller. When such functions are treated inline, it might result in a significant performance difference.
WhY Virtual Functions in C++??
C++ virtual function is a member function of a class, whose functionality can be over-ridden in its derived classes. The whole function body can be replaced with a new set of implementation in the derived class. The concept of c++ virtual functions is different from C++ Function overloading.
C++ Virtual Function - Properties:
C++ virtual function is,
A member function of a class
Declared with virtual keyword
Usually has a different functionality in the derived class
A function call is resolved at run-time
The difference between a non-virtual c++ member function and a virtual member function is, the non-virtual member functions are resolved at compile time. This mechanism is called static binding. Where as the c++ virtual member functions are resolved during run-time. This mechanism is known as dynamic binding.
C++ Virtual Function - Reasons:
The most prominent reason why a C++ virtual function will be used is to have a different functionality in the derived class.
For example a Create function in a class Window may have to create a window with white background. But a class called CommandButton derived or inherited from Window, may have to use a gray background and write a caption on the center. The Create function for CommandButton now should have a functionality different from the one at the class called Window.
C++ Virtual function - Example:
This article assumes a base class named Window with a virtual member function named Create. The derived class name will be CommandButton, with our over ridden function Create.
class Window // Base class for C++ virtual function example
{ public:
virtual void Create() // virtual function for C++ virtual function example
{ cout <<"Base class Window"<
class CommandButton : public Window
{ public: void Create()
{ cout<<"Derived class Command Button - Overridden C++ virtual function"<
void main()
{ Window *x, *y;
x = new Window();
x->Create();
y = new CommandButton();
y->Create(); }
The output of the above program will be,
Base class Window
Derived class Command Button
If the function had not been declared virtual, then the base class function would have been called all the times. Because, the function address would have been statically bound during compile time. But now, as the function is declared virtual it is a candidate for run-time linking and the derived class function is being invoked.
C++ Virtual function - Call Mechanism:
Whenever a program has a C++ virtual function declared, a v-table is constructed for the class. The v-table consists of addresses to the virtual functions for classes and pointers to the functions from each of the objects of the derived class. Whenever there is a function call made to the c++ virtual function, the v-table is used to resolve to the function address. This is how the Dynamic binding happens during a virtual function call.
C++ Virtual Function - Properties:
C++ virtual function is,
A member function of a class
Declared with virtual keyword
Usually has a different functionality in the derived class
A function call is resolved at run-time
The difference between a non-virtual c++ member function and a virtual member function is, the non-virtual member functions are resolved at compile time. This mechanism is called static binding. Where as the c++ virtual member functions are resolved during run-time. This mechanism is known as dynamic binding.
C++ Virtual Function - Reasons:
The most prominent reason why a C++ virtual function will be used is to have a different functionality in the derived class.
For example a Create function in a class Window may have to create a window with white background. But a class called CommandButton derived or inherited from Window, may have to use a gray background and write a caption on the center. The Create function for CommandButton now should have a functionality different from the one at the class called Window.
C++ Virtual function - Example:
This article assumes a base class named Window with a virtual member function named Create. The derived class name will be CommandButton, with our over ridden function Create.
class Window // Base class for C++ virtual function example
{ public:
virtual void Create() // virtual function for C++ virtual function example
{ cout <<"Base class Window"<
class CommandButton : public Window
{ public: void Create()
{ cout<<"Derived class Command Button - Overridden C++ virtual function"<
void main()
{ Window *x, *y;
x = new Window();
x->Create();
y = new CommandButton();
y->Create(); }
The output of the above program will be,
Base class Window
Derived class Command Button
If the function had not been declared virtual, then the base class function would have been called all the times. Because, the function address would have been statically bound during compile time. But now, as the function is declared virtual it is a candidate for run-time linking and the derived class function is being invoked.
C++ Virtual function - Call Mechanism:
Whenever a program has a C++ virtual function declared, a v-table is constructed for the class. The v-table consists of addresses to the virtual functions for classes and pointers to the functions from each of the objects of the derived class. Whenever there is a function call made to the c++ virtual function, the v-table is used to resolve to the function address. This is how the Dynamic binding happens during a virtual function call.
Friday, 27 June 2008
Gauss-Newton algorithm from wikimedia
The Gauss–Newton algorithm is a method used to solve non-linear least squares problems. It can be seen as a modification of Newton's method for finding a minimum of a function. Unlike Newton's method, the Gauss–Newton algorithm can only be used to minimize a sum of squared function values, but it has the advantage that second derivatives, which can be challenging to compute, are not required.
Non-linear least squares problems arise for instance in non-linear regression, where parameters in a model are sought such that the model is in good agreement with available observations.
The method is due to the renowned mathematician Carl Friedrich Gauss.
Non-linear least squares problems arise for instance in non-linear regression, where parameters in a model are sought such that the model is in good agreement with available observations.
The method is due to the renowned mathematician Carl Friedrich Gauss.
Thursday, 26 June 2008
Conjugate Gradient Methods in Multidimensional non-linear Optimization
Use gradient information:
f(x)~=c-b*x+1/2x*A*x.
Steepest Descent algorithm....not so good.
Solution: Conjugate Gradient Method.
Minimizing the function
f(x)=1/2 *x*A*x-b*x.
The function is minimized when its gradient
gradient(f)=A*x-b is zero.
The conjugate gradient method can be used to solve not only linear algebraic equations by minimizing a quadratic form, but also minimizing a non-linear function.
*Conjugate directions
--"non-interfering" directions with special property that minimizing along one is not "spoiled" by subsequently minimizing along another.
In conjugate gradient method, we want to find new gradient in a direction that is "conjugate" to the old gradient, and to all previous directions traversed.
f(x)~=c-b*x+1/2x*A*x.
Steepest Descent algorithm....not so good.
Solution: Conjugate Gradient Method.
Minimizing the function
f(x)=1/2 *x*A*x-b*x.
The function is minimized when its gradient
gradient(f)=A*x-b is zero.
The conjugate gradient method can be used to solve not only linear algebraic equations by minimizing a quadratic form, but also minimizing a non-linear function.
*Conjugate directions
--"non-interfering" directions with special property that minimizing along one is not "spoiled" by subsequently minimizing along another.
In conjugate gradient method, we want to find new gradient in a direction that is "conjugate" to the old gradient, and to all previous directions traversed.
Powell's Quadratically Convergent Method
Powell first discovered a direction set method that does produce N mutually congugate directions.
Here is how it goes:
Initialize the set of directions u_i to the basis vectors,
u_i=e_i, for i=1,...,N.
Now repeat the following steps until your function stops decreasing:
1. Save your start position as P_0
2. For i=1,..,N. Move P_{i-1} to the minimum along direction u_i and call this point P_i.
3. For i=1,...,N-1, set u_i<-u_{i+1}.
4. Set u_{N}<-P_{N}-P_0
5. Move P_{N} to the minimum along direction u_{N} and call this point P_0.
Done!
Here is how it goes:
Initialize the set of directions u_i to the basis vectors,
u_i=e_i, for i=1,...,N.
Now repeat the following steps until your function stops decreasing:
1. Save your start position as P_0
2. For i=1,..,N. Move P_{i-1} to the minimum along direction u_i and call this point P_i.
3. For i=1,...,N-1, set u_i<-u_{i+1}.
4. Set u_{N}<-P_{N}-P_0
5. Move P_{N} to the minimum along direction u_{N} and call this point P_0.
Done!
Numerical solution to multidimensional non-linear optimization
Basic idea, start at a point P in N-dimensional space, and we proceed from there in some vector direction n, then any function of N variables f(P) can be minimized along the line n by our one-dimensional methods. So a multidimensional minimization methods can be transformed as sequences of line minimizations. Different methods differ only by how, at each stage, they choose the next direction n to try.
The basic routine of line minimization is
linmin: Given as input the vectors P and n, and the function f, find the scalar /lambda that minimizes f(P+\lambda*n). Replace P by P+\lambda*n, replace n by \lambda*n. Done.
The basic routine of line minimization is
linmin: Given as input the vectors P and n, and the function f, find the scalar /lambda that minimizes f(P+\lambda*n). Replace P by P+\lambda*n, replace n by \lambda*n. Done.
G++ and qt in linux
Qt is really nice to develop graphic user interface under linux platform, just the same as in Visual C++..
For me, I prefer gcc+Qt under linux, and Visual C++ under windows.
Well, you can also use Qt under windows, but need to install MSYS and MinGW, which is really lots of extra work! But after that, basically you are able to run a window interface under windows using linux command line, as if you were programming under linux, isn't that cool?
For me, I prefer gcc+Qt under linux, and Visual C++ under windows.
Well, you can also use Qt under windows, but need to install MSYS and MinGW, which is really lots of extra work! But after that, basically you are able to run a window interface under windows using linux command line, as if you were programming under linux, isn't that cool?
A good book for numerical computation
"Numerical Recipes in C, the art of Scientific Computing", authored by W.H.Press, S.A. Teukolsky, W.T.Vetterling and B.P. Flannery.
I really found this book very useful, since it covers almost all problems in numerical computation, like solution of linear algebraic equations, functions, sorting, minimizing and maximizing of functions, eigensystems..
I read this book since I come to a multidimensional non-linear function minimization problem, and I search on the internet found that there is one very famous algorithm called the "Downhill Simplex Method" by Nelder and Mead.
I really found this book very useful, since it covers almost all problems in numerical computation, like solution of linear algebraic equations, functions, sorting, minimizing and maximizing of functions, eigensystems..
I read this book since I come to a multidimensional non-linear function minimization problem, and I search on the internet found that there is one very famous algorithm called the "Downhill Simplex Method" by Nelder and Mead.
Subscribe to:
Posts (Atom)