Home Features
Features
Glossary of Mortgage-Backed Securities PDF Print E-mail
Written by FemiByte   
Friday, 05 September 2014 04:54

 

 

 

  • MBS Deal
    A set of structured bonds linked to a common pool (or pools) of mortgages

 

Last Updated on Friday, 05 September 2014 05:02
 
Variable storage in Java PDF Print E-mail
Written by FemiByte   
Friday, 16 May 2014 04:33

In order to figure out where a variable is stored in Java the most important factor is where the variable is declared.
A general rule of thumb is this:

 

  • local variables are stored on the stack
  • instance variables are stored on the heap
  • static variables are stored on the PermGen area of the heap

 

There are caveats to this however, which are explained below:

Variable Storage Details

Local variables

Primitives and object references declared in a method will be stored on the stack. However, the actual object, if created using new() will be stored on the heap, regardless of where the declaration took place. Hence in the following piece of code:
void aMethod()
{
int playerNum=5;
Player pl=new Player();
}

The primitive variable playerNum and object reference variable pl will be stored on the stack, while the actual Player object itself will live on the heap. When the code exits aMethod and goes out of scope, playerNum and pl will be popped from the stack and cease to exist but the Player object will persist on the heap until it is eventually garbage collected.

Instance variables

Instance variables, even primitives live on the stack.

Consider the code below:

public class Car {
int vinNumber;
String make;
String model;
int year;
String class;

Car(int vin, String make, String model, int year, String class)
{
this.vinNumber=vin;
this.make=make;
this.model=model;
this.year=year;
this.class=class;
}
...
public static void main(String[] args)
{
Car c=new Car(19281,"Audi", "A6",2012,"sedan");

}

 

Since an instance of Car can only be instantiated via a call to new(), we see that:

  • The Car object c lives on the heap
  • All instance primitives and objects that are part of the Car object are also stored on the heap.

Static variables

The rule for static variables is this: Static methods, primitive variables and object references are stored in the PermGen section of the heap since they are part of the reflection i.e. class, not instance related data. However, in the case of objects, the actual object itself is stored in the regular areas of the heap (young/old generation or survivor space).

 

References:

