To create a device node use mknod
mknod /dev/mem c 1 1
here the device name is /dev/mem
c represents character device this can be block device also
the first 1 represents the major number
the second 1 represents the minor number
Monday, September 14, 2009
Sunday, September 13, 2009
Little Sugar: FreeBSD total RAM
to find out the total RAM installed on your FreeBSD box use something like this
sysctl hw.physmem
sysctl hw.physmem
Thursday, September 10, 2009
Little Sugar: insmod
Linux kernel extensions have a .ko extension are inserted directly into the kernel
using insmod command.
The kernel modules are removed using rmmod.
Kernel modules are generally placed at /lib/modules.
To update kernel dependencies run depmod
using insmod command.
The kernel modules are removed using rmmod.
Kernel modules are generally placed at /lib/modules.
To update kernel dependencies run depmod
Wednesday, September 09, 2009
Finding Document Similarity
If you need to find how similar a document is we can use vector algebra to our advantage. If we have 2 document d1 and d2 then
Let D1 be a vector of the word-frequency of the document d1
Let D2 be a vector of the word-frequency of the document d2
then the deviation of the 2 documents can be calculated by
using the fact that
cos(theta) = (D1 * D2)/(N(D1) * N(D2))
where N(D) is the normalized vector represented by
N(D) = sqrt(D * D)
Let D1 be a vector of the word-frequency of the document d1
Let D2 be a vector of the word-frequency of the document d2
then the deviation of the 2 documents can be calculated by
using the fact that
cos(theta) = (D1 * D2)/(N(D1) * N(D2))
where N(D) is the normalized vector represented by
N(D) = sqrt(D * D)
Wednesday, September 02, 2009
OpenGL: Diffuse, Specular, Ambient, Emissive lights
In the OpenGL world there are different types of light types
Diffuse
--------
These type come from one direction bu are scattered equally in all directions.
Specular
--------
This type of light comes in one directions and is also bounced off in one direction
Ambient
--------
This type of light source comes from different light sources and is also scattered in different directions
Emissive
---------
This represents light originating from the object
So in general A light source has two types of properties associated with it
LR,LG,LB => the RGB components that make up the light and
MR,MG,MB => the RGB components that are reflected away
Hence the net effect of the RGB is given by a vector
(LR.MR, LG.MG, LB.MB)
if light is coming from two or more sources the light is added and next effect is
of the form
(R1 + R2, G1 + G2, B1 + B2)
Diffuse
--------
These type come from one direction bu are scattered equally in all directions.
Specular
--------
This type of light comes in one directions and is also bounced off in one direction
Ambient
--------
This type of light source comes from different light sources and is also scattered in different directions
Emissive
---------
This represents light originating from the object
So in general A light source has two types of properties associated with it
LR,LG,LB => the RGB components that make up the light and
MR,MG,MB => the RGB components that are reflected away
Hence the net effect of the RGB is given by a vector
(LR.MR, LG.MG, LB.MB)
if light is coming from two or more sources the light is added and next effect is
of the form
(R1 + R2, G1 + G2, B1 + B2)
Tuesday, September 01, 2009
Opengl : Hidden Surface Removal and LIghting
Hidden surface removal is a common technique in the graphics world. The technique says
we should draw only parts of objects that are visible to the user.
What support does OpenGL have for hidden surface removal.OpenGL supports hidden surface removal through a combination of DEPTH BUFFER and COLOR BUFFER.
Depth Buffer and Color Buffer
------------------------------
Our screen is flat basically we see a flat 2D rectangular screen made up of pixels. Associated with these pixels we have a corresponding value in the Depth Buffer and
Color Buffer. So what pixel appears forward or backward depends on its value in the depth buffer. The color value it shows depends on its value in the color buffer.
to enable the depth buffer we need use something like the following:
glClear(GL_COLOR_BUFFER_BIT GL_DEPTH_BUFFER_BIT);
Material And Ambient Light
---------------------------
OpenGL understands RGB. A material quality decides the amount of RGB that is reflected or absorbed. An area can be lit by many light sources.Say in a room the bulb can the primary source of light.But there might be light reflected from walls which can be called secondory sources, the light contributed by such sources is called ambient light.
we should draw only parts of objects that are visible to the user.
What support does OpenGL have for hidden surface removal.OpenGL supports hidden surface removal through a combination of DEPTH BUFFER and COLOR BUFFER.
Depth Buffer and Color Buffer
------------------------------
Our screen is flat basically we see a flat 2D rectangular screen made up of pixels. Associated with these pixels we have a corresponding value in the Depth Buffer and
Color Buffer. So what pixel appears forward or backward depends on its value in the depth buffer. The color value it shows depends on its value in the color buffer.
to enable the depth buffer we need use something like the following:
glClear(GL_COLOR_BUFFER_BIT GL_DEPTH_BUFFER_BIT);
Material And Ambient Light
---------------------------
OpenGL understands RGB. A material quality decides the amount of RGB that is reflected or absorbed. An area can be lit by many light sources.Say in a room the bulb can the primary source of light.But there might be light reflected from walls which can be called secondory sources, the light contributed by such sources is called ambient light.
ps options
If you invoke the ps command like this
ps -C -o etime,pid,pri,cmd
this will show processes that match the command name and show output using the
fields followed after -o i.e
etime - how long the process is running
pid - the pid of the process
pri - priority of the process
cmd - the full command line of the command
ps -C
this will show processes that match the command name and show output using the
fields followed after -o i.e
etime - how long the process is running
pid - the pid of the process
pri - priority of the process
cmd - the full command line of the command
Sunday, August 30, 2009
Looking back at mergesort
For mergesort the recurrence relation is
T(N) = 1 if N == 1
T(N) = cn + 2T(N/2) if N != 1
so if we draw the recursion tree for such a recurrence relation we get
Height of tree = lg N
Number of leaves = N
--------------
Leaves at level 0 -> 1
Leaves at level 1 -> 2
Leaves at level 2 -> 4
Leaves at level 3 -> 8
...
Leaves at level lg N -> N
Total Work Done
----------------
Work done at level 0 -> cn
Work done at level 1 -> cn
Work done at level 2 -> cn
....
Work done at level lg N -> cn
So total work done => cn * lg N => O(n lg n)
T(N) = 1 if N == 1
T(N) = cn + 2T(N/2) if N != 1
so if we draw the recursion tree for such a recurrence relation we get
Height of tree = lg N
Number of leaves = N
--------------
Leaves at level 0 -> 1
Leaves at level 1 -> 2
Leaves at level 2 -> 4
Leaves at level 3 -> 8
...
Leaves at level lg N -> N
Total Work Done
----------------
Work done at level 0 -> cn
Work done at level 1 -> cn
Work done at level 2 -> cn
....
Work done at level lg N -> cn
So total work done => cn * lg N => O(n lg n)
Similarity between objects
Lets say we need to find the similarity between people based on how they rank different items. Based on the that collected data we need to recommend some new
items to the users.People who are similar to each other have a higher likelihood of
liking things that we recommend them.
Now lets consider a Set of Users.A Set of Items and a set of Rankings which users give to these Items.
Given this information how can we find the similarity between 2 Users.
Approach 1)
------------
Take user A and user B
Calculate union of items chosen by user A & B , call it A_UNION_B
Calculate the intersection of the items chosen between A & b, call it A_INT_B
then the value A_INT_B/A_UNION_B represents the similarity of the 2 users A and B
Approach 2)
------------
For every item that is common between A & B we can calculate the Euclidean distance between the rankings of the 2 items.
D = RA (ranking A gave to an item) - RB (ranking B gave to an item)
Sum = Sum + D^2
The once we have the sum we can call it Similarity Sum and use a formula
like
Similarity = 1/(1 + Sum)
The 1 is added to prevent the 1/0 off case, where our magic factor factor is 1
we can choose an arbitrary magic factor say N
Then Similarity = N/(N + Sum)
Approach 3)
-----------
Use the Sum that we got in Approach 2 above
Similarity = Sum / N where N is the number of items common between A and B that have the same rating
Similarity = Sqrt(Similarity)
then to make Similarity fall between the range 0 and 1
Similarity = 1 - tanh(Similarity)
Approach 4)
-----------
Use the Similarity that we calculated in Approach 3
NN = Number of items common between A and B
Similarity = Similarity * (N / NN)
items to the users.People who are similar to each other have a higher likelihood of
liking things that we recommend them.
Now lets consider a Set of Users.A Set of Items and a set of Rankings which users give to these Items.
Given this information how can we find the similarity between 2 Users.
Approach 1)
------------
Take user A and user B
Calculate union of items chosen by user A & B , call it A_UNION_B
Calculate the intersection of the items chosen between A & b, call it A_INT_B
then the value A_INT_B/A_UNION_B represents the similarity of the 2 users A and B
Approach 2)
------------
For every item that is common between A & B we can calculate the Euclidean distance between the rankings of the 2 items.
D = RA (ranking A gave to an item) - RB (ranking B gave to an item)
Sum = Sum + D^2
The once we have the sum we can call it Similarity Sum and use a formula
like
Similarity = 1/(1 + Sum)
The 1 is added to prevent the 1/0 off case, where our magic factor factor is 1
we can choose an arbitrary magic factor say N
Then Similarity = N/(N + Sum)
Approach 3)
-----------
Use the Sum that we got in Approach 2 above
Similarity = Sum / N where N is the number of items common between A and B that have the same rating
Similarity = Sqrt(Similarity)
then to make Similarity fall between the range 0 and 1
Similarity = 1 - tanh(Similarity)
Approach 4)
-----------
Use the Similarity that we calculated in Approach 3
NN = Number of items common between A and B
Similarity = Similarity * (N / NN)
Wednesday, June 17, 2009
Little Sugar: Writing an oscillator function
Say you need to transform values v1, v2 in such a way that they lie between lowerlimit L and upper limit U
then u can use an oscillator function
new value = U - (L + (U * (1 / (1 + value))))
this new value will be between L and U, if u omit L then new value
would be between 0 - U
then u can use an oscillator function
new value = U - (L + (U * (1 / (1 + value))))
this new value will be between L and U, if u omit L then new value
would be between 0 - U
Thursday, June 11, 2009
Little Sugar: Useful commands while programming on Unix Systems
Here are a list of commands that re use file while programming on unix systems
pid = process id
ps aux | grep process_name
- to search for a given process name , generally use this to fetch pid of process
lsof -p pid
- to get all the files or file descriptors being used by the process having pid
lsof | grep file_name
- to get the list processes using a given file
sockstat | grep process_name
- to see the socket connections between a host and desinations
strace -p pid
- to see what the process is doing in general
nc hostname port
- to see data flowing from the hostname at the given portu
pid = process id
ps aux | grep process_name
- to search for a given process name , generally use this to fetch pid of process
lsof -p pid
- to get all the files or file descriptors being used by the process having pid
lsof | grep file_name
- to get the list processes using a given file
sockstat | grep process_name
- to see the socket connections between a host and desinations
strace -p pid
- to see what the process is doing in general
nc hostname port
- to see data flowing from the hostname at the given portu
Tuesday, June 09, 2009
Little Sugar: Things to remember while creating a daemon
While writing a daemon on unix systems. The daemon should not be attached to any
terminals. So make sure the following things hold valid.
chdir '/'
umask 0
Map STDIN to /dev/null
Map STDOUT to your logging system
MAP STRERR to STDOUT
setsid # Make the daemon a session leader
terminals. So make sure the following things hold valid.
chdir '/'
umask 0
Map STDIN to /dev/null
Map STDOUT to your logging system
MAP STRERR to STDOUT
setsid # Make the daemon a session leader
Wednesday, October 15, 2008
use find magic to create etags
To create a TAGS file so that you can work with emacs / vim use the following command
find . -ipath '*.p*' -print | etags -
the above command for example will create etags for all the pl , pm files in the current directory
find . -ipath '*.p*' -print | etags -
the above command for example will create etags for all the pl , pm files in the current directory
Saturday, September 27, 2008
Little Sugar:Decreasing capacity in C++
When you use vector,string class in C++, the problem that happens is that the capacity just increases and does not decrease, causing the data structure to hog lot of memory.
For example, say in the vector 10 elements were added , then to the same vector 1000000 elements were added. Then 900000 were deleted. This will not cause the capacity to decrease. This might result in the vector still holding on to a lot of memory.
One way to fix this is the sap trick.
vector < int > elements; // the original vector
vector < int > (elements.begin(), elements.end() ).swap(elements)
For example, say in the vector 10 elements were added , then to the same vector 1000000 elements were added. Then 900000 were deleted. This will not cause the capacity to decrease. This might result in the vector still holding on to a lot of memory.
One way to fix this is the sap trick.
vector < int > elements; // the original vector
vector < int > (elements.begin(), elements.end() ).swap(elements)
Wednesday, August 13, 2008
vector::assign
"assign" is a method in STL for the vector class. When ever you need to erase the contents of the vector and replace it with totally new contents use the assign member function .You should use the operator= then all elements of the right hand side container would be copied to the container on the left hand side.
e.g
v1.assign(v2.begin() + v2.size()/2 , v2.end())
e.g
v1.assign(v2.begin() + v2.size()/2 , v2.end())
Thursday, June 05, 2008
C++ : std::swap and template specialization
say you want to use the swap method provided in std namespace for your own class . Then instead of using the std::swap option blindly , that would involve making use that your class has at least a copy constructor and that the assignment operator is overridden.
It makes more sense to use template specialization for std::swap
class MyClass {
public:
void Swap(MyClass &) ;
};
namespace std {
template<> swap(MyClass& a, MyClass& b){
a.Swap(b);
}
}
so that way we just tune the default swap to use our class specific Swap and things become fast and simple for us .
It makes more sense to use template specialization for std::swap
class MyClass {
public:
void Swap(MyClass &) ;
};
namespace std {
template<> swap
a.Swap(b);
}
}
so that way we just tune the default swap to use our class specific Swap and things become fast and simple for us .
Wednesday, June 04, 2008
C++ : std::mem_fun
Say you have a class
class MyClass {
public:
int DoSomething(){
}
};
now say you have
std::vector classes;
you can use something like this
for_each(classes.begin(), classes.end(), &DoSomething);
where DoSomething is
int DoSomething(MyClass& e){
d.DoSomething();
}
to call the member function DoSomething rather than the wrapper function use function calls like these
for_each(classes.begin(), classes.end(), mem_fun_ref(&MyClass::DoSomething));
if the classes vector was defined something like this
vector classes
then you would use something like this
for_each(classes.begin(), classes.end(), mem_fun(&MyClass::DoSomething));
class MyClass {
public:
int DoSomething(){
}
};
now say you have
std::vector
you can use something like this
for_each(classes.begin(), classes.end(), &DoSomething);
where DoSomething is
int DoSomething(MyClass& e){
d.DoSomething();
}
to call the member function DoSomething rather than the wrapper function use function calls like these
for_each(classes.begin(), classes.end(), mem_fun_ref(&MyClass::DoSomething));
if the classes vector was defined something like this
vector
then you would use something like this
for_each(classes.begin(), classes.end(), mem_fun(&MyClass::DoSomething));
C++ : Template specializations
Assuming you have a piece of code that is templatized and looks like this
template
void prettyPrint(T value, char* buf);
now assume that you are using sprintf for formatting the input .
that being the case you can use template specializations like this :
template<> void prettyPrint(int value, char* buf){
sprintf(buf, "%d", value);
}
template<> void prettyPrint(char value, char* buf){
sprintf(buf, "%c", value);
}
template
void prettyPrint(T value, char* buf);
now assume that you are using sprintf for formatting the input .
that being the case you can use template specializations like this :
template<> void prettyPrint
sprintf(buf, "%d", value);
}
template<> void prettyPrint
sprintf(buf, "%c", value);
}
Tuesday, June 03, 2008
C++: copy magic
Say if you have a vector and you want to print it . Then instead of the normal for loop and cout call inside the loop you can do the following :
if you have a vector "v"
copy(v.begin(), v.end(), ostream_iterator(cout, "\n"));
this will print the entire vector .
To generalize this you could use something like this :
template
OutputIterator copy(const Container& c, OutputIterator result){
return std::copy(c.begin(), c.end(), result);
}
if you have a vector "v"
copy(v.begin(), v.end(), ostream_iterator
this will print the entire vector .
To generalize this you could use something like this :
template
OutputIterator copy(const Container& c, OutputIterator result){
return std::copy(c.begin(), c.end(), result);
}
C++ : pragma warning
if there is a header file after including which you start getting warnings , and especially if that header file is a third party header file , its better to wrap the header file with your own custom wrapper .
#pragma warning(push)
#pragma warning(disable:line_number_causing_warning)
#include
#pragma warning(pop)
#pragma warning(push)
#pragma warning(disable:line_number_causing_warning)
#include
#pragma warning(pop)
Subscribe to:
Posts (Atom)
Labels
. linux
(1)
algorithm
(15)
analytics
(1)
bash
(2)
bigoh
(1)
bruteforce
(1)
c#
(1)
c++
(40)
collections
(1)
commands
(2)
const
(1)
cosine similarity
(1)
creating projects
(1)
daemon
(1)
device_drivers
(1)
eclipse
(6)
eclipse-plugin-development
(9)
equals
(1)
formatting
(1)
freebsd
(1)
game programming
(1)
hashcode
(1)
heap
(1)
heaps
(1)
immutable-objects
(1)
java
(19)
JDT
(1)
kernel
(1)
linux
(4)
little sugar
(23)
logging
(1)
machine learning
(1)
marker-resolution
(1)
markers
(1)
mergesort
(1)
mixins
(1)
numbers
(1)
opengl
(2)
patterns
(2)
priority-queue
(1)
programming
(51)
ps
(1)
ranking
(1)
refactoring
(3)
references
(1)
security
(1)
set
(1)
shell
(1)
similarity
(1)
statistics
(1)
stl
(1)
tetris
(1)
threads
(1)
trees
(2)
unicode
(1)
unix
(2)
views
(2)
windows programming
(2)
XNA
(1)