  1. http://www.tutorialspoint.com/java/java_variable_types.htm
  2. http://www.coderanch.com/t/202217/Performance/java/JVM-heap-stores-local-objects
  3. http://stackoverflow.com/questions/8387989/where-is-a-static-method-and-a-static-variable-stored-in-java-in-heap-or-in-sta
  4. http://stackoverflow.com/questions/3698078/where-does-the-jvm-store-primitive-variables
Last Updated on Friday, 16 May 2014 04:48
 
Pass-by-value vs Pass-by-reference in Java and C++ PDF Print E-mail
Written by FemiByte   
Tuesday, 29 April 2014 03:36

 

Pass-by-value vs Pass-by-reference in Java and C++

 

In this article I illustrate what it means to pass-by-value as opposed to pass-by-reference with a focus on Java vs C++.

The question often asked is this : Is Java pass-by-reference ?
A common and often erroneous answer is : Java is pass by reference for objects, and pass-by-value for primitives.
This is WRONG. To illustrate why this is so, let me refer you to this quote by the father
of Java himself, James Gosling:

Some people will say incorrectly that objects are passed “by reference.” In programming language design, the term pass by reference properly means that when an argument is passed to a function, the invoked function gets a reference to the original value, not a copy of its value. If the function modifies its parameter, the value in the calling code will be changed because the argument and parameter use the same slot in memory…. The Java programming language does not pass objects by reference; it passes object references by value. Because two copies of the same reference refer to the same actual object, changes made through one reference variable are visible through the other. There is exactly one parameter passing mode — pass by value — and that helps keep things simple.

– James Gosling, et al., The Java Programming Language, 4th Edition

The above clearly states that Java passes object references by value meaning that when the reference is passed, a copy of that reference (which is an address) is passed. Since the copy of the reference and the reference refer to the same object, if a call is made to a method that modifies the object in Java, that object is modified, hence the line “Because two copies of the same reference refer to the same actual object, changes made through one reference variable are visible through the other”.

I will now illustrate what pass-by-reference means, via a clear example in C++.

Let us create the following files in an appropriate directory with the following contents:

PassByReference.hpp:
#ifndef PassByReference_hpp
#define PassByReference_hpp
void swapIntByRef(int& iParam, int& jParam);
void swapIntByVal(int iParam, int jParam);
#endif

PassByReference.cpp:
#include <iostream>
#include "PassByReference.hpp"
using namespace std;

int main()
{
int i=1000;
int j=2300;

cout << "Illustration of Pass By Reference:\n";
cout << "Before: i= " << i << " j=" << j;
cout << "\n";
swapIntByRef(i,j);
cout << "After: i= " << i << " j=" << j;
cout << "\n";

cout << "\nIllustration of Pass By Value:\n";

i=1100;
j=2500;

cout << "Before: i= " << i << " j=" << j;
cout << "\n";
swapIntByVal(i,j);
cout << "After: i= " << i << " j=" << j;
cout << "\n";

}

void swapIntByRef(int& iParam, int& jParam)
{
int temp(iParam);
iParam=jParam;
jParam=temp;
}

void swapIntByVal(int iParam, int jParam)
{
int temp(iParam);
iParam=jParam;
jParam=temp;
}

We now compile and run the code (assuming you have the g++ compiler):

g++ -o PassByReference PassByReference.cpp
./PassByReference
Illustration of Pass By Reference:
Before: i=1000 j=2300
After: i=2300 j=1000

Illustration of Pass By Value:
Before: i=1100 j=2500
After: i=1100 j=2500

The results above perfectly illustrate the difference between passing by reference vas pass-by-value, at least from the C++ point of view.
By using the reference operator &, when the value of i is passed to the swapIntByReffunction, the actual parameter value is modified in the function such that when the function returns back to the main() function that calls it and the values of i and j are printed out, the values of i and j have been swapped.

In the latter case of pass-by-value, copies of i and j are passed, not references via the & operator.
The result of this is that even though an attempt is made to swap the values in theswapIntByVal function, the original actual parameter values remain unchanged, and this is what we see in the result.

The latter case is what prevails in Java even for all cases, even in the case of objects.

Here is an illustration in Java for both primitives and object references:

Create the file PassByValueDemo.java:

public class PassByValueDemo {

public static void main(String[] args) {
int i=1000;
int j=2300;
System.out.println("Primitives Case");
System.out.println("----------------");
System.out.println(" Before: i=" + i + " j=" + j);
swapInt(i,j);

System.out.println(" After: i=" + i + " j=" + j + "\n");

System.out.println("Wrapper Case");
System.out.println("--------------");
Integer iw=1000;
Integer jw=2300;
System.out.println(" Before: iw=" + iw + " jw=" + jw);
swapInteger(iw,jw);

System.out.println(" After: iw=" + iw + " jw=" + jw);

}

static void swapInt(int iParam, int jParam)
{
int temp=jParam;
jParam=iParam;
iParam=temp;
System.out.println(" iParam=" + iParam + " jParam=" + jParam);

}

static void swapInteger(Integer iParam, Integer jParam)
{
Integer temp=jParam;
jParam=iParam;
iParam=temp;
System.out.println(" iParam=" + iParam + " jParam=" + jParam);
}

}

We now compile and run the code:

javac PassByValueDemo.java

java PassByValueDemo

which produces:

Primitives Case
----------------
Before: i=1000 j=2300
iParam=2300 jParam=1000
After: i=1000 j=2300

Wrapper Case
--------------
Before: iw=1000 jw=2300
iParam=2300 jParam=1000
After: iw=1000 jw=2300

Thus we can see that in both cases of primitive and wrapper classes the values of the actual parameters i and j remain unchanged in the calling routine main. There is no way to achieve the effect we observed in the C++ method PassByRef in Java where the original actual parameters are changed. The underlying object that the reference refers to can be changed via a call to a modifying method on the referenced object, but the reference parameter is always a copy of the original actual parameter.

Summary

  • C++ supports pass-by-value and pass by reference via its & operator.
  • Java supports pass-by-value ONLY. What is erroneously thought of as pass-by-reference is really pass-by-value of an object reference.
Last Updated on Tuesday, 29 April 2014 06:39
 
Data Analytics: Summaries on Strang's Linear Algebra - Singular Value Decomposition PDF Print E-mail
Written by FemiByte   
Monday, 01 April 2013 02:48

Singular Value Decomposition

Singular Value Decomposition is the culmination of concepts in the course. It involves a factorization into orthogonal x diagonal x orthogonal matrices We look for the case where matrix A takes an orthogonal basis of vectors from the row space into an orthogonal basis of vectors in the column space.
On SVD the goal is to find

  1. orthonormal vectors $v_1, v_2, \cdots v_n$ in the row space $R^n$
  2. orthonormal vectors $uv_1, uv_2, \cdots u_m$ in the column space $R^m$
  3. multiplicands $\sigma_1 > 0, \sigma_2>0, \cdots \sigma_n > 0$ s.t $Av_1=\sigma_1 u_1 \text{ }Av_2=\sigma_2 u_2 \cdots Av_r=\sigma_r u_r$
    In this case $A$ is diagonalized

The singular vectors $v_1,\cdots v_r$ are in the row space of $A$ and are the eigenvectors of $A^TA$ and the outputs $u_1,\cdots u_r$ are in the column space of $A$ and are the eigenvectors of $AA^T$. The singular values $\sigma_1,\cdots \sigma_r$ are all +ve. We have that \[ A \left[ \begin{array}{ccc} \\v_1&&\cdots&&v_r\\ \end{array}\right]= \left[ \begin{array}{ccc} \\u_1&&\cdots&&u_r\\ \end{array}\right] \left[ \begin{array}{ccc}\sigma_1&& && \\ &&.&& \\ && &&\sigma_r\end{array}\right] \] The above relationship is the heart if the SVD and can be expressed as $AV=U\Sigma$. We also need $n-r$ more $v$'s and $m-r$ more $u$'s, from the nullspace $N(A)$ and the left nullspace $N(A^T)$. This makes $V$ now a square orthogonal matrix and the core relationship for SVD becomes: \[ A=U \Sigma V^T = u_1 \sigma_1 v_1^T + \cdots + u_r \sigma_r v_r^T \]

The Bases and the SVD

In the 2x2 case we diagonalize the crucial symmetric matrix $A^TA$, whose eigenvalues are $\sigma_1^2$ and $\sigma_2^2$:
Eigenvalues $\sigma_1^2 \sigma_2^2$, Eigenvectors $v_1, v_2$ \[ A^TA = V \left[\begin{array}{cc}\sigma_1^2&&0\\0&&\sigma_2^2\end{array} \right] V^T\] Rule:
Compute the eigenvectors $v$ and eigenvalues $\sigma^2$ of $A^TA$. Then each $u=Av/\sigma$. The matrices $U$ and $V$ contain orthornormal bases for all 4 subspaces:

  • first $r$ columns of $V$: row space of $A$
  • first $n-r$ columns of $V$: nullspace of $A$
  • first $r$ columns of $U$: column space of $A$
  • first $m-r$ columns of $U$: nullspace of $A^T$

Key Ideas

  1. The SVD factors $A$ into $U\Sigma V^T$, with $r$ singular values $\sigma_1 \geq \cdots \sigma_r > 0 $.
  2. The numbers $\sigma_1 ^2 \cdots \sigma_r ^2$ are the nonzero eigenvalues of $AA^T$ and $A^TA$.
  3. The orthonormal columns of $U$ and $V$ are eigenvectors of $AA^T$ and $A^TA$.
  4. Those columns hold orthonormal bases for the 4 fundamental subspaces of $A$.
  5. Those bases diagonalize the matrix: $Av_i=\sigma_i u_i$ for $i \leq r$. This is $AV=U\Sigma$
Last Updated on Monday, 01 April 2013 02:52
 
Data Analytics: Summaries on Strang's Linear Algebra - Similar Matrices PDF Print E-mail
Written by FemiByte   
Thursday, 21 March 2013 08:29

Similar Matrices

Let $M$ be any invertible matrix. Then $B=M^{-1}AM$ is similar to $A$. Similar matrices $A$ and $M^{-1}AM$ have the same eigenvalues. If $x$ is an eigenvector of $A$, then $M^{-1}x$ is an eigenvector of $B=M^{-1}AM$. Matrices that are similar to each other form a family of related matrices, all having the same eigenvalues.

If $A$ has $s$ independent eigenvectors, it is similar to a matrix $J$ that has $s$ Jordan blocks on its diagonal: Some matrix $M$ puts $A$ into Jordan form:

Jordan form $M^{-1}AM=\left[\begin{array}{ccccc}J_1&& && && && \\ &&.&& && &&\\ && &&.&& &&\\ && && && &&J_s&&\end{array}\right] = J$
Each block in $J$ has one eigenvalue $\lambda_i$, one eigenvector and 1's above the diagonal:
Jordan block $J_i=\left[\begin{array}{ccccc}\lambda_i&&1&& && && \\ &&. &&. && &&\\ && &&.&& &&\\ && && && &&\lambda_i&&\end{array}\right] = J$
$A$ is similar to $B$ if they share the same Jordan form $J$ - not otherwise.

Last Updated on Thursday, 21 March 2013 08:30
 
«StartPrev123NextEnd»

Page 1 of 3

joomla 1.5 stats