tag:blogger.com,1999:blog-46005270510192001122024-03-13T20:36:49.457-07:00Yet Another Coder's BlogThis blog is made to post some interesting things on Software Development and Quantitative Analysis. (T.Liu)Quantitative Coderhttp://www.blogger.com/profile/17386008753838448085noreply@blogger.comBlogger49125tag:blogger.com,1999:blog-4600527051019200112.post-53847546397131435292020-04-11T13:12:00.003-07:002020-04-11T13:12:49.427-07:00MongoDB: Analyzing Data with AggregationHere is the sample code to analyze data with the Aggregation Framework within MongoDB.<br />
<br />
<br />
<div class="separator" style="clear: both; text-align: center;">
<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhSidECp2OIKq6SUN5Ox9bF5dJyLeXhAXclz9k6pS6TnRYRlTwwY2aFkqVQO-KvdEVzRp-SAkFIhRM4zXe3n2RDMOj6omu6t8TyFcDxUB6OR5PXkcm9HgqtGR3IeQJQB-ypDskSnaNJlJ4w/s1600/mongodb-pipeline.png" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" data-original-height="668" data-original-width="742" height="576" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhSidECp2OIKq6SUN5Ox9bF5dJyLeXhAXclz9k6pS6TnRYRlTwwY2aFkqVQO-KvdEVzRp-SAkFIhRM4zXe3n2RDMOj6omu6t8TyFcDxUB6OR5PXkcm9HgqtGR3IeQJQB-ypDskSnaNJlJ4w/s640/mongodb-pipeline.png" width="640" /></a></div>
<br />Quantitative Coderhttp://www.blogger.com/profile/17386008753838448085noreply@blogger.com0tag:blogger.com,1999:blog-4600527051019200112.post-60983876001272561292019-04-14T22:01:00.000-07:002019-04-14T22:12:47.943-07:00Connect to Oracle Database from Python or R<b>1. To connect to Oracle in Python</b><br />
<br />
<span style="font-family: "verdana" , sans-serif;">1)<span style="background-color: white;"> </span></span><span style="background-color: white;"> <span style="color: #333333; font-family: "consolas" , "liberation mono" , "courier" , monospace; font-size: 16px;">conda install -c anaconda cx_oracle</span><span style="color: #333333; font-family: "consolas" , "liberation mono" , "courier" , monospace; font-size: 16px;"> </span></span><br />
<span style="color: #333333; font-size: 16px;"><br /><span style="background-color: white; font-family: "verdana" , sans-serif;">2.1) Method 1:</span></span><br />
<span style="background-color: white;"><span style="color: #333333; font-family: "consolas" , "liberation mono" , "courier" , monospace; font-size: 16px;"><br /></span>
<span style="color: #333333; font-family: "courier new" , "courier" , monospace;"> import pandas as pd</span></span><br />
<span style="background-color: white;"><span style="font-family: "courier new" , "courier" , monospace;"> import cx_Oracle</span></span><br />
<span style="background-color: white;"><span style="font-family: "courier new" , "courier" , monospace;"><br /></span></span>
<span style="background-color: #eeeff0; color: #333333; font-family: "courier new" , "courier" , monospace;"></span><br />
<pre style="background-color: white; list-style: none; padding: 0px;"><span style="font-family: "courier new" , "courier" , monospace;"> con = cx_Oracle.connect('pythonhol/welcome@127.0.0.1/orcl')
</span></pre>
<pre style="background-color: white; list-style: none; padding: 0px;"><span style="font-family: "courier new" , "courier" , monospace;">
</span></pre>
<pre style="background-color: white; list-style: none; padding: 0px;"><span style="font-family: "courier new" , "courier" , monospace;"> query = 'select * from departments order by department_id'</span> </pre>
<pre style="background-color: white; list-style: none; padding: 0px;"><span style="font-family: "courier new" , "courier" , monospace;"> df_ora = pd.read_sql(query, con=con)</span></pre>
<pre style="background-color: white; list-style: none; padding: 0px;"><span style="font-family: "courier new" , "courier" , monospace;"> df_ora.head()</span></pre>
<pre style="background-color: white; list-style: none; padding: 0px;"></pre>
<div>
<pre style="background-color: white; list-style: none; padding: 0px;"><span style="font-family: "courier new" , "courier" , monospace;"><span style="font-family: "courier new" , "courier" , monospace;"> con.close()</span></span></pre>
<span style="font-family: "courier new" , "courier" , monospace;">
</span></div>
<span style="background-color: #eeeff0; color: #333333;"><br /></span>
<span style="background-color: white; color: #333333; font-family: "consolas" , "liberation mono" , "courier" , monospace; font-size: 16px;">2.2) Method 2:</span><br />
<span style="background-color: white;"><span style="font-family: "courier new" , "courier" , monospace;"> import cx_Oracle</span></span><br />
<pre style="background-color: white; list-style: none; padding: 0px;"><span style="font-family: "courier new" , "courier" , monospace;"> con = cx_Oracle.connect('pythonhol/welcome@127.0.0.1/orcl')
</span></pre>
<pre style="background-color: white; list-style: none; padding: 0px;"><span style="font-family: "courier new" , "courier" , monospace;"> cur = con.cursor()</span></pre>
<pre style="background-color: white; list-style: none; padding: 0px;"><span style="font-family: "courier new" , "courier" , monospace;">
</span></pre>
<pre style="background-color: white; list-style: none; padding: 0px;"><span style="font-family: "courier new" , "courier" , monospace;"> query = 'select * from departments order by department_id'
cur.execute(query)
for result in cur:
print(result)
</span></pre>
<pre style="background-color: white; list-style: none; padding: 0px;"><span style="font-family: "courier new" , "courier" , monospace;"> cur.close()
con.close()</span></pre>
<pre style="-webkit-font-smoothing: antialiased; background-color: white; font-size: 12px; list-style: none; padding: 0px;"><span style="font-family: inherit;">
</span></pre>
<pre style="-webkit-font-smoothing: antialiased; background-color: white; font-size: 12px; list-style: none; padding: 0px;"></pre>
<pre style="-webkit-font-smoothing: antialiased; background-color: white; font-size: 12px; list-style: none; padding: 0px;"></pre>
<pre style="background-color: white; list-style: none; padding: 0px;"><div style="white-space: normal;">
<div style="font-family: "times new roman"; font-size: medium;">
<b>2. To connect to Oracle in R</b><br />
<b>
</b>
<b>
</b>
<b>
</b>
<b>
</b>
<b>
</b>
<b>
</b>
<b>
</b></div>
<div style="font-family: "times new roman"; font-size: medium;">
</div>
<div>
<span style="font-family: "verdana" , sans-serif; font-size: x-small;">
</span>
<span style="font-family: "verdana" , sans-serif;">
</span>
<span style="font-family: "verdana" , sans-serif;">1) </span><span style="font-family: "times new roman"; font-size: small;"> <span style="font-family: monospace; font-size: 14px;">install.packages("ROracle")</span></span><br />
<span style="font-family: "times new roman"; font-size: small;"><span style="font-family: monospace; font-size: 14px;">
</span></span>
<span style="font-family: "times new roman"; font-size: small;"><span style="font-family: monospace; font-size: 14px;">
</span></span>
<span style="font-family: "times new roman"; font-size: small;"><span style="font-family: monospace; font-size: 14px;">
</span></span>
<span style="font-family: "times new roman"; font-size: small;"><span style="font-family: monospace; font-size: 14px;">
</span></span>
<span style="font-family: "times new roman"; font-size: small;"><span style="font-family: monospace; font-size: 14px;">
</span></span>
<span style="font-family: "times new roman"; font-size: small;"><span style="font-family: monospace; font-size: 14px;">
</span></span>
<br />
<div style="font-size: medium;">
<span style="font-family: "times new roman";"><span style="font-family: monospace; font-size: 14px;">
</span></span></div>
</div>
<div style="font-family: "times new roman"; font-size: medium;">
<span style="font-family: monospace; font-size: 14px;">
</span></div>
<span style="font-family: "verdana" , sans-serif;">
</span>
<span style="font-family: "verdana" , sans-serif;">
</span>
<span style="font-family: "verdana" , sans-serif;">2)</span><span style="font-family: "verdana" , sans-serif;"> Method 1:</span><br />
<span style="font-family: "verdana" , sans-serif; font-size: x-small;">
</span>
<span style="font-family: "verdana" , sans-serif;">
</span>
<span style="font-family: "courier new" , "courier" , monospace;">
</span>
<span style="font-family: "courier new" , "courier" , monospace;">
</span>
<span style="font-family: "verdana" , sans-serif;">
</span>
<span style="font-family: "verdana" , sans-serif;">
</span>
<span style="font-family: "courier new" , "courier" , monospace;">
</span>
<span style="font-family: "courier new" , "courier" , monospace;">
library("ROracle")</span><br />
<span style="font-family: "courier new" , "courier" , monospace;"> drv = dbDriver("Oracle")</span><br />
<span style="font-family: "courier new" , "courier" , monospace;"> host = "hostname"</span><br />
<span style="font-family: "courier new" , "courier" , monospace;"> port = "1521"</span><br />
<span style="font-family: "courier new" , "courier" , monospace;"> sid = "SID_NAME"</span><br />
<span style="font-family: "courier new" , "courier" , monospace;"> connect.string = paste(
"(DESCRIPTION=",</span><br />
<span style="font-family: "courier new" , "courier" , monospace;"> "(ADDRESS=(PROTOCOL=tcp)(HOST=", host, ")(PORT=",port, "))",</span><br />
<span style="font-family: "courier new" , "courier" , monospace;"> "(CONNECT_DATA=(SID=", sid, ")))", sep="")</span><br />
<span style="font-family: "courier new" , "courier" , monospace;"> con = dbConnect(drv, username="uid", password="pwd", </span><br />
<span style="font-family: "courier new" , "courier" , monospace;"> dbname=connect.string, prefetch=FALSE,</span><br />
<span style="font-family: "courier new" , "courier" , monospace;"> bulk_read=1000L, stmt_cache=0L,</span><br />
<span style="font-family: "courier new" , "courier" , monospace;"> external_credentials=FALSE, sysdba=FALSE)</span><br />
<span style="font-family: "courier new" , "courier" , monospace;"> res = dbGetQuery(con, "select * from dual")</span><br />
<span style="font-family: "courier new" , "courier" , monospace;"> print(res)</span><br />
<span style="font-family: "courier new" , "courier" , monospace;"> dbDisconnect(con)</span><br />
<span style="font-family: "courier new" , "courier" , monospace;">
</span>
<span style="font-family: "courier new" , "courier" , monospace;">
</span>
<span style="font-family: "times new roman";">
</span>
<br />
<div style="font-family: "times new roman"; font-size: medium;">
</div>
<div style="font-family: "times new roman"; font-size: medium;">
</div>
<div style="font-family: "times new roman"; font-size: medium;">
</div>
</div>
<span style="font-size: 12px;">
</span><div style="font-size: 12px;">
</div>
<span style="font-size: 12px;">
</span><div style="font-size: 12px;">
</div>
<span style="font-size: 12px;">
</span></pre>
Quantitative Coderhttp://www.blogger.com/profile/17386008753838448085noreply@blogger.com0tag:blogger.com,1999:blog-4600527051019200112.post-1878524463637087652016-12-21T19:12:00.001-08:002017-01-02T21:23:27.562-08:00Quantopian(Python) vs Quantmod(R)<br />
<br />
1, <a href="http://www.eickonomics.com/posts/2014-03-25-python-vs-R-adding-TA-indicators/">Sid-by-side comparison</a>
<br />
<br />
2, Code snippet:<br />
<br />
from rpy2.robjects import r<br />
<div>
from pandas_datareader import data, wb</div>
import talib<br />
import numpy<br />
import matplotlib.pyplot as plt<br />
<div>
<br /></div>
<div>
IBM = r("getSymbols('IBM', src='google', from='2013-01-01')")</div>
<div>
f = data.DataReader(ticker,'google')</div>
<div>
f['SMA_50'] = talib.SMA(numpy.asarray(f['Close']), 50)</div>
<div>
f.plot(y= ['Close','SMA_20','SMA_50'], title='AAPL Close & Moving Averages')</div>
<div>
plt.show()</div>
<div>
<br /></div>
<div>
3. Module Installation</div>
<div>
<br /></div>
<div>
conda install rpy2</div>
<div>
conda install -c quantopian ta-lib</div>
<div>
<br /></div>
<div>
# more inteteresting modules</div>
<div>
conda install -c quantopian zipline</div>
<div>
conda install -c quantopian pyfolio</div>
<div>
conda install seaborn</div>
<div>
<div>
conda install quandl</div>
</div>
<div>
<br />
<br />
<br />
<a href="http://tradingwithpython.blogspot.com/" target="_blank"><span style="background-color: white; color: #222222; font-family: Arial, Tahoma, Helvetica, FreeSans, sans-serif; font-size: 12px;">Jev Kuznetsov's blog: </span>Trading with Python</a></div>
Quantitative Coderhttp://www.blogger.com/profile/17386008753838448085noreply@blogger.com0tag:blogger.com,1999:blog-4600527051019200112.post-83844886900114259562016-08-21T20:53:00.000-07:002016-08-24T18:34:33.242-07:00Covariance formula with CDF (Hoeffding's Covariance Identity)<a href="https://en.wikipedia.org/wiki/Covariance" target="_blank"><img alt="{\displaystyle \operatorname {cov} (X,Y)=\int _{\mathbb {R} }\int _{\mathbb {R} }F_{XY}(x,y)-F_{X}(x)F_{Y}(y)dxdy}" src="https://wikimedia.org/api/rest_v1/media/math/render/svg/bd2043207dae534a1132d623271c0bbf73659189" /></a><br />
<br />
<br />
A complete proof of above lemma can be found on page 241 (Lemma 7.27) of <span class="a-size-extra-large" id="productTitle" style="background-color: white; box-sizing: border-box; color: #111111; font-family: "arial" , sans-serif; line-height: 1.2;"><span style="font-size: xx-small;"><a href="https://www.amazon.com/Quantitative-Risk-Management-Techniques-Princeton/dp/0691166277/ref=sr_1_1?s=books&ie=UTF8&qid=1471836413&sr=1-1&keywords=quantitative+risk+management" target="_blank">Quantitative Risk Management: Concepts, Techniques and Tools</a></span></span><span style="color: #111111; font-family: "arial" , sans-serif; line-height: 1.2;">.</span><br />
<br />
Hint: 2\(cov(X_1, X_2) = E[(X_1-\tilde{X_1})(X_2-\tilde{X_2})]\),<br />
where \( (\tilde{X_1}, \tilde{X_2}) \) is an independent copy with the same joint distribution function as \( (X_1, X_2) \).<br />
<br />
Link to <a href="http://www.martinkeefe.com/math/mathjax3" target="_blank">MathJax</a><br />
<br />Quantitative Coderhttp://www.blogger.com/profile/17386008753838448085noreply@blogger.com0tag:blogger.com,1999:blog-4600527051019200112.post-48947184692584241462016-08-15T07:53:00.002-07:002016-08-15T08:33:29.659-07:00Mathematical Programming and its modeling languesPython is widely used in Mathematical Programming as a modeling language.<br />
<br /><br />
<br />
<br />
In commercial products, <strong>Gurobi</strong> has built its interactive shell in Python.<br />
<br />
<br />
<br /><br />
In open source world, <strong>Pyomo</strong> from Sandia National Lab use Python to offer an AMPL-like modeling language. Pyomo uses GLPK solver by default, but other solvers, such as GLPK, Gurobi, COIN CBC, can also be selected.<br />
<br />
<br />
<strong><br /></strong><br />
<strong>GLPK</strong> (GNU Linear Programming Toolkit) supports <strong>MathProg</strong>, which is also referred as <strong>GMPL</strong> (GNU Mathematical Programming Language). GLPK provides a command line tool <strong>glpsol</strong>, which is convenient for users to solve various optimization problems with well designed reports.<br />
<br />
<br />
<strong><br /></strong><br />
<strong>PuLP</strong> is an LP modeler written in Python. PuLP can generate MPS or LP files, and can call GLPK, COIN CLP.CBC, CPLEX and Gurobi to solve linear problems.<br />
<br /><br />
<a href="http://solverstudio.org/solverstudio/smalloverview/#main" target="_blank">SolverStudio</a> makes it easy to develop models inside Excel using Python. Data entered into the spreadsheet is automatically available to the model. SolverStudio supports PuLP, COOPR/Pyomo, AMPL, GMPL, GAMS, Gurobi, CMPL, SimPy.<br />
<br /><br />
<br />Quantitative Coderhttp://www.blogger.com/profile/17386008753838448085noreply@blogger.com0tag:blogger.com,1999:blog-4600527051019200112.post-6924836773919502262016-08-10T14:02:00.004-07:002016-08-10T14:02:43.057-07:00OLS in PythonThere are a few ways to perform Linear Regression(OLS) in Python. <br />
<br /><br />
Here is a short list of them:<br />
<br /><br />
1: > pandas.ols(y, x)<br />
2: > pandas.stats.api.ols(y ,x)<br />
3: > scipy.stats.linregress(x, y)<br />
4: > import statsmodels.formula.api as smf<br />
> results = smf.ols('Close ~ Open + Volume', data = df) # df is a DataFrame<br />
<br /><br />
<br />Quantitative Coderhttp://www.blogger.com/profile/17386008753838448085noreply@blogger.com0tag:blogger.com,1999:blog-4600527051019200112.post-31010261331519355992016-07-29T10:32:00.003-07:002016-07-29T10:34:26.356-07:00Interior Point MethodsInterior Point Methods are a class of optimization algorithms for solving linear or nonlinear programming problems.<br />
<br />
<br />
It finds the optimum solution by moving inside the polygon rather than moving around its surface.<br />
<br />
<br />
History:<br />
<br />
<br />
In 1984, Karmarkar "invented" interior-point method.<br />
In 1985, Affine-scalling method was "invented" as an intuitive version of Karmarkar's algorithm.<br />
In 1989, it was realized that Dikin(USSR) invented Affine-scaling(Barrier method) in 1967.<br />
<br />
<br />
Interior point method was the first practical polynomial time algorithm for solving linear programing problems. Ellipsoid method's run time is polynomial, but in practice, the Interior Point Method and variants of Simplex Methods are much faster.<br />
<br />
<br />
Here goes technical in summary:<br />
<br />
<br />
<strong>1. Primal objective with log barrier function: G(μ) = cx - μ Σ ln(xj)</strong><br />
<br />
<br />
<strong>2. Central path algorithm: μ from infinity to 0.</strong><br />
<br />
<br />
<strong>3. Min with constraint Ax=b?</strong><br />
<br />
∇G(μ) perpendicular to Ax=b;<br />
<cj-μ/xj> is linear combination of A's rows;<br />
<cj-μ/xj> = yA for some y;<br />
<br />
<br />
Let sj = μ/xj, then <br />
yA + s = c ==> yA ≤ <b></b> c, this the dual constraints.<br />
<br />
<br />
<strong>4, Duality gap: cx - yb</strong> = (yA+s)x - y (Ax)<br />
= sx<br />
<strong>= nμ</strong><br />
<br />
<strong>5.</strong> Conversely, if all sj xj = μ, then on central path.<br />
To follow <strong>Central Path</strong>, use "<strong>predictor-corrector</strong>".<br />
<br />
<br />
<strong>6. Improvement direction? "Affine-scaling"</strong><br />
From current x, s, μ ==> x+dx, s+ds, μ+dμ<br />
==> sj dxj + xj dsj = dμ (1)<br />
Also A(x+dx) = b ==> Adx = 0 (2)<br />
yA + s = c ==> (dy)A + ds = 0 (3)<br />
<br />
<br />
To solve (1)-(3), rescale "affine scaling", all xj = 1 ==> sj = μ<br />
The equations say<br />
μdx + ds = <strong>1</strong>dμ<br />
Adx = 0 ==> dx <img alt="\in \!\," aria-hidden="true" class="mwe-math-fallback-image-inline" src="https://wikimedia.org/api/rest_v1/media/math/render/svg/7bf13d1313b769789680da2f349d076e20881a75" style="height: 1.84ex; vertical-align: -0.33ex; width: 1.56ex;" /> A<br />
(dy)A + ds = 0 ==> ds <img alt="\in \!\," aria-hidden="true" class="mwe-math-fallback-image-inline" src="https://wikimedia.org/api/rest_v1/media/math/render/svg/7bf13d1313b769789680da2f349d076e20881a75" style="height: 1.84ex; vertical-align: -0.33ex; width: 1.56ex;" /> A<img alt="\bot \!\," aria-hidden="true" class="mwe-math-fallback-image-inline" src="https://wikimedia.org/api/rest_v1/media/math/render/svg/858206fa33dd605de90860d7a7663a2a116e31d1" style="height: 2.17ex; vertical-align: -0.33ex; width: 1.81ex;" /><br />
<br />
<br />
==> project <strong>1</strong>dμ into A and A<img alt="\bot \!\," aria-hidden="true" class="mwe-math-fallback-image-inline" src="https://wikimedia.org/api/rest_v1/media/math/render/svg/858206fa33dd605de90860d7a7663a2a116e31d1" style="height: 2.17ex; vertical-align: -0.33ex; width: 1.81ex;" /><br />
<br />
<br />
<br />
<br />
Note: some of the information comes from course "Advanced Algorithms" as follows:<br />
<br />
<br />
MIT <a href="http://courses.csail.mit.edu/6.854/current/" target="_blank">6.854/18.415J: Advanced Algorithms</a> (Fall 2014, <a href="http://people.csail.mit.edu/karger/" target="_blank">David Karger</a>)<br />
MIT <a href="http://people.csail.mit.edu/moitra/854.html" target="_blank">6.854/18.415 Advanced Algorithms</a> (Spring 2016, <a href="http://people.csail.mit.edu/moitra/">Ankur Moitra</a>)<br />
<br />Quantitative Coderhttp://www.blogger.com/profile/17386008753838448085noreply@blogger.com0tag:blogger.com,1999:blog-4600527051019200112.post-62999411486151569882016-05-23T13:03:00.000-07:002016-08-10T12:20:56.492-07:00Memoization in Python<span style="font-family: "courier new" , "courier" , monospace; font-size: medium;"># -*- coding: utf-8 -*-<br />"""</span><br />
<br />
<span style="font-family: "courier new"; font-size: medium;">File name: fib_mem.py</span><br />
<span style="font-family: "courier new" , "courier" , monospace; font-size: medium;"><br />Created on Mon May 23 14:50:39 2016</span><br />
<span style="font-family: "courier new" , "courier" , monospace;"><br /></span><span style="font-family: "courier new" , "courier" , monospace; font-size: medium;">Source: MITx: 6.00x Introduction to Computer Science and Programming</span><br />
<br />
<span style="font-family: "courier new" , "courier" , monospace; font-size: medium;">Output:</span><span style="font-family: "courier new" , "courier" , monospace; font-size: medium;"> 222232244629420445529739893461909967206666939096499764990979600</span><br />
<span style="font-family: "courier new" , "courier" , monospace;"><br /></span><span style="font-family: "courier new" , "courier" , monospace; font-size: medium;">PEP8 Style Compliant:</span><br />
<span style="font-family: "courier new" , "courier" , monospace; font-size: medium;">In Spyder: Preferences -> Editor -> Code Introspection/Analysis,<br /> near the bottom right, check Style analysis (pep8).</span><br />
<span style="font-family: "courier new";"><br /></span><span style="font-family: "courier new" , "courier" , monospace; font-size: medium;">The 2nd way to run it:</span><br />
<span style="font-family: "courier new" , "courier" , monospace; font-size: medium;"> - Comment out "@my_memoize"<br /> - Uncomment "fib = my_memoize(fib)"<br /> - Run</span><span style="font-family: "courier new"; font-size: medium;"><br /></span><br />
<span style="font-family: "courier new"; font-size: medium;">The 3rd way to run it:<br />
<span style="font-family: "courier new" , "courier" , monospace; font-size: medium;"> >> from fib_mem import fib</span><br />
<span style="font-family: "courier new" , "courier" , monospace; font-size: medium;"> >> print(fib(300))</span></span><span style="font-family: "courier new"; font-size: medium;"><br /></span><br />
<span style="font-family: "courier new"; font-size: medium;">Tested on Python 3.x</span><br />
<span style="font-family: "courier new" , "courier" , monospace; font-size: medium;">"""</span><br />
<span style="color: blue; font-family: "courier new" , "courier" , monospace; font-size: large;"><span style="color: blue; font-family: "courier new"; font-size: large;"><br /></span><br />
<span style="color: blue; font-family: "courier new"; font-size: large;">##########################################</span><br />
# Example 1</span><br />
<br />
<span style="font-family: "courier new" , "courier" , monospace; font-size: xx-small;"></span><br />
<span style="color: blue; font-family: "courier new" , "courier" , monospace; font-size: large;">def my_memoize(f):<br /> cache = {}</span><br />
<span style="font-family: "courier new" , "courier" , monospace;"><br /><span style="color: blue; font-family: "courier new" , "courier" , monospace; font-size: large;"> def helper(*x): # Refer to Item 18 of "Effective Python"<br /> if x not in cache:<br /> cache[x] = f(*x)<br /> return cache[x]<br /> return helper</span></span><br />
<span style="color: blue; font-family: "courier new" , "courier" , monospace; font-size: large;"></span><br />
<span style="color: blue; font-family: "courier new" , "courier" , monospace; font-size: large;">@my_memoize<br />def fib(n):<br /> if n <= 1:<br /> return n<br /> else:<br /> return fib(n-1) + fib(n-2)</span><br />
<span style="color: blue; font-family: "courier new" , "courier" , monospace; font-size: large;"># fib = memoize(fib)<br />print(fib(300))</span><br />
<span style="color: blue; font-family: "courier new"; font-size: large;"><br /></span><br />
<span style="color: blue; font-family: "courier new"; font-size: large;">##########################################</span><br />
<span style="color: blue; font-family: "courier new" , "courier" , monospace; font-size: large;"># Example 2 (stack overflow)</span><br />
<span style="color: blue; font-family: "courier new" , "courier" , monospace; font-size: large;">def functionDecorator(f):
</span><br />
<div style="-qt-block-indent: 0; -qt-user-state: 0; margin: 0px; text-indent: 0px;">
<span style="color: blue; font-family: "courier new" , "courier" , monospace; font-size: large;"> def new_f():</span></div>
<div style="-qt-block-indent: 0; -qt-user-state: 0; margin: 0px; text-indent: 0px;">
<span style="color: blue; font-family: "courier new" , "courier" , monospace; font-size: large;"> print("Begin", f.__name__)</span></div>
<div style="-qt-block-indent: 0; -qt-user-state: 0; margin: 0px; text-indent: 0px;">
<span style="color: blue; font-family: "courier new" , "courier" , monospace; font-size: large;"> foo() # using f() instead</span></div>
<div style="-qt-block-indent: 0; -qt-user-state: 0; margin: 0px; text-indent: 0px;">
<span style="color: blue; font-family: "courier new" , "courier" , monospace; font-size: large;"> print("End", f.__name__)</span></div>
<div style="-qt-block-indent: 0; -qt-user-state: 0; margin: 0px; text-indent: 0px;">
<span style="color: blue; font-family: "courier new" , "courier" , monospace; font-size: large;"> return new_f</span></div>
<span style="color: blue;"></span><br />
<span style="color: blue;"></span><br />
<span style="color: blue; font-family: "courier new" , "courier" , monospace; font-size: large;">@functionDecorator</span><br />
<span style="color: blue;"><span style="font-family: "courier new" , "courier" , monospace; font-size: large;">
</span><span style="font-family: "courier new" , "courier" , monospace; font-size: large;">def foo():</span></span><br />
<span style="color: blue; font-family: "courier new"; font-size: large;"> </span><span style="color: blue; font-family: "courier new" , "courier" , monospace; font-size: large;">print("inside foo()")</span><br />
<span style="color: blue;"></span><br />
<span style="color: blue;"></span><br />
<div style="-qt-block-indent: 0; -qt-user-state: 0; margin: 0px; text-indent: 0px;">
<span style="color: blue; font-family: "courier new" , "courier" , monospace; font-size: large;">foo()</span><br />
<span style="color: blue; font-family: "courier new" , "courier" , monospace; font-size: large;">print(foo.__name__)</span></div>
<span style="font-family: "courier new";"><br /><span style="color: blue; font-family: "courier new" , "courier" , monospace;"></span></span><br />
<span style="color: blue; font-family: "courier new" , "courier" , monospace;">###############################################################</span><br />
<span style="font-family: "courier new";"><br /><span style="font-family: "courier new" , "courier" , monospace;"></span></span><br />
<span style="font-family: "courier new";"><span style="font-family: "courier new" , "courier" , monospace;">"</span><a href="https://www.youtube.com/watch?v=F-sH1s11kZI" rel="nofollow" target="_blank"><span style="color: blue; font-family: "courier new" , "courier" , monospace;">Python is basically pseudo code</span></a><span style="font-family: "courier new" , "courier" , monospace;">, ..." -- <strong>Brett Slatkin</strong>, The author of "Effective Python".</span></span><span style="font-family: "courier new";"><br /></span><br />
<br />Quantitative Coderhttp://www.blogger.com/profile/17386008753838448085noreply@blogger.com0tag:blogger.com,1999:blog-4600527051019200112.post-89997117464121994562016-05-07T18:48:00.001-07:002016-05-07T19:03:56.807-07:00Matrix CalculusWhat's the partial derivatives (w.r.t. <span style="background-color: rgba(252 , 251 , 248 , 0.901961); color: #242729; font-size: 18px; white-space: nowrap;"><b>μ</b> and </span><span style="background-color: rgba(252 , 251 , 248 , 0.901961); color: #242729; font-size: 18px; white-space: nowrap;"><b>Σ</b></span>) of this function?<br />
<br />
<img alt="\ln(L)= -\frac{1}{2} \ln (|\boldsymbol\Sigma|\,) -\frac{1}{2}(\mathbf{x}-\boldsymbol\mu)^{\rm T}\boldsymbol\Sigma^{-1}(\mathbf{x}-\boldsymbol\mu) - \frac{k}{2}\ln(2\pi)" class="mwe-math-fallback-image-inline tex" src="https://upload.wikimedia.org/math/7/4/9/749d0c90d18ee0f64b9c36589b0c04f9.png" style="background-color: white; border: none; color: #252525; display: inline-block; font-family: sans-serif; font-size: 14px; line-height: 22.4px; vertical-align: middle;" /><br />
<br />
Yes, it's a beautiful formula: log-likelihood function of mvn distribution.<br />
<a href="http://www.math.uwaterloo.ca/~hwolkowi//matrixcookbook.pdf" target="_blank"><br /></a>
<a href="http://www.math.uwaterloo.ca/~hwolkowi//matrixcookbook.pdf" target="_blank">T</a>he answer can be found on page 40 of this book: <a href="http://www.math.uwaterloo.ca/~hwolkowi//matrixcookbook.pdf" target="_blank">The Matrix Cookbook</a><br />
<br />
You will find equation (81), (57) and (61) are useful to get the partial derivatives.<br />
<br />
<br />
The partial derivatives are used in Vibrato Monte Carlo method, which is a Path-wise/LRM hybrid method.<br />
<br />
Note that there are a few alternative approaches to valuate financial derivatives which have non-differentiable payoff functions.<br />
<br />
<ul>
<li>Likelihood Ratio Method (LRM) </li>
<li>Mallianvin Calculus (Stochastic Calculus of Variations)</li>
<li>"Vibrato" Monte Carlo Method</li>
</ul>
<br />
<br />Quantitative Coderhttp://www.blogger.com/profile/17386008753838448085noreply@blogger.com0tag:blogger.com,1999:blog-4600527051019200112.post-47859519347469676002016-01-24T19:26:00.002-08:002016-02-06T23:13:06.917-08:00QuantLib in C++QuantLib is an open-source C++ Library for quantitative analysis in Finance, and the QuantLib project was started by a few Quants in 2000. Now QuantLib project is Luigi Ballabio and ferninando Ametrano.<br />
<br />
Secondly, QuantLib has been ported to other languages:<br />
<br />
R: RQuantLib<br />
Python: PyQL<br />
Java: JQuantLib<br />
Excel: QuantLibXL<br />
<br />
QuantLib.org provides a very good <a href="http://quantlib.org/reference/classes.html" target="_blank">API Doc</a>, but you may still want to take a look at other sources for API documents. The following is a short list of links for QuantLib API Docs.<br />
<br />
<a href="http://sourcecodebrowser.com/quantlib/1.2/dirs.html" target="_blank">QuantLib SourceCodeBrowser</a><br />
<a href="http://www.jquantlib.com/en/latest/_static/javadocs/latest/jquantlib/index.html" target="_blank">QuantLib Java API Docs</a><br />
<a href="http://www.javaquant.net/books/quantlib.pdf" target="_blank">QuantLib API Docs generated by Doxygen</a>(v0.3.4)<br />
<a href="http://www.implementingquantlib.com/p/the-book.html" target="_blank">Implementing QuantLib</a><br />
<a href="http://www.markjoshi.com/design/index.htm" target="_blank">C++ Design Patterns and Derivatives Pricing 2e</a><br />
<a href="https://www.youtube.com/watch?v=e26P26kQsxc" target="_blank">QuantLib on YouTube</a><br />
<br />
In addition, some commercial software products are also available: QRM, FinCAD, Numerix, SunGard-FastVal, Savvysoft, Quantifi, Pricing Partners Cie, Bloomberg, Intex.<br />
<br />
http://libguides.caltech.edu/LindeFinanceQuantitative Coderhttp://www.blogger.com/profile/17386008753838448085noreply@blogger.com0tag:blogger.com,1999:blog-4600527051019200112.post-61939601290954434962016-01-09T20:06:00.001-08:002016-01-09T20:08:37.415-08:00Print a float or double in C++?<span style="font-family: Courier New, Courier, monospace; font-size: xx-small;">#include <iostream></span><br />
<span style="font-family: Courier New, Courier, monospace; font-size: xx-small;">#include <bitset></span><br />
<span style="font-family: Courier New, Courier, monospace; font-size: xx-small;">#include <cassert></span><br />
<span style="font-family: Courier New, Courier, monospace; font-size: xx-small;"><br /></span>
<span style="font-family: Courier New, Courier, monospace; font-size: xx-small;">using namespace std;</span><br />
<span style="font-family: Courier New, Courier, monospace; font-size: xx-small;"><br /></span>
<span style="font-family: Courier New, Courier, monospace; font-size: xx-small;">int main(void)</span><br />
<span style="font-family: Courier New, Courier, monospace; font-size: xx-small;">{</span><br />
<span style="font-family: Courier New, Courier, monospace; font-size: xx-small;"><span class="Apple-tab-span" style="white-space: pre;"> </span>const int n = sizeof(float)* 8; //32 bits</span><br />
<span style="font-family: Courier New, Courier, monospace; font-size: xx-small;"><span class="Apple-tab-span" style="white-space: pre;"> </span>float f = 975.75;</span><br />
<span style="font-family: Courier New, Courier, monospace; font-size: xx-small;"><span class="Apple-tab-span" style="white-space: pre;"> </span>unsigned int u;</span><br />
<span style="font-family: Courier New, Courier, monospace; font-size: xx-small;"><span class="Apple-tab-span" style="white-space: pre;"> </span>assert(sizeof(f) == sizeof(u));</span><br />
<span style="font-family: Courier New, Courier, monospace; font-size: xx-small;"><span class="Apple-tab-span" style="white-space: pre;"> </span>std::memcpy(&u, &f, sizeof(f));</span><br />
<span style="font-family: Courier New, Courier, monospace; font-size: xx-small;"><span class="Apple-tab-span" style="white-space: pre;"> </span>std::cout << n << ": " << bitset<n>(u) << endl;</span><br />
<span style="font-family: Courier New, Courier, monospace; font-size: xx-small;"><span class="Apple-tab-span" style="white-space: pre;"> </span>//32: 01000100011100111111000000000000</span><br />
<span style="font-family: Courier New, Courier, monospace; font-size: xx-small;"><br /></span>
<span style="font-family: Courier New, Courier, monospace; font-size: xx-small;"><span class="Apple-tab-span" style="white-space: pre;"> </span>const int nd = sizeof(double)* 8;</span><br />
<span style="font-family: Courier New, Courier, monospace; font-size: xx-small;"><span class="Apple-tab-span" style="white-space: pre;"> </span>double d = 975.75;</span><br />
<span style="font-family: Courier New, Courier, monospace; font-size: xx-small;"><span class="Apple-tab-span" style="white-space: pre;"> </span>unsigned long long ull;</span><br />
<span style="font-family: Courier New, Courier, monospace; font-size: xx-small;"><span class="Apple-tab-span" style="white-space: pre;"> </span>assert(sizeof(d) == sizeof(ull));</span><br />
<span style="font-family: Courier New, Courier, monospace; font-size: xx-small;"><span class="Apple-tab-span" style="white-space: pre;"> </span>std::memcpy(&ull, &d, sizeof(d));</span><br />
<span style="font-family: Courier New, Courier, monospace; font-size: xx-small;"><span class="Apple-tab-span" style="white-space: pre;"> </span>std::cout << nd << ": " << bitset<nd>(ull) << endl;</span><br />
<span style="font-family: Courier New, Courier, monospace; font-size: xx-small;"><span class="Apple-tab-span" style="white-space: pre;"> </span>//64: 0100000010001110011111100000000000000000000000000000000000000000</span><br />
<span style="font-family: Courier New, Courier, monospace; font-size: xx-small;"><br /></span>
<span style="font-family: Courier New, Courier, monospace; font-size: xx-small;"><span class="Apple-tab-span" style="white-space: pre;"> </span>std::system("pause");</span><br />
<span style="font-family: Courier New, Courier, monospace; font-size: xx-small;"><span class="Apple-tab-span" style="white-space: pre;"> </span>return 0;</span><br />
<span style="font-family: Courier New, Courier, monospace; font-size: xx-small;">}</span><br />
<br />
<span style="font-family: Trebuchet MS, sans-serif; font-size: xx-small;">To confirm the conversion, please check out: </span><span style="font-family: 'Trebuchet MS', sans-serif; font-size: xx-small;"><a href="http://www.binaryconvert.com/index.html">http://www.binaryconvert.com/index.html</a></span>Quantitative Coderhttp://www.blogger.com/profile/17386008753838448085noreply@blogger.com0tag:blogger.com,1999:blog-4600527051019200112.post-24517380172193854032015-12-30T23:19:00.000-08:002016-05-23T14:12:16.302-07:00Built-in Smart Pointers in Modern C++C++ is a general programming language that supports raw pointers. To use the raw pointers, we have to manage the memory carefully with new/delete, new[]/delete[], or perhaps C-style malloc/free pairs. The memory leak is always a potential risk -- imagine a <span style="color: blue; font-family: "courier new" , "courier" , monospace; font-size: x-small;">runtime_error</span> just occurred. Detecting tools like Valgrind or Garbage Collectors like Boehm GC[using mark-sweep algorithm] may be helpful to some extent, but it's still our responsibilities to make sure that the memory is properly managed and thus less time is left for the actual business needs.<br />
<br />
Smart pointer is one answer in the language level. Actually, smart pointers were introduced in C++98. With the <span style="font-family: inherit;">move </span>semantics, they got even better in C+11.<br />
<br />
Topics in C++ built-in smart pointers could be intricate if we dig them further deep into areas, such as GC algorithms, thread safety and exception safety. In this post, I'll compare the various smart pointers in a high level, and summarize it in a simple table. For the detailed discussion and the guidelines to use them, please refer to Chapter 4 of Scott Meyers' "<span style="font-family: "helvetica neue" , "arial" , "helvetica" , sans-serif; font-size: x-small;">Effective Modern C++</span>", or <a href="http://www.cplusplus.com/reference/memory/" rel="nofollow" target="_blank"><memory></a> on <span style="font-family: "helvetica neue" , "arial" , "helvetica" , sans-serif; font-size: x-small;">cplusplus.com</span>.<br />
<br />
<table border="1" cellpadding="0" cellspacing="0" class="MsoTableGrid" style="border-collapse: collapse; border-image: none; border: currentColor;">
<tbody>
<tr>
<td style="border-image: none; border: 1pt solid currentColor; padding: 0in 5.4pt; width: 41.75pt;" valign="top" width="84"><div class="MsoNormal" style="margin-bottom: 0pt;">
<br /></div>
</td>
<td style="border-image: none; border: 1pt solid currentColor; padding: 0in 5.4pt; width: 58.3pt;" valign="top" width="117"><div class="MsoNormal" style="margin-bottom: 0pt;">
<span style="font-size: 8pt;">raw pointer<o:p></o:p></span></div>
</td>
<td style="border-image: none; border: 1pt solid currentColor; padding: 0in 5.4pt; width: 49.35pt;" valign="top" width="99"><div class="MsoNormal" style="margin-bottom: 0pt;">
<span style="font-size: 8pt;">auto_ptr<o:p></o:p></span></div>
</td>
<td style="border-image: none; border: 1pt solid currentColor; padding: 0in 5.4pt; width: 117pt;" valign="top" width="234"><div class="MsoNormal" style="margin-bottom: 0pt;">
<span style="font-size: 8pt;">unique_ptr<o:p></o:p></span></div>
</td>
<td style="border-image: none; border: 1pt solid currentColor; padding: 0in 5.4pt; width: 112.5pt;" valign="top" width="225"><div class="MsoNormal" style="margin-bottom: 0pt;">
<span style="font-size: 8pt;">shared_ptr<o:p></o:p></span></div>
</td>
<td style="border-image: none; border: 1pt solid currentColor; padding: 0in 5.4pt; width: 99.9pt;" valign="top" width="200"><div class="MsoNormal" style="margin-bottom: 0pt;">
<span style="font-size: 8pt;">weak_ptr<o:p></o:p></span></div>
</td>
</tr>
<tr>
<td style="border-image: none; border: 1pt solid currentColor; padding: 0in 5.4pt; width: 41.75pt;" valign="top" width="84"><div class="MsoNormal" style="margin-bottom: 0pt;">
<span style="font-size: 8pt;">Language support<o:p></o:p></span></div>
</td>
<td style="border-color: currentColor; border-style: none solid solid none; border-width: medium 1pt 1pt medium; padding: 0in 5.4pt; width: 58.3pt;" valign="top" width="117"><div class="MsoNormal" style="margin-bottom: 0pt;">
<span style="font-size: 8pt;">Always allowed<o:p></o:p></span></div>
</td>
<td style="border-color: currentColor; border-style: none solid solid none; border-width: medium 1pt 1pt medium; padding: 0in 5.4pt; width: 49.35pt;" valign="top" width="99"><div class="MsoNormal" style="margin-bottom: 0pt;">
<span style="font-size: 8pt;">Deprecated in C++11<o:p></o:p></span></div>
</td>
<td style="border-color: currentColor; border-style: none solid solid none; border-width: medium 1pt 1pt medium; padding: 0in 5.4pt; width: 117pt;" valign="top" width="234"><div class="MsoNormal" style="margin-bottom: 0pt;">
<span style="font-size: 8pt;">C++11 (replacing auto_ptr)<o:p></o:p></span></div>
</td>
<td style="border-color: currentColor; border-style: none solid solid none; border-width: medium 1pt 1pt medium; padding: 0in 5.4pt; width: 112.5pt;" valign="top" width="225"><div class="MsoNormal" style="margin-bottom: 0pt;">
<span style="font-size: 8pt;">C++11<o:p></o:p></span></div>
</td>
<td style="border-color: currentColor; border-style: none solid solid none; border-width: medium 1pt 1pt medium; padding: 0in 5.4pt; width: 99.9pt;" valign="top" width="200"><div class="MsoNormal" style="margin-bottom: 0pt;">
<span style="font-size: 8pt;">C++11<o:p></o:p></span></div>
</td>
</tr>
<tr>
<td style="border-image: none; border: 1pt solid currentColor; padding: 0in 5.4pt; width: 41.75pt;" valign="top" width="84"><div class="MsoNormal" style="margin-bottom: 0pt;">
<span style="font-size: 8pt;">What are they<o:p></o:p></span></div>
</td>
<td style="border-color: currentColor; border-style: none solid solid none; border-width: medium 1pt 1pt medium; padding: 0in 5.4pt; width: 58.3pt;" valign="top" width="117"><div class="MsoNormal" style="margin-bottom: 0pt;">
<span style="font-size: 8pt;">T* t<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0pt;">
<span style="font-size: 8pt;">T *t[n]<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0pt;">
<br /></div>
</td>
<td style="border-color: currentColor; border-style: none solid solid none; border-width: medium 1pt 1pt medium; padding: 0in 5.4pt; width: 49.35pt;" valign="top" width="99"><div class="MsoNormal" style="margin-bottom: 0pt;">
<span style="font-size: 8pt;">Wrapper of raw pointer<o:p></o:p></span></div>
</td>
<td style="border-color: currentColor; border-style: none solid solid none; border-width: medium 1pt 1pt medium; padding: 0in 5.4pt; width: 117pt;" valign="top" width="234"><div class="MsoListParagraphCxSpFirst" style="margin: 0in 0in 0pt 0.25in; text-indent: -0.25in;">
<!--[if !supportLists]--><span style="font-family: "symbol"; font-size: 8pt;">·<span style="font-family: "times new roman"; font-size: 7pt; font-stretch: normal;"> </span></span><!--[endif]--><span style="font-size: 8pt;">A smart ptr uniquely
owned -- no two unique_ptr instances manage one object<o:p></o:p></span></div>
<div class="MsoListParagraphCxSpMiddle" style="margin: 0in 0in 0pt 0.25in; text-indent: -0.25in;">
<!--[if !supportLists]--><span style="font-family: "symbol"; font-size: 8pt;">·<span style="font-family: "times new roman"; font-size: 7pt; font-stretch: normal;"> </span></span><!--[endif]--><span style="font-size: 8pt;">It provides a
limited GC<o:p></o:p></span></div>
<div class="MsoListParagraphCxSpMiddle" style="margin: 0in 0in 0pt 0.25in; text-indent: -0.25in;">
<!--[if !supportLists]--><span style="font-family: "symbol"; font-size: 8pt;">·<span style="font-family: "times new roman"; font-size: 7pt; font-stretch: normal;"> </span></span><!--[endif]--><span style="font-size: 8pt;">It contains a
stored ptr and a stored deleter.<o:p></o:p></span><br />
<span style="font-size: 8pt;"> . Move-only type.</span></div>
</td>
<td style="border-color: currentColor; border-style: none solid solid none; border-width: medium 1pt 1pt medium; padding: 0in 5.4pt; width: 112.5pt;" valign="top" width="225"><div class="MsoListParagraphCxSpMiddle" style="margin: 0in 0in 0pt 0.25in; text-indent: -0.25in;">
<!--[if !supportLists]--><span style="font-family: "symbol"; font-size: 8pt;">·<span style="font-family: "times new roman"; font-size: 7pt; font-stretch: normal;"> </span></span><span style="font-size: 8pt;">A smart ptr shared
ownership group<o:p></o:p></span></div>
<div class="MsoListParagraphCxSpMiddle" style="margin: 0in 0in 0pt 0.25in; text-indent: -0.25in;">
<!--[if !supportLists]--><span style="font-family: "symbol"; font-size: 8pt;">·<span style="font-family: "times new roman"; font-size: 7pt; font-stretch: normal;"> </span></span><!--[endif]--><span style="font-size: 8pt;">It contains a
stored ptr and an owned ptr to control block.<o:p></o:p></span></div>
<div class="MsoListParagraphCxSpMiddle" style="margin: 0in 0in 0pt 0.25in; text-indent: -0.25in;">
<!--[if !supportLists]--><span style="font-family: "symbol"; font-size: 8pt;">·<span style="font-family: "times new roman"; font-size: 7pt; font-stretch: normal;"> </span></span><span style="font-size: 8pt;">Stored and owned
ptrs may refer to one object.<o:p></o:p></span></div>
<div class="MsoListParagraphCxSpMiddle" style="margin: 0in 0in 0pt 0.25in; text-indent: -0.25in;">
<!--[if !supportLists]--><span style="font-family: "symbol"; font-size: 8pt;">·<span style="font-family: "times new roman"; font-size: 7pt; font-stretch: normal;"> </span></span><!--[endif]--><span style="font-size: 8pt;">Empty shared_ptr<o:p></o:p></span></div>
<div class="MsoListParagraphCxSpMiddle" style="margin: 0in 0in 0pt 0.25in; text-indent: -0.25in;">
<!--[if !supportLists]--><span style="font-family: "symbol"; font-size: 8pt;">·<span style="font-family: "times new roman"; font-size: 7pt; font-stretch: normal;"> </span></span><!--[endif]--><span style="font-size: 8pt;">Null shared_ptr<o:p></o:p></span></div>
</td>
<td style="border-color: currentColor; border-style: none solid solid none; border-width: medium 1pt 1pt medium; padding: 0in 5.4pt; width: 99.9pt;" valign="top" width="200"><div class="MsoListParagraphCxSpMiddle" style="margin: 0in 0in 0pt 0.25in; text-indent: -0.25in;">
<!--[if !supportLists]--><span style="font-family: "symbol"; font-size: 8pt;">·<span style="font-family: "times new roman"; font-size: 7pt; font-stretch: normal;"> </span></span><span style="font-size: 8pt;">A smart ptr
holding non-owning ref. to an object managed by shared_ptr.<o:p></o:p></span></div>
<div class="MsoListParagraphCxSpMiddle" style="margin: 0in 0in 0pt 0.25in; text-indent: -0.25in;">
<!--[if !supportLists]--><span style="font-family: "symbol"; font-size: 8pt;">·<span style="font-family: "times new roman"; font-size: 7pt; font-stretch: normal;"> </span></span><!--[endif]--><span style="font-size: 8pt;">It models the temp
ownership<o:p></o:p></span><br />
<span style="font-size: 8pt;"><br /></span></div>
<div class="MsoListParagraphCxSpLast" style="margin: 0in 0in 0pt 0.25in;">
<br /></div>
</td>
</tr>
<tr>
<td style="border-image: none; border: 1pt solid currentColor; padding: 0in 5.4pt; width: 41.75pt;" valign="top" width="84"><div class="MsoNormal" style="margin-bottom: 0pt;">
<span style="font-size: 8pt;">Use Cases<o:p></o:p></span></div>
</td>
<td style="border-color: currentColor; border-style: none solid solid none; border-width: medium 1pt 1pt medium; padding: 0in 5.4pt; width: 58.3pt;" valign="top" width="117"><div class="MsoNormal" style="margin-bottom: 0pt;">
<span style="font-size: 8pt;">Almost never in practice<o:p></o:p></span></div>
</td>
<td style="border-color: currentColor; border-style: none solid solid none; border-width: medium 1pt 1pt medium; padding: 0in 5.4pt; width: 49.35pt;" valign="top" width="99"><div class="MsoNormal" style="margin-bottom: 0pt;">
<span style="font-size: 8pt;">Prefer to unique_ptr<o:p></o:p></span></div>
</td>
<td style="border-color: currentColor; border-style: none solid solid none; border-width: medium 1pt 1pt medium; padding: 0in 5.4pt; width: 117pt;" valign="top" width="234"><div class="MsoNormal" style="margin-bottom: 0pt;">
<span style="font-size: 8pt;"> . A ptr w/ exclusive
ownership.<o:p></o:p></span><br />
<span style="font-size: 8pt;"> . Used in Pimpl idiom</span></div>
</td>
<td style="border-color: currentColor; border-style: none solid solid none; border-width: medium 1pt 1pt medium; padding: 0in 5.4pt; width: 112.5pt;" valign="top" width="225"><div class="MsoNormal" style="margin-bottom: 0pt;">
<span style="font-size: 8pt;"> . A ptr w/ shared ownership.<o:p></o:p></span></div>
</td>
<td style="border-color: currentColor; border-style: none solid solid none; border-width: medium 1pt 1pt medium; padding: 0in 5.4pt; width: 99.9pt;" valign="top" width="200"><div class="MsoNormal" style="margin-bottom: 0pt;">
<span style="font-size: 8pt;"> . A shared_ptr like ptr in risk of
dangling.<o:p></o:p></span><br />
<span style="font-size: 8pt;"><br /></span></div>
</td>
</tr>
<tr>
<td style="border-image: none; border: 1pt solid currentColor; padding: 0in 5.4pt; width: 41.75pt;" valign="top" width="84"><div class="MsoNormal" style="margin-bottom: 0pt;">
<span style="font-size: 8pt;">How to use<o:p></o:p></span></div>
</td>
<td style="border-color: currentColor; border-style: none solid solid none; border-width: medium 1pt 1pt medium; padding: 0in 5.4pt; width: 58.3pt;" valign="top" width="117"><div class="MsoNormal" style="margin-bottom: 0pt;">
<span style="font-size: 8pt;">new/delete<br />
new[]/delete[]<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0pt;">
<br /></div>
</td>
<td style="border-color: currentColor; border-style: none solid solid none; border-width: medium 1pt 1pt medium; padding: 0in 5.4pt; width: 49.35pt;" valign="top" width="99"><div class="MsoNormal" style="margin-bottom: 0pt;">
<br /></div>
</td>
<td style="border-color: currentColor; border-style: none solid solid none; border-width: medium 1pt 1pt medium; padding: 0in 5.4pt; width: 117pt;" valign="top" width="234"><div class="MsoNormal" style="margin-bottom: 0pt;">
<span style="font-size: 8pt;">up = make_unique<T>();//C++14<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0pt;">
<span style="font-size: 8pt;">up = make_unique<T[]>();<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0pt;">
<span style="font-size: 8pt;">up.get_deleter();<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0pt;">
<span style="font-size: 8pt;">T* rp = up.get();<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0pt;">
<span style="font-size: 8pt;">T* rp = up.release();<br />
up.reset(p);//destroy & own p<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0pt;">
<span style="font-size: 8pt;">*up<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0pt;">
<span style="font-size: 8pt;">up->v1</span><br />
<span style="font-size: 10.66px;">shared_ptr<T> up{move(up)};</span></div>
</td>
<td style="border-color: currentColor; border-style: none solid solid none; border-width: medium 1pt 1pt medium; padding: 0in 5.4pt; width: 112.5pt;" valign="top" width="225"><div class="MsoNormal" style="margin-bottom: 0pt;">
<span style="font-size: 8pt;">sp = make_shared<T>(n);<br />
sp = make_shared<T[]>(n);<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0pt;">
<span style="font-size: 8pt;">sp.use_count()<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0pt;">
<span style="font-size: 8pt;">sp.unique()?<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0pt;">
<span style="font-size: 8pt;">T* rp = sp.get(); // stored ptr<br />
sp.reset();<br />
*sp<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0pt;">
<span style="font-size: 8pt;">sp->v1<o:p></o:p></span><br />
<span style="font-size: 8pt;">sp1=allocate_shared<T>(alloc,10);</span></div>
</td>
<td style="border-color: currentColor; border-style: none solid solid none; border-width: medium 1pt 1pt medium; padding: 0in 5.4pt; width: 99.9pt;" valign="top" width="200"><div class="MsoNormal" style="margin-bottom: 0pt;">
<span style="font-size: 8pt;">weak_ptr<T> wp(sp);<br />
sp1=wp.lock()<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0pt;">
<span style="font-size: 8pt;">wp.use_count()<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0pt;">
<span style="font-size: 8pt;">wp.expired()?<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0pt;">
<span style="font-size: 8pt;">wp.reset();<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0pt;">
<br /></div>
</td>
</tr>
<tr>
<td style="border-image: none; border: 1pt solid currentColor; padding: 0in 5.4pt; width: 41.75pt;" valign="top" width="84"><div class="MsoNormal" style="margin-bottom: 0pt;">
<span style="font-size: 8pt;">Pros<o:p></o:p></span></div>
</td>
<td style="border-color: currentColor; border-style: none solid solid none; border-width: medium 1pt 1pt medium; padding: 0in 5.4pt; width: 58.3pt;" valign="top" width="117"><div class="MsoNormal" style="margin-bottom: 0pt;">
<br /></div>
</td>
<td style="border-color: currentColor; border-style: none solid solid none; border-width: medium 1pt 1pt medium; padding: 0in 5.4pt; width: 49.35pt;" valign="top" width="99"><div class="MsoNormal" style="margin-bottom: 0pt;">
<br /></div>
</td>
<td style="border-color: currentColor; border-style: none solid solid none; border-width: medium 1pt 1pt medium; padding: 0in 5.4pt; width: 117pt;" valign="top" width="234"><div class="MsoListParagraphCxSpFirst" style="margin: 0in 0in 0pt 0.25in; text-indent: -0.25in;">
<!--[if !supportLists]--><span style="font-family: "symbol"; font-size: 8pt;">·<span style="font-family: "times new roman"; font-size: 7pt; font-stretch: normal;"> Small and fast. </span></span><!--[endif]--><span style="font-size: 8pt;">Little overhead
over raw pointer.<o:p></o:p></span></div>
<div class="MsoListParagraphCxSpLast" style="margin: 0in 0in 0pt 0.25in; text-indent: -0.25in;">
<!--[if !supportLists]--><span style="font-family: "symbol"; font-size: 8pt;">·<span style="font-family: "times new roman"; font-size: 7pt; font-stretch: normal;"> Easy to convert to shared_ptr</span></span><br />
<span style="font-size: xx-small;"> . Allowed to custom </span><span style="font-size: 10.66px; text-indent: -0.25in;">deleter (using lambda expression)</span><br />
<span style="font-size: 10.66px; text-indent: -0.25in;"> . Capture closure support</span></div>
</td>
<td style="border-color: currentColor; border-style: none solid solid none; border-width: medium 1pt 1pt medium; padding: 0in 5.4pt; width: 112.5pt;" valign="top" width="225"><div class="MsoNormal" style="margin-bottom: 0pt;">
<span style="font-size: xx-small;"> . Low overhead (2 x unique_ptr)</span><br />
<span style="font-size: xx-small;"> . Works in multi-threaded </span><br />
<span style="font-size: xx-small;"> environments.</span></div>
</td>
<td style="border-color: currentColor; border-style: none solid solid none; border-width: medium 1pt 1pt medium; padding: 0in 5.4pt; width: 99.9pt;" valign="top" width="200"><div class="MsoNormal" style="margin-bottom: 0pt;">
<span style="font-size: xx-small;"> . Prevent shared_ptr cycles.</span><br />
<span style="font-size: xx-small;"> . shared_ptr <==> weak_ptr</span></div>
</td>
</tr>
<tr>
<td style="border-image: none; border: 1pt solid currentColor; padding: 0in 5.4pt; width: 41.75pt;" valign="top" width="84"><div class="MsoNormal" style="margin-bottom: 0pt;">
<span style="font-size: 8pt;">Cons<o:p></o:p></span></div>
</td>
<td style="border-color: currentColor; border-style: none solid solid none; border-width: medium 1pt 1pt medium; padding: 0in 5.4pt; width: 58.3pt;" valign="top" width="117"><div class="MsoNormal" style="margin-bottom: 0pt;">
<br /></div>
</td>
<td style="border-color: currentColor; border-style: none solid solid none; border-width: medium 1pt 1pt medium; padding: 0in 5.4pt; width: 49.35pt;" valign="top" width="99"><div class="MsoNormal" style="margin-bottom: 0pt;">
<br /></div>
</td>
<td style="border-color: currentColor; border-style: none solid solid none; border-width: medium 1pt 1pt medium; padding: 0in 5.4pt; width: 117pt;" valign="top" width="234"><div class="MsoListParagraph" style="margin: 0in 0in 0pt 0.25in; text-indent: -0.25in;">
<!--[if !supportLists]--><span style="font-family: "symbol"; font-size: 8pt;">·<span style="font-family: "times new roman"; font-size: 7pt; font-stretch: normal;"> Not capoyable</span></span></div>
</td>
<td style="border-color: currentColor; border-style: none solid solid none; border-width: medium 1pt 1pt medium; padding: 0in 5.4pt; width: 112.5pt;" valign="top" width="225"><div class="MsoNormal" style="margin-bottom: 0pt;">
<span style="font-size: xx-small;"> . Circular reference</span></div>
</td>
<td style="border-color: currentColor; border-style: none solid solid none; border-width: medium 1pt 1pt medium; padding: 0in 5.4pt; width: 99.9pt;" valign="top" width="200"><div class="MsoNormal" style="margin-bottom: 0pt;">
<span style="font-size: xx-small;"> . Exception: bad_weak_ptr</span></div>
</td>
</tr>
</tbody></table>
<br />
<span style="font-family: "georgia" , "times new roman" , serif;"><br /></span>
<span style="font-family: "georgia" , "times new roman" , serif;"><span style="font-size: medium;"><br /></span>
<span style="font-family: "georgia" , "times new roman" , serif; font-size: medium;"><span style="color: blue;">"The present is the past rolled up for action, and the past is the present unrolled for understanding." - Will Durant.</span> </span></span>Quantitative Coderhttp://www.blogger.com/profile/17386008753838448085noreply@blogger.com0tag:blogger.com,1999:blog-4600527051019200112.post-45094184958486825892015-12-30T13:24:00.001-08:002015-12-30T22:20:24.847-08:00REPL, Online IDE and Tools for Static Code AnalysisThe code in general programming languages like Java and C++ and code is usually compiled and tested in IDEs. In other scripting languages, it's common to see a REPL (read-eval-print loop) language shell -- interactive interpreter.<br />
<br />
REPL environment allows us to run the code piece by piece. This is very handy for testing purpose sometimes. Now there are some solutions in Java and C++.<br />
[My thinking of picking a pair of similar tools comes from Hotelling's law -- "Linear City Model", though many more other tools are available, too.]<br />
<br />
1. <b>cint</b> and <b>igcc </b>are two REPL simulators for C/C++.<br />
2. <b>javarepl </b>and Eclipse's "<b>scrap book</b>" are two REPL simulators for Java.<br />
3. <b>ideone</b>, <b>codechef</b>, and <b>coding-ground</b> provide online compiler suites for various programming languages(C++, Java, Scala, R, Python) by using cloud computing technologies.<br />
4. <b>cppcheck </b>and <b>cpplint.py</b> are two tools for C++ static code analysis.<br />
<br />
<a href="http://www.tutorialspoint.com/codingground.htm" rel="nofollow" target="_blank"><b>Coding ground</b></a> is my favorite. It supports almost all popular languages, and claims 100% cloud. Best of all, it displays the command line and allows me to change the compiling options!<br />
<br />
<span style="font-size: xx-small;">* As of December 2015, coding ground works well on my PC. It has an Android app for Tutorialspoint, but it's slow and Coding Ground on my Galaxy Note 4 is not working as well as it is on PCs.</span><br />
<br />Quantitative Coderhttp://www.blogger.com/profile/17386008753838448085noreply@blogger.com2tag:blogger.com,1999:blog-4600527051019200112.post-8158569676842229712015-12-28T14:20:00.001-08:002015-12-30T23:20:00.527-08:00Move Semantics and Constructors<span style="font-family: inherit; font-size: xx-small;">C++11 includes four types of constructors:</span><br />
<ol>
<li><span style="font-family: inherit; font-size: xx-small;">Default Constructor</span></li>
<li><span style="font-family: inherit; font-size: xx-small;">Conversion Constructor (disabled if explicit)</span></li>
<li><span style="font-family: inherit; font-size: xx-small;">Copy Constructor (const &)</span></li>
<li><span style="font-family: inherit; font-size: xx-small;">Move Constructor (&&)</span></li>
</ol>
<span style="font-family: inherit; font-size: xx-small;">C++11 also allows two types of assignment operators:</span><br />
<ol>
<li><span style="font-family: inherit; font-size: xx-small;">(Copy) Assignment Operator (const &)</span></li>
<li><span style="font-family: inherit; font-size: xx-small;">Move Assignment Operator (&&)</span></li>
</ol>
<div>
<span style="font-size: xx-small;">The following is the sample code to demonstrate what they look like:</span><br />
<span style="font-size: xx-small;"><br /></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="color: blue; font-family: "Courier New"; font-size: 8.0pt; mso-fareast-font-family: "Times New Roman";">// MyCPP11Container.cpp<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<br /></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="color: blue; font-family: "Courier New"; font-size: 8.0pt; mso-fareast-font-family: "Times New Roman";">template<typename T, int value><o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="color: blue; font-family: "Courier New"; font-size: 8.0pt; mso-fareast-font-family: "Times New Roman";">class MyCPP11Container<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="color: blue; font-family: "Courier New"; font-size: 8.0pt; mso-fareast-font-family: "Times New Roman";">{<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="color: blue; font-family: "Courier New"; font-size: 8.0pt; mso-fareast-font-family: "Times New Roman";">public:<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="color: blue; font-family: "Courier New"; font-size: 8.0pt; mso-fareast-font-family: "Times New Roman";"> MyCPP11Container() { // 1. DEFAULT CTOR<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="color: blue; font-family: "Courier New"; font-size: 8.0pt; mso-fareast-font-family: "Times New Roman";"> /*intended to be empty*/<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="color: blue; font-family: "Courier New"; font-size: 8.0pt; mso-fareast-font-family: "Times New Roman";"> }<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="color: blue; font-family: "Courier New"; font-size: 8.0pt; mso-fareast-font-family: "Times New Roman";"> explicit MyCPP11Container(int)
: MyCPP11Container() { // 2. Disable
CONVERSION CTOR<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="color: blue; font-family: "Courier New"; font-size: 8.0pt; mso-fareast-font-family: "Times New Roman";">
// DELEGATE CONSTRUCTION<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="color: blue; font-family: "Courier New"; font-size: 8.0pt; mso-fareast-font-family: "Times New Roman";"> m_pData = new T(value);<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="color: blue; font-family: "Courier New"; font-size: 8.0pt; mso-fareast-font-family: "Times New Roman";"> }<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="color: blue; font-family: "Courier New"; font-size: 8.0pt; mso-fareast-font-family: "Times New Roman";"> MyCPP11Container(const
MyCPP11Container& other) : m_pData(NULL) { // 3. COPY CTOR<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="color: blue; font-family: "Courier New"; font-size: 8.0pt; mso-fareast-font-family: "Times New Roman";"> *this = other;<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="color: blue; font-family: "Courier New"; font-size: 8.0pt; mso-fareast-font-family: "Times New Roman";"> }<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="color: blue; font-family: "Courier New"; font-size: 8.0pt; mso-fareast-font-family: "Times New Roman";"> MyCPP11Container(MyCPP11Container&&
other) : m_pData(NULL) { // 4. MOVE
CTOR<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="color: blue; font-family: "Courier New"; font-size: 8.0pt; mso-fareast-font-family: "Times New Roman";"> *this = std::move(other);<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="color: blue; font-family: "Courier New"; font-size: 8.0pt; mso-fareast-font-family: "Times New Roman";"> }<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="color: blue; font-family: "Courier New"; font-size: 8.0pt; mso-fareast-font-family: "Times New Roman";"> virtual ~MyCPP11Container()
{ // DESTRUCTOR<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="color: blue; font-family: "Courier New"; font-size: 8.0pt; mso-fareast-font-family: "Times New Roman";"> if (m_pData
!= NULL) {<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="color: blue; font-family: "Courier New"; font-size: 8.0pt; mso-fareast-font-family: "Times New Roman";"> delete m_pData;<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="color: blue; font-family: "Courier New"; font-size: 8.0pt; mso-fareast-font-family: "Times New Roman";"> }<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="color: blue; font-family: "Courier New"; font-size: 8.0pt; mso-fareast-font-family: "Times New Roman";"> }<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<br /></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="color: blue; font-family: "Courier New"; font-size: 8.0pt; mso-fareast-font-family: "Times New Roman";"> MyCPP11Container&
operator=(const MyCPP11Container& other) { // ASSIGNMENT OPTR<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="color: blue; font-family: "Courier New"; font-size: 8.0pt; mso-fareast-font-family: "Times New Roman";"> if (this == &other) {<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="color: blue; font-family: "Courier New"; font-size: 8.0pt; mso-fareast-font-family: "Times New Roman";"> return *this;<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="color: blue; font-family: "Courier New"; font-size: 8.0pt; mso-fareast-font-family: "Times New Roman";"> }<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="color: blue; font-family: "Courier New"; font-size: 8.0pt; mso-fareast-font-family: "Times New Roman";"> delete m_pData;<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="color: blue; font-family: "Courier New"; font-size: 8.0pt; mso-fareast-font-family: "Times New Roman";"> m_pData = new
int(*(other.m_pData));<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="color: blue; font-family: "Courier New"; font-size: 8.0pt; mso-fareast-font-family: "Times New Roman";"> }<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<br /></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="color: blue; font-family: "Courier New"; font-size: 8.0pt; mso-fareast-font-family: "Times New Roman";"> MyCPP11Container&
operator=(MyCPP11Container&& other) { // MOVE ASSIGNMENT OPTR<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="color: blue; font-family: "Courier New"; font-size: 8.0pt; mso-fareast-font-family: "Times New Roman";"> if (this == &other) {<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="color: blue; font-family: "Courier New"; font-size: 8.0pt; mso-fareast-font-family: "Times New Roman";"> return *this;<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="color: blue; font-family: "Courier New"; font-size: 8.0pt; mso-fareast-font-family: "Times New Roman";"> }<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="color: blue; font-family: "Courier New"; font-size: 8.0pt; mso-fareast-font-family: "Times New Roman";"> delete m_pData;<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="color: blue; font-family: "Courier New"; font-size: 8.0pt; mso-fareast-font-family: "Times New Roman";"> m_pData = other.m_pData;<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="color: blue; font-family: "Courier New"; font-size: 8.0pt; mso-fareast-font-family: "Times New Roman";"> other.m_Ptr = nullptr; //
So the destructor doesn't free memory multiple times.<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="color: blue; font-family: "Courier New"; font-size: 8.0pt; mso-fareast-font-family: "Times New Roman";"> }<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<br /></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="color: blue; font-family: "Courier New"; font-size: 8.0pt; mso-fareast-font-family: "Times New Roman";"> operator T() const { return
*m_pData; } // type conversion: (T).<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<br /></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="color: blue; font-family: "Courier New"; font-size: 8.0pt; mso-fareast-font-family: "Times New Roman";">private:<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="color: blue; font-family: "Courier New"; font-size: 8.0pt; mso-fareast-font-family: "Times New Roman";"> T* m_pData;<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="color: blue; font-family: "Courier New"; font-size: 8.0pt; mso-fareast-font-family: "Times New Roman";">};<o:p></o:p></span></div>
<br />
<div class="MsoNormal">
<br /></div>
<div>
// C++11 version of swap in <utility> behaves like this:<br />
<br />
<pre style="font-size: 12px;"><code><var style="color: blue; font-style: normal;">template</var><span style="color: blue;"> <</span><var style="color: blue; font-style: normal;">class</var><span style="color: blue;"> T> </span><var style="color: blue; font-style: normal;">void</var><span style="color: blue;"> swap (T& a, T& b) </span>{<span style="color: blue;"> // moved in <utility></span>
T c(std::move(a)); a=std::move(b); b=std::move(c);
}</code></pre>
<pre style="font-size: 12px;"><code>
</code></pre>
Note that swap() has been in <algorithm> until C++11. std::move() in <utility> is the new semantic move, which is different from the ranged std::move() in <algorithm>.<br />
<br />
<pre style="font-size: 12px;"><span style="color: blue;">typename remove_reference<T>::type&& move (T&& arg) noexcept; // <utility></span></pre>
<pre style="font-size: 12px;"><span style="color: blue;">
</span></pre>
<pre style="font-size: 12px;"><span style="color: blue;">
</span></pre>
<pre style="color: green; font-size: 12px;"></pre>
</div>
Quantitative Coderhttp://www.blogger.com/profile/17386008753838448085noreply@blogger.com0tag:blogger.com,1999:blog-4600527051019200112.post-45479807000297296042015-12-28T09:41:00.001-08:002015-12-28T09:42:44.534-08:00Parallel Programming Paradigms - MPI, OpenMP and CUDA<table border="1" cellpadding="0" cellspacing="0" class="MsoTableGrid" style="border-collapse: collapse; border: none; mso-border-alt: solid windowtext .5pt; mso-padding-alt: 0in 5.4pt 0in 5.4pt; mso-table-layout-alt: fixed; mso-yfti-tbllook: 1184;">
<tbody>
<tr>
<td style="border: solid windowtext 1.0pt; mso-border-alt: solid windowtext .5pt; padding: 0in 5.4pt 0in 5.4pt; width: 59.4pt;" valign="top" width="119"><div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<br /></div>
</td>
<td style="border-left: none; border: solid windowtext 1.0pt; mso-border-alt: solid windowtext .5pt; mso-border-left-alt: solid windowtext .5pt; padding: 0in 5.4pt 0in 5.4pt; width: 139.5pt;" valign="top" width="279"><div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: inherit; font-size: xx-small;">MPI</span></div>
</td>
<td style="border-left: none; border: solid windowtext 1.0pt; mso-border-alt: solid windowtext .5pt; mso-border-left-alt: solid windowtext .5pt; padding: 0in 5.4pt 0in 5.4pt; width: 117.0pt;" valign="top" width="234"><div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: inherit; font-size: xx-small;">OpenMP</span></div>
</td>
<td style="border-left: none; border: solid windowtext 1.0pt; mso-border-alt: solid windowtext .5pt; mso-border-left-alt: solid windowtext .5pt; padding: 0in 5.4pt 0in 5.4pt; width: 162.9pt;" valign="top" width="326"><div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: inherit; font-size: xx-small;">CUDA</span></div>
</td>
</tr>
<tr>
<td style="border-top: none; border: solid windowtext 1.0pt; mso-border-alt: solid windowtext .5pt; mso-border-top-alt: solid windowtext .5pt; padding: 0in 5.4pt 0in 5.4pt; width: 59.4pt;" valign="top" width="119"><div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: inherit; font-size: xx-small;">Description</span></div>
</td>
<td style="border-bottom: solid windowtext 1.0pt; border-left: none; border-right: solid windowtext 1.0pt; border-top: none; mso-border-alt: solid windowtext .5pt; mso-border-left-alt: solid windowtext .5pt; mso-border-top-alt: solid windowtext .5pt; padding: 0in 5.4pt 0in 5.4pt; width: 139.5pt;" valign="top" width="279"><div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<br /></div>
</td>
<td style="border-bottom: solid windowtext 1.0pt; border-left: none; border-right: solid windowtext 1.0pt; border-top: none; mso-border-alt: solid windowtext .5pt; mso-border-left-alt: solid windowtext .5pt; mso-border-top-alt: solid windowtext .5pt; padding: 0in 5.4pt 0in 5.4pt; width: 117.0pt;" valign="top" width="234"><div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: inherit; font-size: xx-small;">Multi-threaded API</span></div>
</td>
<td style="border-bottom: solid windowtext 1.0pt; border-left: none; border-right: solid windowtext 1.0pt; border-top: none; mso-border-alt: solid windowtext .5pt; mso-border-left-alt: solid windowtext .5pt; mso-border-top-alt: solid windowtext .5pt; padding: 0in 5.4pt 0in 5.4pt; width: 162.9pt;" valign="top" width="326"><div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: inherit; font-size: xx-small;">Parallel Computing Platform<br />
and API (NVIDIA GPU)</span></div>
</td>
</tr>
<tr>
<td style="border-top: none; border: solid windowtext 1.0pt; mso-border-alt: solid windowtext .5pt; mso-border-top-alt: solid windowtext .5pt; padding: 0in 5.4pt 0in 5.4pt; width: 59.4pt;" valign="top" width="119"><div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: inherit; font-size: xx-small;">Compiler</span></div>
</td>
<td style="border-bottom: solid windowtext 1.0pt; border-left: none; border-right: solid windowtext 1.0pt; border-top: none; mso-border-alt: solid windowtext .5pt; mso-border-left-alt: solid windowtext .5pt; mso-border-top-alt: solid windowtext .5pt; padding: 0in 5.4pt 0in 5.4pt; width: 139.5pt;" valign="top" width="279"><div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: inherit; font-size: xx-small;">mpicc</span></div>
</td>
<td style="border-bottom: solid windowtext 1.0pt; border-left: none; border-right: solid windowtext 1.0pt; border-top: none; mso-border-alt: solid windowtext .5pt; mso-border-left-alt: solid windowtext .5pt; mso-border-top-alt: solid windowtext .5pt; padding: 0in 5.4pt 0in 5.4pt; width: 117.0pt;" valign="top" width="234"><div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: inherit; font-size: xx-small;">gcc –fompenmp -Igomp</span></div>
</td>
<td style="border-bottom: solid windowtext 1.0pt; border-left: none; border-right: solid windowtext 1.0pt; border-top: none; mso-border-alt: solid windowtext .5pt; mso-border-left-alt: solid windowtext .5pt; mso-border-top-alt: solid windowtext .5pt; padding: 0in 5.4pt 0in 5.4pt; width: 162.9pt;" valign="top" width="326"><div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: inherit; font-size: xx-small;">nvcc (LLVM-based)</span></div>
</td>
</tr>
<tr>
<td style="border-top: none; border: solid windowtext 1.0pt; mso-border-alt: solid windowtext .5pt; mso-border-top-alt: solid windowtext .5pt; padding: 0in 5.4pt 0in 5.4pt; width: 59.4pt;" valign="top" width="119"><div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: inherit; font-size: xx-small;">Headers</span></div>
</td>
<td style="border-bottom: solid windowtext 1.0pt; border-left: none; border-right: solid windowtext 1.0pt; border-top: none; mso-border-alt: solid windowtext .5pt; mso-border-left-alt: solid windowtext .5pt; mso-border-top-alt: solid windowtext .5pt; padding: 0in 5.4pt 0in 5.4pt; width: 139.5pt;" valign="top" width="279"><div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: inherit; font-size: xx-small;">#include <mpi.h></span></div>
</td>
<td style="border-bottom: solid windowtext 1.0pt; border-left: none; border-right: solid windowtext 1.0pt; border-top: none; mso-border-alt: solid windowtext .5pt; mso-border-left-alt: solid windowtext .5pt; mso-border-top-alt: solid windowtext .5pt; padding: 0in 5.4pt 0in 5.4pt; width: 117.0pt;" valign="top" width="234"><div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: inherit; font-size: xx-small;">#include <omp.h></span></div>
</td>
<td style="border-bottom: solid windowtext 1.0pt; border-left: none; border-right: solid windowtext 1.0pt; border-top: none; mso-border-alt: solid windowtext .5pt; mso-border-left-alt: solid windowtext .5pt; mso-border-top-alt: solid windowtext .5pt; padding: 0in 5.4pt 0in 5.4pt; width: 162.9pt;" valign="top" width="326"><div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: inherit; font-size: xx-small;">#include <cuda_runtime.h></span></div>
</td>
</tr>
<tr>
<td style="border-top: none; border: solid windowtext 1.0pt; mso-border-alt: solid windowtext .5pt; mso-border-top-alt: solid windowtext .5pt; padding: 0in 5.4pt 0in 5.4pt; width: 59.4pt;" valign="top" width="119"><div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: inherit; font-size: xx-small;">CFlags</span></div>
</td>
<td style="border-bottom: solid windowtext 1.0pt; border-left: none; border-right: solid windowtext 1.0pt; border-top: none; mso-border-alt: solid windowtext .5pt; mso-border-left-alt: solid windowtext .5pt; mso-border-top-alt: solid windowtext .5pt; padding: 0in 5.4pt 0in 5.4pt; width: 139.5pt;" valign="top" width="279"><div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: inherit; font-size: xx-small;">-I/usr/lib/openmpi<br />
-L/usr/lib/openmpi/…/lib<br />
-lmpi</span></div>
</td>
<td style="border-bottom: solid windowtext 1.0pt; border-left: none; border-right: solid windowtext 1.0pt; border-top: none; mso-border-alt: solid windowtext .5pt; mso-border-left-alt: solid windowtext .5pt; mso-border-top-alt: solid windowtext .5pt; padding: 0in 5.4pt 0in 5.4pt; width: 117.0pt;" valign="top" width="234"><div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: inherit; font-size: xx-small;">lgomp</span></div>
</td>
<td style="border-bottom: solid windowtext 1.0pt; border-left: none; border-right: solid windowtext 1.0pt; border-top: none; mso-border-alt: solid windowtext .5pt; mso-border-left-alt: solid windowtext .5pt; mso-border-top-alt: solid windowtext .5pt; padding: 0in 5.4pt 0in 5.4pt; width: 162.9pt;" valign="top" width="326"><div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: inherit; font-size: xx-small;">-I/usr/local/cuda/include<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: inherit; font-size: xx-small;">-L/usr/local/cuda/lib<br />
-lcudrt</span></div>
</td>
</tr>
<tr>
<td style="border-top: none; border: solid windowtext 1.0pt; mso-border-alt: solid windowtext .5pt; mso-border-top-alt: solid windowtext .5pt; padding: 0in 5.4pt 0in 5.4pt; width: 59.4pt;" valign="top" width="119"><div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: inherit; font-size: xx-small;">Memory Model</span></div>
</td>
<td style="border-bottom: solid windowtext 1.0pt; border-left: none; border-right: solid windowtext 1.0pt; border-top: none; mso-border-alt: solid windowtext .5pt; mso-border-left-alt: solid windowtext .5pt; mso-border-top-alt: solid windowtext .5pt; padding: 0in 5.4pt 0in 5.4pt; width: 139.5pt;" valign="top" width="279"><div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: inherit; font-size: xx-small;">Shared memory and Distributed memory</span></div>
</td>
<td style="border-bottom: solid windowtext 1.0pt; border-left: none; border-right: solid windowtext 1.0pt; border-top: none; mso-border-alt: solid windowtext .5pt; mso-border-left-alt: solid windowtext .5pt; mso-border-top-alt: solid windowtext .5pt; padding: 0in 5.4pt 0in 5.4pt; width: 117.0pt;" valign="top" width="234"><div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: inherit; font-size: xx-small;">Shared memory multiprocessing</span></div>
</td>
<td style="border-bottom: solid windowtext 1.0pt; border-left: none; border-right: solid windowtext 1.0pt; border-top: none; mso-border-alt: solid windowtext .5pt; mso-border-left-alt: solid windowtext .5pt; mso-border-top-alt: solid windowtext .5pt; padding: 0in 5.4pt 0in 5.4pt; width: 162.9pt;" valign="top" width="326"><div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: inherit; font-size: xx-small;">Shared memory</span></div>
</td>
</tr>
<tr>
<td style="border-top: none; border: solid windowtext 1.0pt; mso-border-alt: solid windowtext .5pt; mso-border-top-alt: solid windowtext .5pt; padding: 0in 5.4pt 0in 5.4pt; width: 59.4pt;" valign="top" width="119"><div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: inherit; font-size: xx-small;">Concepts</span></div>
</td>
<td style="border-bottom: solid windowtext 1.0pt; border-left: none; border-right: solid windowtext 1.0pt; border-top: none; mso-border-alt: solid windowtext .5pt; mso-border-left-alt: solid windowtext .5pt; mso-border-top-alt: solid windowtext .5pt; padding: 0in 5.4pt 0in 5.4pt; width: 139.5pt;" valign="top" width="279"><div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: inherit; font-size: xx-small;">Point-to-point messaging<br />
Broadcasting (1 to M)<br />
Scatter/Gather. Support R, C++, Java, Fortran, Python.</span></div>
</td>
<td style="border-bottom: solid windowtext 1.0pt; border-left: none; border-right: solid windowtext 1.0pt; border-top: none; mso-border-alt: solid windowtext .5pt; mso-border-left-alt: solid windowtext .5pt; mso-border-top-alt: solid windowtext .5pt; padding: 0in 5.4pt 0in 5.4pt; width: 117.0pt;" valign="top" width="234"><div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: inherit; font-size: xx-small;">An add-on in compiler</span></div>
</td>
<td style="border-bottom: solid windowtext 1.0pt; border-left: none; border-right: solid windowtext 1.0pt; border-top: none; mso-border-alt: solid windowtext .5pt; mso-border-left-alt: solid windowtext .5pt; mso-border-top-alt: solid windowtext .5pt; padding: 0in 5.4pt 0in 5.4pt; width: 162.9pt;" valign="top" width="326"><div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: inherit; font-size: xx-small;">Designed to work with C/C++ and Fortran. More effective for parallel
computing: e.g., fast sort algorithms of large lists.</span></div>
</td>
</tr>
<tr>
<td style="border-top: none; border: solid windowtext 1.0pt; mso-border-alt: solid windowtext .5pt; mso-border-top-alt: solid windowtext .5pt; padding: 0in 5.4pt 0in 5.4pt; width: 59.4pt;" valign="top" width="119"><div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: inherit; font-size: xx-small;">Variates</span></div>
</td>
<td style="border-bottom: solid windowtext 1.0pt; border-left: none; border-right: solid windowtext 1.0pt; border-top: none; mso-border-alt: solid windowtext .5pt; mso-border-left-alt: solid windowtext .5pt; mso-border-top-alt: solid windowtext .5pt; padding: 0in 5.4pt 0in 5.4pt; width: 139.5pt;" valign="top" width="279"><div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: inherit; font-size: xx-small;">OpenMPI, MPICH2</span></div>
</td>
<td style="border-bottom: solid windowtext 1.0pt; border-left: none; border-right: solid windowtext 1.0pt; border-top: none; mso-border-alt: solid windowtext .5pt; mso-border-left-alt: solid windowtext .5pt; mso-border-top-alt: solid windowtext .5pt; padding: 0in 5.4pt 0in 5.4pt; width: 117.0pt;" valign="top" width="234"><div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<br /></div>
</td>
<td style="border-bottom: solid windowtext 1.0pt; border-left: none; border-right: solid windowtext 1.0pt; border-top: none; mso-border-alt: solid windowtext .5pt; mso-border-left-alt: solid windowtext .5pt; mso-border-top-alt: solid windowtext .5pt; padding: 0in 5.4pt 0in 5.4pt; width: 162.9pt;" valign="top" width="326"><div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: inherit; font-size: xx-small;">SIMD, SIMT, SMT</span></div>
</td>
</tr>
<tr>
<td style="border-top: none; border: solid windowtext 1.0pt; mso-border-alt: solid windowtext .5pt; mso-border-top-alt: solid windowtext .5pt; padding: 0in 5.4pt 0in 5.4pt; width: 59.4pt;" valign="top" width="119"><div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: inherit; font-size: xx-small;">Pros</span></div>
</td>
<td style="border-bottom: solid windowtext 1.0pt; border-left: none; border-right: solid windowtext 1.0pt; border-top: none; mso-border-alt: solid windowtext .5pt; mso-border-left-alt: solid windowtext .5pt; mso-border-top-alt: solid windowtext .5pt; padding: 0in 5.4pt 0in 5.4pt; width: 139.5pt;" valign="top" width="279"><div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: inherit; font-size: xx-small;">More general solution;<br />
Can run in clustering environments;<br />
Distributed memory is less expensive</span></div>
</td>
<td style="border-bottom: solid windowtext 1.0pt; border-left: none; border-right: solid windowtext 1.0pt; border-top: none; mso-border-alt: solid windowtext .5pt; mso-border-left-alt: solid windowtext .5pt; mso-border-top-alt: solid windowtext .5pt; padding: 0in 5.4pt 0in 5.4pt; width: 117.0pt;" valign="top" width="234"><div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: inherit; font-size: xx-small;">Easier to program;<br />
Can still run the program as a serial code(no code change);<br />
<!--[if !supportLineBreakNewLine]--><br />
<!--[endif]--></span></div>
</td>
<td style="border-bottom: solid windowtext 1.0pt; border-left: none; border-right: solid windowtext 1.0pt; border-top: none; mso-border-alt: solid windowtext .5pt; mso-border-left-alt: solid windowtext .5pt; mso-border-top-alt: solid windowtext .5pt; padding: 0in 5.4pt 0in 5.4pt; width: 162.9pt;" valign="top" width="326"><div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: inherit; font-size: xx-small;">Scattered reads;<br />
Unified virtual memory;<br />
Fast shared memory;<br />
Full support for integer and bitwise operations.</span></div>
</td>
</tr>
<tr>
<td style="border-top: none; border: solid windowtext 1.0pt; mso-border-alt: solid windowtext .5pt; mso-border-top-alt: solid windowtext .5pt; padding: 0in 5.4pt 0in 5.4pt; width: 59.4pt;" valign="top" width="119"><div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: inherit; font-size: xx-small;">Cons</span></div>
</td>
<td style="border-bottom: solid windowtext 1.0pt; border-left: none; border-right: solid windowtext 1.0pt; border-top: none; mso-border-alt: solid windowtext .5pt; mso-border-left-alt: solid windowtext .5pt; mso-border-top-alt: solid windowtext .5pt; padding: 0in 5.4pt 0in 5.4pt; width: 139.5pt;" valign="top" width="279"><div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: inherit; font-size: xx-small;">Code change from serial to parallel version;<br />
Harder to debug;<br />
Bottleneck of network communication.</span></div>
</td>
<td style="border-bottom: solid windowtext 1.0pt; border-left: none; border-right: solid windowtext 1.0pt; border-top: none; mso-border-alt: solid windowtext .5pt; mso-border-left-alt: solid windowtext .5pt; mso-border-top-alt: solid windowtext .5pt; padding: 0in 5.4pt 0in 5.4pt; width: 117.0pt;" valign="top" width="234"><div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: inherit; font-size: xx-small;">Difficult to debug synchronization bugs and race conditions;</span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: inherit; font-size: xx-small;">Requires compiler support;<br />
Only in shared memory architecture;</span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: inherit; font-size: xx-small;">Mostly used in loop parallelization.</span></div>
</td>
<td style="border-bottom: solid windowtext 1.0pt; border-left: none; border-right: solid windowtext 1.0pt; border-top: none; mso-border-alt: solid windowtext .5pt; mso-border-left-alt: solid windowtext .5pt; mso-border-top-alt: solid windowtext .5pt; padding: 0in 5.4pt 0in 5.4pt; width: 162.9pt;" valign="top" width="326"><div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: inherit; font-size: xx-small;">Memory copy between host and device may incur performance hit due to
bandwidth and latency;<br />
Thread group with 32+ threads for best performance;<br />
Valid C/C++ may not compile;<br />
C++ RTTI is not support(?).</span></div>
</td>
</tr>
</tbody></table>
<br />
<div>
The information sources in the above table include, but not limited to, open-mpi.org, openmp.org, nvidia.com and Wikipedia.org.</div>
<div>
<br /></div>
Quantitative Coderhttp://www.blogger.com/profile/17386008753838448085noreply@blogger.com0tag:blogger.com,1999:blog-4600527051019200112.post-34249573630105025682015-12-26T23:52:00.003-08:002015-12-30T12:16:43.406-08:00Eclipse CDT, a C++ IDEs on WindowsIn addition to VC++, a few open sourced C++ IDEs are also widely used on Windows: Code::Block and Eclipse CDT.<br />
<div>
<br /></div>
<div>
Eclipse CDT is available in Eclipse IDE for C++ Developers or as a plugin in Eclipse. Here is how to set up Eclipse CDT for C++11.</div>
<div>
<ul>
<li>Install New Software... in Eclipse IDE for Java Developers <br /> (web url: <a href="http://download.eclipse.org/tools/cdt/releases/8.8" style="background-color: white; border: 0px; color: #7036be; font-family: Arial, sans-serif; font-size: 12px; font-weight: bold; line-height: 16px; margin: 0px; outline: 0px; padding: 0px; text-decoration: none;">http://download.eclipse.org/tools/cdt/releases/8.8</a>)</li>
<li>Install a C++ Compiler (e.g., Cygwin including g++ and make)</li>
<li>Create New C++ Project</li>
<li>In C/C++ Build > Settings > Other Flags Box, append "-std=c++11"</li>
</ul>
</div>
<div>
The following is the screenshot of my Eclipse </div>
<div class="separator" style="clear: both; text-align: center;">
</div>
<div class="separator" style="clear: both; text-align: center;">
</div>
<br />
<div class="separator" style="clear: both; text-align: center;">
<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEh84eK88bdcuo6Op2JdpimdP0LJR3uJ4jk02VNC0GvcGChDQuGbc48McxCPcauT7lqVPdnp75kQJpM1oy3h61y1hv-Bt-LO2uCndKPc1X5OoWLmEVUjtMHMGMnVERP8Q8QZg253wJDVL-M1/s1600/CDT.png" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" height="320" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEh84eK88bdcuo6Op2JdpimdP0LJR3uJ4jk02VNC0GvcGChDQuGbc48McxCPcauT7lqVPdnp75kQJpM1oy3h61y1hv-Bt-LO2uCndKPc1X5OoWLmEVUjtMHMGMnVERP8Q8QZg253wJDVL-M1/s400/CDT.png" width="400" /></a></div>
<div>
<br />
<span style="font-family: inherit; font-size: xx-small;">Note: RAND_MAX is a macro defined in <cstdlib>. RAND_MAX = 32,767 in VC++, while RAND_MAX = 2,147,483,647 in GNU g++.</span><br />
<br /></div>
Quantitative Coderhttp://www.blogger.com/profile/17386008753838448085noreply@blogger.com0tag:blogger.com,1999:blog-4600527051019200112.post-11455414120041131252015-12-26T12:48:00.001-08:002016-01-04T17:27:32.682-08:00C++ 11 New Feature List (for Quants)This is a list of <a href="http://www.cplusplus.com/reference/" rel="nofollow" target="_blank">C++ 11</a> (https://isocpp.org/wiki/faq/cpp11) new features (relative to C++98):<br />
<ul>
<li>RValue Reference and move ctor.</li>
<li>Lambda Expression (for functional programming)</li>
<li>Concurrency API (future, async)</li>
<li>New Smart Pointers (unique_ptr, shared_ptr in <memory>)</li>
<li>Fixed-length <array></li>
<li><random> supports Mersenne Twister(MT19937) PRNG and distributions.</li>
<li>Enhanced Containers: <forward_list> and hashed <unordered_map></li>
<li>Compile-time static_assert with <type_traits> for template code</li>
<li>noexcept, unexpected() and the enhanced <exception></li>
<li><regex> with EMCAScript syntax and backreference</li>
<li>RTTI: auto, decltype, and typeid </li>
<li>Misc (ranged for loop, nullptr, non-type template parameters)</li>
</ul>
<div>
<br />
C++ Standard Library(since C++98), which is based on STL (designed and developed by Alexander Stepanov and Meng Lee), contains key components called containers, algorithms, functional and iterators.<br />
<br />
Here the discussion is focused on Iterator, since it is very relevant to the performance. As shown in the following graph, C++/STL has 5-category iterators. Another way to present the iterator hierarchical relationship is using UML class diagram, with Input/Output as the bases at the top and Random Access Iterator at the bottom. A rule of thumb for choosing the iterators is to pick the weakest iterator first to accommodate the most efficient algorithm.<br />
<br />
<div class="separator" style="clear: both; text-align: center;">
<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgJS2CSUHLfIPjCVSFucHDzLOkBT5uV4jExwhD6zN5AcW5wgm-iWBSWlZ2C9T5ET7kAHoUwt5Bf9kvB0Yyj0lHccd6dDDbjHet8-qQ9wAaG39Fn9MXPk_JcogJUZocnOScAahss8lTqTSLC/s1600/iterator_category.png" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" height="295" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgJS2CSUHLfIPjCVSFucHDzLOkBT5uV4jExwhD6zN5AcW5wgm-iWBSWlZ2C9T5ET7kAHoUwt5Bf9kvB0Yyj0lHccd6dDDbjHet8-qQ9wAaG39Fn9MXPk_JcogJUZocnOScAahss8lTqTSLC/s400/iterator_category.png" width="400" /></a></div>
<div class="separator" style="clear: both; text-align: left;">
For example,</div>
<div class="separator" style="clear: both; text-align: left;">
Forward Iterator: std::replace()</div>
Bidirectional Iterator: std::reverse() <br />
Random Access Iterator: std::sort()<br />
<br />
Just for fun, I collected some C++ and OOP buzzwords and put them in a table.<br />
<br />
<table border="1" cellpadding="0" cellspacing="0" class="MsoTableGrid" style="border-collapse: collapse; border: none; margin-left: .7in; mso-border-alt: solid windowtext .5pt; mso-padding-alt: 0in 5.4pt 0in 5.4pt; mso-yfti-tbllook: 1184;">
<tbody>
<tr>
<td style="border: solid windowtext 1.0pt; mso-border-alt: solid windowtext .5pt; padding: 0in 5.4pt 0in 5.4pt; width: 103.5pt;" valign="top" width="207"><div class="MsoNormal" style="margin-bottom: 0.0001pt;">
RAII</div>
</td>
<td style="border-left: none; border: solid windowtext 1.0pt; mso-border-alt: solid windowtext .5pt; mso-border-left-alt: solid windowtext .5pt; padding: 0in 5.4pt 0in 5.4pt; width: 3.75in;" valign="top" width="540"><div class="MsoNormal" style="margin-bottom: 0.0001pt;">
Resource Acquisition Is Initialization</div>
</td>
</tr>
<tr>
<td style="border-top: none; border: solid windowtext 1.0pt; mso-border-alt: solid windowtext .5pt; mso-border-top-alt: solid windowtext .5pt; padding: 0in 5.4pt 0in 5.4pt; width: 103.5pt;" valign="top" width="207"><div class="MsoNormal" style="margin-bottom: 0.0001pt;">
CADR</div>
</td>
<td style="border-bottom: solid windowtext 1.0pt; border-left: none; border-right: solid windowtext 1.0pt; border-top: none; mso-border-alt: solid windowtext .5pt; mso-border-left-alt: solid windowtext .5pt; mso-border-top-alt: solid windowtext .5pt; padding: 0in 5.4pt 0in 5.4pt; width: 3.75in;" valign="top" width="540"><div class="MsoNormal" style="margin-bottom: 0.0001pt;">
CTOR Acquire, DTOR Release, scoped-based res mgmt</div>
</td>
</tr>
<tr>
<td style="border-top: none; border: solid windowtext 1.0pt; mso-border-alt: solid windowtext .5pt; mso-border-top-alt: solid windowtext .5pt; padding: 0in 5.4pt 0in 5.4pt; width: 103.5pt;" valign="top" width="207"><div class="MsoNormal" style="margin-bottom: 0.0001pt;">
RTTI</div>
</td>
<td style="border-bottom: solid windowtext 1.0pt; border-left: none; border-right: solid windowtext 1.0pt; border-top: none; mso-border-alt: solid windowtext .5pt; mso-border-left-alt: solid windowtext .5pt; mso-border-top-alt: solid windowtext .5pt; padding: 0in 5.4pt 0in 5.4pt; width: 3.75in;" valign="top" width="540"><div class="MsoNormal" style="margin-bottom: 0.0001pt;">
RunTime Type Info</div>
</td>
</tr>
<tr>
<td style="border-top: none; border: solid windowtext 1.0pt; mso-border-alt: solid windowtext .5pt; mso-border-top-alt: solid windowtext .5pt; padding: 0in 5.4pt 0in 5.4pt; width: 103.5pt;" valign="top" width="207"><div class="MsoNormal" style="margin-bottom: 0.0001pt;">
BSS</div>
</td><td style="border-bottom: solid windowtext 1.0pt; border-left: none; border-right: solid windowtext 1.0pt; border-top: none; mso-border-alt: solid windowtext .5pt; mso-border-left-alt: solid windowtext .5pt; mso-border-top-alt: solid windowtext .5pt; padding: 0in 5.4pt 0in 5.4pt; width: 3.75in;" valign="top" width="540">Block Started by Symbol. Memory Layout: Stack, [Free Memory], Heap, Initialized Data, BSS, Text.</td>
</tr>
<tr>
<td style="border-top: none; border: solid windowtext 1.0pt; mso-border-alt: solid windowtext .5pt; mso-border-top-alt: solid windowtext .5pt; padding: 0in 5.4pt 0in 5.4pt; width: 103.5pt;" valign="top" width="207"><div class="MsoNormal" style="margin-bottom: 0.0001pt;">
vtable</div>
</td>
<td style="border-bottom: solid windowtext 1.0pt; border-left: none; border-right: solid windowtext 1.0pt; border-top: none; mso-border-alt: solid windowtext .5pt; mso-border-left-alt: solid windowtext .5pt; mso-border-top-alt: solid windowtext .5pt; padding: 0in 5.4pt 0in 5.4pt; width: 3.75in;" valign="top" width="540"><div class="MsoNormal" style="margin-bottom: 0.0001pt;">
Virtual function Table</div>
</td>
</tr>
<tr>
<td style="border-top: none; border: solid windowtext 1.0pt; mso-border-alt: solid windowtext .5pt; mso-border-top-alt: solid windowtext .5pt; padding: 0in 5.4pt 0in 5.4pt; width: 103.5pt;" valign="top" width="207"><div class="MsoNormal" style="margin-bottom: 0.0001pt;">
STL</div>
</td>
<td style="border-bottom: solid windowtext 1.0pt; border-left: none; border-right: solid windowtext 1.0pt; border-top: none; mso-border-alt: solid windowtext .5pt; mso-border-left-alt: solid windowtext .5pt; mso-border-top-alt: solid windowtext .5pt; padding: 0in 5.4pt 0in 5.4pt; width: 3.75in;" valign="top" width="540"><div class="MsoNormal" style="margin-bottom: 0.0001pt;">
3-legged STooL: Algorithm, Iterator, Containers</div>
</td>
</tr>
<tr>
<td style="border-top: none; border: solid windowtext 1.0pt; mso-border-alt: solid windowtext .5pt; mso-border-top-alt: solid windowtext .5pt; padding: 0in 5.4pt 0in 5.4pt; width: 103.5pt;" valign="top" width="207"><div class="MsoNormal" style="margin-bottom: 0.0001pt;">
Algorithms</div>
</td>
<td style="border-bottom: solid windowtext 1.0pt; border-left: none; border-right: solid windowtext 1.0pt; border-top: none; mso-border-alt: solid windowtext .5pt; mso-border-left-alt: solid windowtext .5pt; mso-border-top-alt: solid windowtext .5pt; padding: 0in 5.4pt 0in 5.4pt; width: 3.75in;" valign="top" width="540"><div class="MsoNormal" style="margin-bottom: 0.0001pt;">
Algorithms use iterators and containers</div>
</td>
</tr>
<tr>
<td style="border-top: none; border: solid windowtext 1.0pt; mso-border-alt: solid windowtext .5pt; mso-border-top-alt: solid windowtext .5pt; padding: 0in 5.4pt 0in 5.4pt; width: 103.5pt;" valign="top" width="207"><div class="MsoNormal" style="margin-bottom: 0.0001pt;">
Iterator Types</div>
</td>
<td style="border-bottom: solid windowtext 1.0pt; border-left: none; border-right: solid windowtext 1.0pt; border-top: none; mso-border-alt: solid windowtext .5pt; mso-border-left-alt: solid windowtext .5pt; mso-border-top-alt: solid windowtext .5pt; padding: 0in 5.4pt 0in 5.4pt; width: 3.75in;" valign="top" width="540"><div class="MsoNormal" style="margin-bottom: 0.0001pt;">
Input/Output, Forward, Bidirectional, Random Access</div>
</td>
</tr>
<tr>
<td style="border-top: none; border: solid windowtext 1.0pt; mso-border-alt: solid windowtext .5pt; mso-border-top-alt: solid windowtext .5pt; padding: 0in 5.4pt 0in 5.4pt; width: 103.5pt;" valign="top" width="207"><div class="MsoNormal" style="margin-bottom: 0.0001pt;">
Container Types</div>
</td>
<td style="border-bottom: solid windowtext 1.0pt; border-left: none; border-right: solid windowtext 1.0pt; border-top: none; mso-border-alt: solid windowtext .5pt; mso-border-left-alt: solid windowtext .5pt; mso-border-top-alt: solid windowtext .5pt; padding: 0in 5.4pt 0in 5.4pt; width: 3.75in;" valign="top" width="540"><div class="MsoNormal" style="margin-bottom: 0.0001pt;">
Sequential, Associative. <forward_list>, <unordered_map></div>
</td>
</tr>
<tr>
<td style="border-top: none; border: solid windowtext 1.0pt; mso-border-alt: solid windowtext .5pt; mso-border-top-alt: solid windowtext .5pt; padding: 0in 5.4pt 0in 5.4pt; width: 103.5pt;" valign="top" width="207"><div class="MsoNormal" style="margin-bottom: 0.0001pt;">
Smart Pointer</div>
</td>
<td style="border-bottom: solid windowtext 1.0pt; border-left: none; border-right: solid windowtext 1.0pt; border-top: none; mso-border-alt: solid windowtext .5pt; mso-border-left-alt: solid windowtext .5pt; mso-border-top-alt: solid windowtext .5pt; padding: 0in 5.4pt 0in 5.4pt; width: 3.75in;" valign="top" width="540"><div class="MsoNormal" style="margin-bottom: 0.0001pt;">
Various raw pointer wrappers in <memory></div>
</td>
</tr>
<tr>
<td style="border-top: none; border: solid windowtext 1.0pt; mso-border-alt: solid windowtext .5pt; mso-border-top-alt: solid windowtext .5pt; padding: 0in 5.4pt 0in 5.4pt; width: 103.5pt;" valign="top" width="207"><div class="MsoNormal" style="margin-bottom: 0.0001pt;">
Pragma once</div>
</td>
<td style="border-bottom: solid windowtext 1.0pt; border-left: none; border-right: solid windowtext 1.0pt; border-top: none; mso-border-alt: solid windowtext .5pt; mso-border-left-alt: solid windowtext .5pt; mso-border-top-alt: solid windowtext .5pt; padding: 0in 5.4pt 0in 5.4pt; width: 3.75in;" valign="top" width="540"><div class="MsoNormal" style="margin-bottom: 0.0001pt;">
Directives similar to Include Guard </div>
</td>
</tr>
<tr>
<td style="border-top: none; border: solid windowtext 1.0pt; mso-border-alt: solid windowtext .5pt; mso-border-top-alt: solid windowtext .5pt; padding: 0in 5.4pt 0in 5.4pt; width: 103.5pt;" valign="top" width="207"><div class="MsoNormal" style="margin-bottom: 0.0001pt;">
ADL</div>
</td>
<td style="border-bottom: solid windowtext 1.0pt; border-left: none; border-right: solid windowtext 1.0pt; border-top: none; mso-border-alt: solid windowtext .5pt; mso-border-left-alt: solid windowtext .5pt; mso-border-top-alt: solid windowtext .5pt; padding: 0in 5.4pt 0in 5.4pt; width: 3.75in;" valign="top" width="540"><div class="MsoNormal" style="margin-bottom: 0.0001pt;">
Argument-dependent lookup, Koenig Lookup</div>
</td>
</tr>
<tr>
<td style="border-top: none; border: solid windowtext 1.0pt; mso-border-alt: solid windowtext .5pt; mso-border-top-alt: solid windowtext .5pt; padding: 0in 5.4pt 0in 5.4pt; width: 103.5pt;" valign="top" width="207"><div class="MsoNormal" style="margin-bottom: 0.0001pt;">
Move Semantics</div>
</td>
<td style="border-bottom: solid windowtext 1.0pt; border-left: none; border-right: solid windowtext 1.0pt; border-top: none; mso-border-alt: solid windowtext .5pt; mso-border-left-alt: solid windowtext .5pt; mso-border-top-alt: solid windowtext .5pt; padding: 0in 5.4pt 0in 5.4pt; width: 3.75in;" valign="top" width="540"><div class="MsoNormal" style="margin-bottom: 0.0001pt;">
Most pervasive C++11 feature. Move CTOR, Move Assignment Operator for perf. Perfect Forwarding?</div>
</td>
</tr>
<tr>
<td style="border-top: none; border: solid windowtext 1.0pt; mso-border-alt: solid windowtext .5pt; mso-border-top-alt: solid windowtext .5pt; padding: 0in 5.4pt 0in 5.4pt; width: 103.5pt;" valign="top" width="207"><div class="MsoNormal" style="margin-bottom: 0.0001pt;">
Move CTOR</div>
</td>
<td style="border-bottom: solid windowtext 1.0pt; border-left: none; border-right: solid windowtext 1.0pt; border-top: none; mso-border-alt: solid windowtext .5pt; mso-border-left-alt: solid windowtext .5pt; mso-border-top-alt: solid windowtext .5pt; padding: 0in 5.4pt 0in 5.4pt; width: 3.75in;" valign="top" width="540"><div class="MsoNormal" style="margin-bottom: 0.0001pt;">
Move Constructor</div>
</td>
</tr>
<tr>
<td style="border-top: none; border: solid windowtext 1.0pt; mso-border-alt: solid windowtext .5pt; mso-border-top-alt: solid windowtext .5pt; padding: 0in 5.4pt 0in 5.4pt; width: 103.5pt;" valign="top" width="207"><div class="MsoNormal" style="margin-bottom: 0.0001pt;">
Rvalue Reference</div>
</td>
<td style="border-bottom: solid windowtext 1.0pt; border-left: none; border-right: solid windowtext 1.0pt; border-top: none; mso-border-alt: solid windowtext .5pt; mso-border-left-alt: solid windowtext .5pt; mso-border-top-alt: solid windowtext .5pt; padding: 0in 5.4pt 0in 5.4pt; width: 3.75in;" valign="top" width="540"><div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<utility></div>
</td>
</tr>
<tr>
<td style="border-top: none; border: solid windowtext 1.0pt; mso-border-alt: solid windowtext .5pt; mso-border-top-alt: solid windowtext .5pt; padding: 0in 5.4pt 0in 5.4pt; width: 103.5pt;" valign="top" width="207"><div class="MsoNormal" style="margin-bottom: 0.0001pt;">
Boost</div>
</td>
<td style="border-bottom: solid windowtext 1.0pt; border-left: none; border-right: solid windowtext 1.0pt; border-top: none; mso-border-alt: solid windowtext .5pt; mso-border-left-alt: solid windowtext .5pt; mso-border-top-alt: solid windowtext .5pt; padding: 0in 5.4pt 0in 5.4pt; width: 3.75in;" valign="top" width="540"><div class="MsoNormal" style="margin-bottom: 0.0001pt;">
Open Source Library, some added in C++11/14.</div>
</td>
</tr>
<tr>
<td style="border-top: none; border: solid windowtext 1.0pt; mso-border-alt: solid windowtext .5pt; mso-border-top-alt: solid windowtext .5pt; padding: 0in 5.4pt 0in 5.4pt; width: 103.5pt;" valign="top" width="207"><div class="MsoNormal" style="margin-bottom: 0.0001pt;">
Empty Aggregate Initialization</div>
</td>
<td style="border-bottom: solid windowtext 1.0pt; border-left: none; border-right: solid windowtext 1.0pt; border-top: none; mso-border-alt: solid windowtext .5pt; mso-border-left-alt: solid windowtext .5pt; mso-border-top-alt: solid windowtext .5pt; padding: 0in 5.4pt 0in 5.4pt; width: 3.75in;" valign="top" width="540"><div class="MsoNormal" style="margin-bottom: 0.0001pt;">
Could be very tricky.</div>
</td>
</tr>
<tr>
<td style="border-top: none; border: solid windowtext 1.0pt; mso-border-alt: solid windowtext .5pt; mso-border-top-alt: solid windowtext .5pt; padding: 0in 5.4pt 0in 5.4pt; width: 103.5pt;" valign="top" width="207"><div class="MsoNormal" style="margin-bottom: 0.0001pt;">
Non-type Template Parameters</div>
</td>
<td style="border-bottom: solid windowtext 1.0pt; border-left: none; border-right: solid windowtext 1.0pt; border-top: none; mso-border-alt: solid windowtext .5pt; mso-border-left-alt: solid windowtext .5pt; mso-border-top-alt: solid windowtext .5pt; padding: 0in 5.4pt 0in 5.4pt; width: 3.75in;" valign="top" width="540"><div class="MsoNormal" style="margin-bottom: 0.0001pt;">
Enhanced template.</div>
</td>
</tr>
<tr>
<td style="border-top: none; border: solid windowtext 1.0pt; mso-border-alt: solid windowtext .5pt; mso-border-top-alt: solid windowtext .5pt; padding: 0in 5.4pt 0in 5.4pt; width: 103.5pt;" valign="top" width="207"><div class="MsoNormal" style="margin-bottom: 0.0001pt;">
Exception-Safety</div>
</td>
<td style="border-bottom: solid windowtext 1.0pt; border-left: none; border-right: solid windowtext 1.0pt; border-top: none; mso-border-alt: solid windowtext .5pt; mso-border-left-alt: solid windowtext .5pt; mso-border-top-alt: solid windowtext .5pt; padding: 0in 5.4pt 0in 5.4pt; width: 3.75in;" valign="top" width="540"><div class="MsoNormal" style="margin-bottom: 0.0001pt;">
Basic and Strong exception safety. noexcept, throw, try-catch, unexpected()
in <exception>. logic_error, runtime_error in <stdexcept></div>
</td>
</tr>
<tr>
<td style="border-top: none; border: solid windowtext 1.0pt; mso-border-alt: solid windowtext .5pt; mso-border-top-alt: solid windowtext .5pt; padding: 0in 5.4pt 0in 5.4pt; width: 103.5pt;" valign="top" width="207"><div class="MsoNormal" style="margin-bottom: 0.0001pt;">
Thread-Safety</div>
</td>
<td style="border-bottom: solid windowtext 1.0pt; border-left: none; border-right: solid windowtext 1.0pt; border-top: none; mso-border-alt: solid windowtext .5pt; mso-border-left-alt: solid windowtext .5pt; mso-border-top-alt: solid windowtext .5pt; padding: 0in 5.4pt 0in 5.4pt; width: 3.75in;" valign="top" width="540"><div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<thread>, <future>. async(), future.get(), timeout.<br />
volatile for hardware access, std::atomic for MT.</div>
</td>
</tr>
<tr>
<td style="border-top: none; border: solid windowtext 1.0pt; mso-border-alt: solid windowtext .5pt; mso-border-top-alt: solid windowtext .5pt; padding: 0in 5.4pt 0in 5.4pt; width: 103.5pt;" valign="top" width="207"><div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<random></div>
</td>
<td style="border-bottom: solid windowtext 1.0pt; border-left: none; border-right: solid windowtext 1.0pt; border-top: none; mso-border-alt: solid windowtext .5pt; mso-border-left-alt: solid windowtext .5pt; mso-border-top-alt: solid windowtext .5pt; padding: 0in 5.4pt 0in 5.4pt; width: 3.75in;" valign="top" width="540"><div class="MsoNormal" style="margin-bottom: 0.0001pt;">
RAND_MAX? Platform-indep? Various Probability Distributions.</div>
</td>
</tr>
<tr>
<td style="border-top: none; border: solid windowtext 1.0pt; mso-border-alt: solid windowtext .5pt; mso-border-top-alt: solid windowtext .5pt; padding: 0in 5.4pt 0in 5.4pt; width: 103.5pt;" valign="top" width="207"><div class="MsoNormal" style="margin-bottom: 0.0001pt;">
assert vs static_assert</div>
</td>
<td style="border-bottom: solid windowtext 1.0pt; border-left: none; border-right: solid windowtext 1.0pt; border-top: none; mso-border-alt: solid windowtext .5pt; mso-border-left-alt: solid windowtext .5pt; mso-border-top-alt: solid windowtext .5pt; padding: 0in 5.4pt 0in 5.4pt; width: 3.75in;" valign="top" width="540"><div class="MsoNormal" style="margin-bottom: 0.0001pt;">
#define NDEBUG vs compile-time assert. <type_traits></div>
</td>
</tr>
<tr>
<td style="border-top: none; border: solid windowtext 1.0pt; mso-border-alt: solid windowtext .5pt; mso-border-top-alt: solid windowtext .5pt; padding: 0in 5.4pt 0in 5.4pt; width: 103.5pt;" valign="top" width="207"><div class="MsoNormal" style="margin-bottom: 0.0001pt;">
OOP Key Concepts</div>
</td>
<td style="border-bottom: solid windowtext 1.0pt; border-left: none; border-right: solid windowtext 1.0pt; border-top: none; mso-border-alt: solid windowtext .5pt; mso-border-left-alt: solid windowtext .5pt; mso-border-top-alt: solid windowtext .5pt; padding: 0in 5.4pt 0in 5.4pt; width: 3.75in;" valign="top" width="540"><div class="MsoNormal" style="margin-bottom: 0.0001pt;">
Encapsulation, Inheritance, Polymorphism</div>
</td>
</tr>
<tr>
<td style="border-top: none; border: solid windowtext 1.0pt; mso-border-alt: solid windowtext .5pt; mso-border-top-alt: solid windowtext .5pt; padding: 0in 5.4pt 0in 5.4pt; width: 103.5pt;" valign="top" width="207"><div class="MsoNormal" style="margin-bottom: 0.0001pt;">
SOLID</div>
</td>
<td style="border-bottom: solid windowtext 1.0pt; border-left: none; border-right: solid windowtext 1.0pt; border-top: none; mso-border-alt: solid windowtext .5pt; mso-border-left-alt: solid windowtext .5pt; mso-border-top-alt: solid windowtext .5pt; padding: 0in 5.4pt 0in 5.4pt; width: 3.75in;" valign="top" width="540"><div class="MsoNormal" style="margin-bottom: 0.0001pt;">
Single-responsibility, Open for extension/Closed for modification, Liskov substitution,
Interface segregation, Dependency inversion.</div>
</td>
</tr>
<tr>
<td style="border-top: none; border: solid windowtext 1.0pt; mso-border-alt: solid windowtext .5pt; mso-border-top-alt: solid windowtext .5pt; padding: 0in 5.4pt 0in 5.4pt; width: 103.5pt;" valign="top" width="207"><div class="MsoNormal" style="margin-bottom: 0.0001pt;">
LLVM</div>
</td><td style="border-bottom: solid windowtext 1.0pt; border-left: none; border-right: solid windowtext 1.0pt; border-top: none; mso-border-alt: solid windowtext .5pt; mso-border-left-alt: solid windowtext .5pt; mso-border-top-alt: solid windowtext .5pt; padding: 0in 5.4pt 0in 5.4pt; width: 3.75in;" valign="top" width="540">Low Level Virtual Machine, Clang compiler</td>
</tr>
<tr>
<td style="border-top: none; border: solid windowtext 1.0pt; mso-border-alt: solid windowtext .5pt; mso-border-top-alt: solid windowtext .5pt; padding: 0in 5.4pt 0in 5.4pt; width: 103.5pt;" valign="top" width="207"><div class="MsoNormal" style="margin-bottom: 0.0001pt;">
JIT vs AOT</div>
</td>
<td style="border-bottom: solid windowtext 1.0pt; border-left: none; border-right: solid windowtext 1.0pt; border-top: none; mso-border-alt: solid windowtext .5pt; mso-border-left-alt: solid windowtext .5pt; mso-border-top-alt: solid windowtext .5pt; padding: 0in 5.4pt 0in 5.4pt; width: 3.75in;" valign="top" width="540"><div class="MsoNormal" style="margin-bottom: 0.0001pt;">
Just-in-time(CPU cycle stealing) vs Ahead-of-time Compilation</div>
</td>
</tr>
<tr>
<td style="border-top: none; border: solid windowtext 1.0pt; mso-border-alt: solid windowtext .5pt; mso-border-top-alt: solid windowtext .5pt; padding: 0in 5.4pt 0in 5.4pt; width: 103.5pt;" valign="top" width="207"><div class="MsoNormal" style="margin-bottom: 0.0001pt;">
SIMD</div>
</td>
<td style="border-bottom: solid windowtext 1.0pt; border-left: none; border-right: solid windowtext 1.0pt; border-top: none; mso-border-alt: solid windowtext .5pt; mso-border-left-alt: solid windowtext .5pt; mso-border-top-alt: solid windowtext .5pt; padding: 0in 5.4pt 0in 5.4pt; width: 3.75in;" valign="top" width="540"><div class="MsoNormal" style="margin-bottom: 0.0001pt;">
Single Instruction, Multiple Data -- Flynn's Taxonomy. Intel MMX, SSE, AVX. 64bit CPU has 16 GPRs.</div>
</td>
</tr>
<tr>
<td style="border-top: none; border: solid windowtext 1.0pt; mso-border-alt: solid windowtext .5pt; mso-border-top-alt: solid windowtext .5pt; padding: 0in 5.4pt 0in 5.4pt; width: 103.5pt;" valign="top" width="207"><div class="MsoNormal" style="margin-bottom: 0.0001pt;">
Hi-C Lo-C</div>
</td>
<td style="border-bottom: solid windowtext 1.0pt; border-left: none; border-right: solid windowtext 1.0pt; border-top: none; mso-border-alt: solid windowtext .5pt; mso-border-left-alt: solid windowtext .5pt; mso-border-top-alt: solid windowtext .5pt; padding: 0in 5.4pt 0in 5.4pt; width: 3.75in;" valign="top" width="540"><div class="MsoNormal" style="margin-bottom: 0.0001pt;">
High Cohesion, Low Coupling</div>
</td>
</tr>
<tr>
<td style="border-top: none; border: solid windowtext 1.0pt; mso-border-alt: solid windowtext .5pt; mso-border-top-alt: solid windowtext .5pt; padding: 0in 5.4pt 0in 5.4pt; width: 103.5pt;" valign="top" width="207"><div class="MsoNormal" style="margin-bottom: 0.0001pt;">
GoF</div>
</td>
<td style="border-bottom: solid windowtext 1.0pt; border-left: none; border-right: solid windowtext 1.0pt; border-top: none; mso-border-alt: solid windowtext .5pt; mso-border-left-alt: solid windowtext .5pt; mso-border-top-alt: solid windowtext .5pt; padding: 0in 5.4pt 0in 5.4pt; width: 3.75in;" valign="top" width="540"><div class="MsoNormal" style="margin-bottom: 0.0001pt;">
Gang of Four, author of “Design Patterns”</div>
</td>
</tr>
<tr>
<td style="border-top: none; border: solid windowtext 1.0pt; mso-border-alt: solid windowtext .5pt; mso-border-top-alt: solid windowtext .5pt; padding: 0in 5.4pt 0in 5.4pt; width: 103.5pt;" valign="top" width="207"><div class="MsoNormal" style="margin-bottom: 0.0001pt;">
IoC</div>
</td>
<td style="border-bottom: solid windowtext 1.0pt; border-left: none; border-right: solid windowtext 1.0pt; border-top: none; mso-border-alt: solid windowtext .5pt; mso-border-left-alt: solid windowtext .5pt; mso-border-top-alt: solid windowtext .5pt; padding: 0in 5.4pt 0in 5.4pt; width: 3.75in;" valign="top" width="540"><div class="MsoNormal" style="margin-bottom: 0.0001pt;">
Inversion of Control. Dep. Inj. Implements IoC for resolving
dependency. No need to create the object by yourself. Template Method is
another IoC example.</div>
</td>
</tr>
<tr>
<td style="border-top: none; border: solid windowtext 1.0pt; mso-border-alt: solid windowtext .5pt; mso-border-top-alt: solid windowtext .5pt; padding: 0in 5.4pt 0in 5.4pt; width: 103.5pt;" valign="top" width="207"><div class="MsoNormal" style="margin-bottom: 0.0001pt;">
Pimpl </div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
(Opaque-pointer)</div>
</td>
<td style="border-bottom: solid windowtext 1.0pt; border-left: none; border-right: solid windowtext 1.0pt; border-top: none; mso-border-alt: solid windowtext .5pt; mso-border-left-alt: solid windowtext .5pt; mso-border-top-alt: solid windowtext .5pt; padding: 0in 5.4pt 0in 5.4pt; width: 3.75in;" valign="top" width="540"><div class="MsoNormal" style="margin-bottom: 0.0001pt;">
Pointer to IMPLementation, Compiler firewall idiom, Handle class, “Cheshire
Cat”.</div>
</td>
</tr>
<tr>
<td style="border-top: none; border: solid windowtext 1.0pt; mso-border-alt: solid windowtext .5pt; mso-border-top-alt: solid windowtext .5pt; padding: 0in 5.4pt 0in 5.4pt; width: 103.5pt;" valign="top" width="207"><div class="MsoNormal" style="margin-bottom: 0.0001pt;">
Forward Declaration</div>
</td>
<td style="border-bottom: solid windowtext 1.0pt; border-left: none; border-right: solid windowtext 1.0pt; border-top: none; mso-border-alt: solid windowtext .5pt; mso-border-left-alt: solid windowtext .5pt; mso-border-top-alt: solid windowtext .5pt; padding: 0in 5.4pt 0in 5.4pt; width: 3.75in;" valign="top" width="540"><div class="MsoNormal" style="margin-bottom: 0.0001pt;">
Reduce build time</div>
</td>
</tr>
</tbody></table>
<br />
<br />
<span style="font-size: x-small;"><span style="font-family: inherit;">"... as you learned more </span><span style="font-family: inherit;">[C++11]</span><span style="font-family: inherit;">, you were surprised by the scope of the changes. </span><span style="color: blue; font-family: Courier New, Courier, monospace;">auto</span><span style="font-family: inherit;"> declarations, </span><span style="font-family: inherit;">range-based </span><span style="color: blue; font-family: Courier New, Courier, monospace;">for</span><span style="font-family: inherit;"> loops, lambda expressions, and rvalue references change the face of </span><span style="font-family: inherit;">C++, to say nothing of the new concurrency features. And then there are the </span><span style="font-family: inherit;">idiomatic changes. </span><span style="color: blue; font-family: Courier New, Courier, monospace;">NULL</span><span style="font-family: inherit;">(0) and </span><span style="color: blue; font-family: Courier New, Courier, monospace;">typedef</span><span style="font-family: inherit;">s are out, </span><span style="color: blue; font-family: Courier New, Courier, monospace;">nullptr</span><span style="font-family: inherit;"> and alias declarations are in. </span><span style="color: blue; font-family: Courier New, Courier, monospace;">enum</span><span style="font-family: inherit;">s should now be scoped. <u>Smart pointers</u> are now preferable to built-in ones. </span><span style="font-family: inherit;">Moving objects is normally better than copying them."<br /> -- </span><span style="font-family: inherit;">Scott Meyers, in </span></span><span style="font-size: x-small;">"</span><span style="font-size: x-small;"><span style="font-family: Helvetica Neue, Arial, Helvetica, sans-serif;">Effective Modern C++</span><span style="font-family: inherit;">" 2015</span></span><span style="font-size: x-small;">.</span></div>
Quantitative Coderhttp://www.blogger.com/profile/17386008753838448085noreply@blogger.com0tag:blogger.com,1999:blog-4600527051019200112.post-51874019600292193472015-12-25T22:42:00.002-08:002015-12-30T22:48:01.282-08:00Java Native Interface<span style="font-family: Arial, Helvetica, sans-serif; font-size: x-small;">JNI supports two way interaction between Java and other languages, such as C++, C, Assembly.</span><br />
<span style="font-family: Arial, Helvetica, sans-serif; font-size: x-small;"><br /></span>
<br />
<div>
<span style="font-family: Arial, Helvetica, sans-serif; font-size: x-small;">Here I describe how to use JNI for Java/C++ 2-way Calls:</span></div>
<span style="font-family: Arial, Helvetica, sans-serif; font-size: x-small;"> 1) Create a Java Project with MyExample.java with native methods.</span><br />
<span style="font-family: Arial, Helvetica, sans-serif; font-size: x-small;"> run "javah -jni MyExample" to generate MyExample.h</span><br />
<span style="font-family: Arial, Helvetica, sans-serif; font-size: x-small;"><br /></span>
<span style="font-family: Arial, Helvetica, sans-serif; font-size: x-small;"> 2) Create a C++ DLL Project with MyDLL.cpp</span><span style="font-family: Courier New, Courier, monospace; font-size: xx-small;">(#include "MyExample.h")</span><br />
<span style="font-family: Arial, Helvetica, sans-serif; font-size: x-small;"> In Project Properties, add Additional Include Directories:</span><br />
<span style="font-family: Arial, Helvetica, sans-serif; font-size: x-small;"> </span><span style="font-family: Courier New, Courier, monospace; font-size: xx-small;">C:\Java\jdk1.7.0_79\include\win32;C:\Java\jdk1.7.0_79\include</span><br />
<span style="font-family: Courier New, Courier, monospace; font-size: xx-small;"><br /></span>
<span style="font-family: Arial, Helvetica, sans-serif; font-size: x-small;"> 3) In Java project, add MyDLL.dll to Native Library Location.</span><br />
<span style="font-family: Arial, Helvetica, sans-serif; font-size: x-small;"> In Java code, add System.loadLibrary("MyDLL");</span><br />
<span style="font-family: Arial, Helvetica, sans-serif; font-size: x-small;"><br /></span>
<span style="font-family: Arial, Helvetica, sans-serif; font-size: x-small;"><br /></span>
<span style="font-family: Arial, Helvetica, sans-serif; font-size: x-small;">Note: A common run-time error when using JNI is <em style="background-color: white; border: 0px; color: #222426; line-height: 19.5px; margin: 0px; padding: 0px;">java.lang.UnsatisfiedLinkError</em><span style="background-color: white; color: #222426; line-height: 19.5px;">, which may be caused by 32-bit/64-bit DLL version mismatch or function signature mismatch. Here is a few tips to deal with this issue.</span></span><br />
<ul>
<li><span style="color: #222426; font-family: Arial, Helvetica, sans-serif; font-size: xx-small;"><span style="line-height: 19.5px;">64-bit JDKs usually work with 64-bit DLLs, while 32-bit JDKs work with 32-bit DLLs.<br />Although java provides options -d32 or -d64, it's not always working.</span></span></li>
<li><span style="color: #222426; font-family: Arial, Helvetica, sans-serif; font-size: xx-small;"><span style="line-height: 19.5px;">To check if a DLL is a 32-bit, run "vcvarsall.bat amd64" and <br />"dumpbin.exe \headers MyDLL.dll" </span></span></li>
<li><span style="color: #222426; font-family: Arial, Helvetica, sans-serif; font-size: xx-small;"><span style="line-height: 19.5px;">One code base can support both 32-bit and 64-bit platforms.</span></span></li>
</ul>
<div>
<span style="font-family: Arial, Helvetica, sans-serif; font-size: x-small;">The tools used in the above discussion: Java 1.7+, Eclipse(Mars) for Java, VC++(2013) for C++.</span><br />
<span style="font-family: Arial, Helvetica, sans-serif; font-size: x-small;"><br /></span>
<span style="font-family: Arial, Helvetica, sans-serif; font-size: x-small;">For best practice of using JNI, I recommend one article on IBM website: <a href="http://www.ibm.com/developerworks/java/library/j-jni/index.html" target="_blank">"Techniques and tools for averting the 10 most common JNI programming mistakes"</a>.</span><br />
<div>
<br /></div>
<div>
<span style="font-family: Arial, Helvetica, sans-serif; font-size: x-small;">Note: Java is newer than C++, but they are influencing each other since the beginning. Wikipedia provides an excellent comparison of the big two, <a href="https://en.wikipedia.org/wiki/Comparison_of_Java_and_C%2B%2B" rel="nofollow" target="_blank">here is the link</a>.</span><br />
<span style="font-family: Arial, Helvetica, sans-serif; font-size: x-small;"><br /></span></div>
<div>
</div>
</div>
Quantitative Coderhttp://www.blogger.com/profile/17386008753838448085noreply@blogger.com0tag:blogger.com,1999:blog-4600527051019200112.post-26045775343218692452015-12-24T21:26:00.004-08:002015-12-31T12:42:56.825-08:00QuantLib1, QuantLib can be built with VC++ 2013, as instructed at http://quantlib.org/install/vc10.shtml<br />
<br />
2, QuantLib is a well-designed C++ library and ported to R and Python.<br />
Check out the training material at http://www.implementingquantlib.com/p/training.html<br />
<br />
3, Serious about QuantLib? Get a copy of <<Implementing QuantLib>> written by Luigi Ballabio.<br />
<br />
4, Quick view of Class Hierarchy: http://yetanotherquant.de/QuantLib/book/BookQuantLib.pdf<br />
<br />
<br />
<div class="separator" style="clear: both; text-align: center;">
<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEj4vKPF5TaiQohUeGzctXKTkZ1hHgwGnSKpYmk_p6GO5n-1zm7eCP9YLwUM4HJPzlkzFVPRe-XhGGOIotipeVzG1x5rRgflGKJoVluGMOYgdcxDCk7-3ezG7t-yLJugcpx5M1XmaP8r91ud/s1600/Instrument.png" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" height="640" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEj4vKPF5TaiQohUeGzctXKTkZ1hHgwGnSKpYmk_p6GO5n-1zm7eCP9YLwUM4HJPzlkzFVPRe-XhGGOIotipeVzG1x5rRgflGKJoVluGMOYgdcxDCk7-3ezG7t-yLJugcpx5M1XmaP8r91ud/s640/Instrument.png" width="435" /></a></div>
<br />
<div class="separator" style="clear: both; text-align: center;">
<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEiCsnHNlhT1ewSvqSCOMY9Oo9uRcihoWeAqzHUoRJS-bGHOMTIES26nysjPZygha5L0_T7ixNrk5nPy0dE33_8PQesee-fkq771pA_mUtBM10vNK2H4Eqho9RgAv0wSQrU-slFPa1ayKlvS/s1600/IR-Model.png" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" height="640" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEiCsnHNlhT1ewSvqSCOMY9Oo9uRcihoWeAqzHUoRJS-bGHOMTIES26nysjPZygha5L0_T7ixNrk5nPy0dE33_8PQesee-fkq771pA_mUtBM10vNK2H4Eqho9RgAv0wSQrU-slFPa1ayKlvS/s640/IR-Model.png" width="513" /></a></div>
<div class="separator" style="clear: both; text-align: center;">
<br /></div>
<br />Quantitative Coderhttp://www.blogger.com/profile/17386008753838448085noreply@blogger.com0tag:blogger.com,1999:blog-4600527051019200112.post-14384059098113551762015-12-21T13:12:00.003-08:002015-12-21T13:15:34.151-08:00Fun with Linux1) Remove ^M<br />
$vi file.txt<br />
>:%s/^V^M//g<br />
<br />
2) 64 bit or 32 bit?<br />
$uname -a<br />
$lscpu<br />
$more /proc/cpuinfo<br />
$nproc<br />
<br />
3) Copy and preserving the same mode including timestamp<br />
$cp -p a.txt b.txt<br />
$cp -a a b<br />
<br />
4) Who logged, from where?<br />
$w<br />
$last myuserid<br />
<br />
5) Compiling C++ code?<br />
$g++ -shared ...<br />
$g++ -static-libgcc -L. -o hello hello.cpp<br />
$ar mylib.a mylib.o<br />
<br />
6) Sort in reverse order (find, grep,...)<br />
$sort -r file.txt<br />
<br />
7) To show free, total, and swap memory info in bytes<br />
$free -t<br />
<br />
8) List top processes?<br />
$top -u myuserid<br />
<br />
9) Backup or transfer files?<br />
$tar cvf a.tar /subdir<br />
$tar xvf a.tar<br />
<br />
10) List all open files?<br />
$lsof -u myuserid<br />
<br />Quantitative Coderhttp://www.blogger.com/profile/17386008753838448085noreply@blogger.com0tag:blogger.com,1999:blog-4600527051019200112.post-16994266022894195172015-12-13T19:43:00.003-08:002015-12-13T19:45:05.025-08:00Month-End Time Series vs Monthly Candle Chart<span style="font-family: "courier new" , "courier" , monospace;">library(quantmod)</span><br />
<span style="font-family: "courier new" , "courier" , monospace;">getSymbols("WMT")</span><br />
<span style="font-family: "courier new" , "courier" , monospace;">wmt = do.call(rbind, lapply(split(WMT, "months"), last))</span><br />
<br />
<span style="font-family: "courier new" , "courier" , monospace;">chartSeries(to.monthly(WMT), </span><br />
<span style="font-family: "courier new" , "courier" , monospace;"> major.ticks='months',</span><br />
<span style="font-family: "courier new" , "courier" , monospace;"> subset='last 10 years')<br />addBBands()</span><br />
<br />
<br />
<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEiGjFZ7JrSPowh8pLT7qFQ0PASFDh0XsnGu5aa41j4MZYAgzI68CnKZV8ZOYY7la9KysU1pr9S_EQYZ6SvLJS0EWXiOfxMuVPdmpQYpGDIKIYcLni2aUJgaHDQ0z_QJGlzc0CdL_W07IZN_/s1600/Rplot01.png" imageanchor="1"><img border="0" height="237" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEiGjFZ7JrSPowh8pLT7qFQ0PASFDh0XsnGu5aa41j4MZYAgzI68CnKZV8ZOYY7la9KysU1pr9S_EQYZ6SvLJS0EWXiOfxMuVPdmpQYpGDIKIYcLni2aUJgaHDQ0z_QJGlzc0CdL_W07IZN_/s400/Rplot01.png" width="400" /></a>Quantitative Coderhttp://www.blogger.com/profile/17386008753838448085noreply@blogger.com0tag:blogger.com,1999:blog-4600527051019200112.post-57214783476320964682015-12-10T21:47:00.001-08:002016-02-15T00:33:52.523-08:00Random Walk in R<span style="font-family: Courier New, Courier, monospace;">M = 150<br />N = 360<br /><br />m = matrix(0,nrow=M,ncol=N)<br />m1 = apply(m, c(1,2), function(x) rnorm(1)) #runif, rnorm<br />m2 = apply(m1, 1, cumsum)<br />m2 = t(m2)<br /><br /> hist(m2[,N], 30)<br /><br /> a = mean(m2[,N])<br />s = sd(m2[,N])^2<br /><br /> sd2 = apply(m2, 1, sd)<br />matplot(t(m2), type="l")</span><span style="color: blue; font-family: "courier new" , "courier" , monospace; font-size: xx-small;"><br /></span>
<br />
<div class="separator" style="clear: both; text-align: center;">
<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgSSp95n2dQoLvy0p_jY5chuM7D06JqiEPB413qxSt48JaV2-UbD90S2Ap4SW_QEVzxzkgESUQOLKF-Zw5unmzbOMjPn5JQh8LHhjYcJTEp8QEF98IM1cLFaIN3Mae7PT10JO8SGDOtkZhZ/s1600/rw.png" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" height="266" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgSSp95n2dQoLvy0p_jY5chuM7D06JqiEPB413qxSt48JaV2-UbD90S2Ap4SW_QEVzxzkgESUQOLKF-Zw5unmzbOMjPn5JQh8LHhjYcJTEp8QEF98IM1cLFaIN3Mae7PT10JO8SGDOtkZhZ/s400/rw.png" width="400" /></a></div>
<br />Quantitative Coderhttp://www.blogger.com/profile/17386008753838448085noreply@blogger.com0tag:blogger.com,1999:blog-4600527051019200112.post-20275891873592847892015-11-28T08:05:00.004-08:002015-11-28T11:06:13.828-08:00Sargan Test and Hausman Test<table border="1" cellpadding="0" cellspacing="0" class="MsoTableGrid" style="border-collapse: collapse; border: none; text-align: center;">
<tbody>
<tr>
<td style="border: solid windowtext 1.0pt; mso-border-alt: solid windowtext .5pt; padding: 0in 5.4pt 0in 5.4pt; width: 239.4pt;" valign="top" width="479"><div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<o:p><span style="font-family: inherit;"> Sargan Test</span></o:p></div>
</td>
<td style="border-left: none; border: solid windowtext 1.0pt; mso-border-alt: solid windowtext .5pt; mso-border-left-alt: solid windowtext .5pt; padding: 0in 5.4pt 0in 5.4pt; width: 239.4pt;" valign="top" width="479"><div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<o:p><span style="font-family: inherit;"> Hausman Test</span></o:p></div>
</td>
</tr>
<tr>
<td style="border-top: none; border: solid windowtext 1.0pt; mso-border-alt: solid windowtext .5pt; mso-border-top-alt: solid windowtext .5pt; padding: 0in 5.4pt 0in 5.4pt; width: 239.4pt;" valign="top" width="479"><div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<o:p><span style="font-family: inherit;"> H<span style="font-size: xx-small;">0</span>: Valid Instrument</span></o:p></div>
</td>
<td style="border-bottom: solid windowtext 1.0pt; border-left: none; border-right: solid windowtext 1.0pt; border-top: none; mso-border-alt: solid windowtext .5pt; mso-border-left-alt: solid windowtext .5pt; mso-border-top-alt: solid windowtext .5pt; padding: 0in 5.4pt 0in 5.4pt; width: 239.4pt;" valign="top" width="479"><div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<o:p><span style="font-family: inherit;"> H<span style="font-size: xx-small;">0</span>: X<span style="font-size: xx-small;">1</span> exogeneity </span></o:p></div>
</td>
</tr>
<tr>
<td style="border-top: none; border: solid windowtext 1.0pt; mso-border-alt: solid windowtext .5pt; mso-border-top-alt: solid windowtext .5pt; padding: 0in 5.4pt 0in 5.4pt; width: 239.4pt;" valign="top" width="479"><div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<o:p><span style="font-family: inherit;">2SLS </span></o:p></div>
</td>
<td style="border-bottom: solid windowtext 1.0pt; border-left: none; border-right: solid windowtext 1.0pt; border-top: none; mso-border-alt: solid windowtext .5pt; mso-border-left-alt: solid windowtext .5pt; mso-border-top-alt: solid windowtext .5pt; padding: 0in 5.4pt 0in 5.4pt; width: 239.4pt;" valign="top" width="479"><div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<o:p><span style="font-family: inherit;">y ~ X </span></o:p></div>
</td>
</tr>
<tr>
<td style="border-top: none; border: solid windowtext 1.0pt; mso-border-alt: solid windowtext .5pt; mso-border-top-alt: solid windowtext .5pt; padding: 0in 5.4pt 0in 5.4pt; width: 239.4pt;" valign="top" width="479"><div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<o:p><span style="font-family: inherit;"> e<span style="font-size: xx-small;">2SLS</span> = y - Xb<span style="font-size: xx-small;">2SLS</span></span></o:p></div>
</td>
<td style="border-bottom: solid windowtext 1.0pt; border-left: none; border-right: solid windowtext 1.0pt; border-top: none; mso-border-alt: solid windowtext .5pt; mso-border-left-alt: solid windowtext .5pt; mso-border-top-alt: solid windowtext .5pt; padding: 0in 5.4pt 0in 5.4pt; width: 239.4pt;" valign="top" width="479"><div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<o:p><span style="font-family: inherit;"> e = y-Xb</span></o:p></div>
</td>
</tr>
<tr>
<td style="border-top: none; border: solid windowtext 1.0pt; mso-border-alt: solid windowtext .5pt; mso-border-top-alt: solid windowtext .5pt; padding: 0in 5.4pt 0in 5.4pt; width: 239.4pt;" valign="top" width="479"><div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<o:p><span style="font-family: inherit;">e<span style="font-size: xx-small;">2SLS</span> ~ Z </span></o:p></div>
</td>
<td style="border-bottom: solid windowtext 1.0pt; border-left: none; border-right: solid windowtext 1.0pt; border-top: none; mso-border-alt: solid windowtext .5pt; mso-border-left-alt: solid windowtext .5pt; mso-border-top-alt: solid windowtext .5pt; padding: 0in 5.4pt 0in 5.4pt; width: 239.4pt;" valign="top" width="479"><div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<o:p><span style="font-family: inherit;">X<span style="font-size: xx-small;">1</span> ~ Z </span></o:p></div>
</td>
</tr>
<tr>
<td style="border-top: none; border: solid windowtext 1.0pt; mso-border-alt: solid windowtext .5pt; mso-border-top-alt: solid windowtext .5pt; padding: 0in 5.4pt 0in 5.4pt; width: 239.4pt;" valign="top" width="479"><div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<o:p><span style="font-family: inherit;"> m instruments in Z</span></o:p></div>
</td>
<td style="border-bottom: solid windowtext 1.0pt; border-left: none; border-right: solid windowtext 1.0pt; border-top: none; mso-border-alt: solid windowtext .5pt; mso-border-left-alt: solid windowtext .5pt; mso-border-top-alt: solid windowtext .5pt; padding: 0in 5.4pt 0in 5.4pt; width: 239.4pt;" valign="top" width="479"><div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<o:p><span style="font-family: inherit;"> v = X<span style="font-size: xx-small;">1 </span>- Zb<span style="font-size: xx-small;">2SLS1</span></span></o:p></div>
</td>
</tr>
<tr>
<td style="border-top: none; border: solid windowtext 1.0pt; mso-border-alt: solid windowtext .5pt; mso-border-top-alt: solid windowtext .5pt; padding: 0in 5.4pt 0in 5.4pt; width: 239.4pt;" valign="top" width="479"><div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<o:p><span style="font-family: inherit;"> k explanatory variables in X</span></o:p></div>
</td>
<td style="border-bottom: solid windowtext 1.0pt; border-left: none; border-right: solid windowtext 1.0pt; border-top: none; mso-border-alt: solid windowtext .5pt; mso-border-left-alt: solid windowtext .5pt; mso-border-top-alt: solid windowtext .5pt; padding: 0in 5.4pt 0in 5.4pt; width: 239.4pt;" valign="top" width="479"><div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<o:p><span style="font-family: inherit;">e ~ X + v </span></o:p></div>
</td>
</tr>
<tr>
<td style="border-top: none; border: solid windowtext 1.0pt; mso-border-alt: solid windowtext .5pt; mso-border-top-alt: solid windowtext .5pt; padding: 0in 5.4pt 0in 5.4pt; width: 239.4pt;" valign="top" width="479"><div class="MsoNormal">
<span style="font-family: inherit;"><span style="font-size: 13.5pt; line-height: 115%;">nR<sup>2</sup>
– χ<sup>2</sup> (m-k)</span><o:p></o:p></span></div>
</td>
<td style="border-bottom: solid windowtext 1.0pt; border-left: none; border-right: solid windowtext 1.0pt; border-top: none; mso-border-alt: solid windowtext .5pt; mso-border-left-alt: solid windowtext .5pt; mso-border-top-alt: solid windowtext .5pt; padding: 0in 5.4pt 0in 5.4pt; width: 239.4pt;" valign="top" width="479"><div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<o:p><span style="font-family: inherit;"><span style="font-family: "calibri" , sans-serif; font-size: 13.5pt; line-height: 115%;">nR</span><sup style="font-family: Calibri, sans-serif; line-height: 115%;">2</sup><span style="font-family: "calibri" , sans-serif; font-size: 13.5pt; line-height: 115%;"> – χ</span><sup style="font-family: Calibri, sans-serif; line-height: 115%;">2</sup><span style="font-family: "calibri" , sans-serif; font-size: 13.5pt; line-height: 115%;"> (k</span><sub style="font-family: Calibri, sans-serif; line-height: 115%;">1</sub><span style="font-family: "calibri" , sans-serif; font-size: 13.5pt; line-height: 115%;">)</span> </span></o:p></div>
</td>
</tr>
</tbody></table>
<span style="font-family: inherit;"><span style="font-family: inherit;"><br /></span>
<span style="font-family: inherit;">Note:</span></span><br />
<span style="font-family: inherit;"><span style="font-family: inherit;"><br /></span>
<span style="font-family: inherit;">X = (X<span style="font-size: xx-small;">1</span>, X<span style="font-size: xx-small;">2</span>)</span></span><br />
<span style="font-family: inherit;">Z = (Z*, X<span style="font-size: xx-small;">2</span>)</span><br />
<span style="font-family: inherit;"><span style="font-family: inherit;"><br /></span>
<span style="font-family: inherit;">X<span style="font-size: xx-small;">1</span>: potentially endogenous variables</span></span><br />
<span style="font-family: inherit;">X<span style="font-size: xx-small;">2</span>: exogenous variables</span><br />
<span style="font-family: inherit;">Z: Instrumental variables</span><br />
<span style="font-family: inherit;"><span style="font-family: inherit;"><br /></span>
<span style="font-family: inherit;">2SLS: Two Stage Least Square Method</span></span><br />
<span style="font-family: inherit;"><span style="font-family: inherit;"><br /></span>
<span style="font-family: inherit;">Stage 1: X<span style="font-size: xx-small;">1</span> = Z*<span style="font-family: "calibri" , sans-serif; font-size: 13.5pt; line-height: 115%;">ϒ</span><span style="font-size: xx-small;">1</span> + X<span style="font-size: xx-small;">2</span><span style="font-family: "calibri" , sans-serif; font-size: 13.5pt; line-height: 115%;">ϒ</span><span style="font-size: xx-small;">2</span> + <span style="font-family: "calibri" , sans-serif; font-size: 13.5pt; line-height: 115%;">η</span></span></span><br />
<span style="font-family: inherit;"><span style="font-family: inherit;">Stage 2: y = <span style="font-size: xx-small;">fitted_</span>X<span style="font-size: xx-small;">1</span>β + X<span style="font-size: xx-small;">2</span>β + <span style="font-family: "calibri" , sans-serif; line-height: 115%;">ε</span></span></span><br />
<div class="MsoNormal">
<span style="font-family: inherit;"><o:p></o:p></span></div>
<span style="font-family: inherit;"><br /></span>
Quantitative Coderhttp://www.blogger.com/profile/17386008753838448085noreply@blogger.com0tag:blogger.com,1999:blog-4600527051019200112.post-57098934823700689572015-10-02T20:17:00.001-07:002016-02-15T00:32:38.343-08:00Lexical Scoping in RLexical Scoping in R<br />
<br />
---------------------------------- What is the output? -----------------------<br />
<br />
y = "World!"<br />
<br />
f = function(x) {<br />
y = "Weird!"<br />
g(x)<br />
}<br />
<br />
g = function(x) {<br />
cat(x, y)<br />
}<br />
<br />
f("Hello")<br />
<div>
<br /></div>
-----------------------------------------------------------------------------------<br />
<br />
> search()<br />
[1] ".GlobalEnv" "package:RMySQL" "package:DBI" <br />
[4] "tools:rstudio" "package:stats" "package:graphics"<br />
[7] "package:grDevices" "package:utils" "package:datasets"<br />
[10] "package:methods" "Autoloads" "package:base" <br />
<br />
R, like Perl, Python and Lisp, is lexical(static) scoping. It searches for free variables in various environments with a specific order.<br />
<br />
In this case, y is the free variable of function g(x).<br />
<br />
The search order is:<br />
<br />
1, The environment where g(x) is defined: Global environment<br />
2, Parent environment (package, namespace, import)<br />
3, Repeat 2 till it reaches the top level<br />
4, Empty environment, error.<br />
<br />
Empty environment has not parent.<br />
<br />
Here is a function closure in R:<br />
<div class="line number1 index0 alt2" style="background: none white; border-radius: 0px; border: 0px; bottom: auto; box-shadow: none; box-sizing: content-box; direction: ltr; float: none; height: auto; left: auto; line-height: 20.9px; margin: 0px; outline: 0px; overflow: visible; padding: 0px 1em; position: static; right: auto; top: auto; vertical-align: baseline; white-space: pre; width: auto;">
<span style="font-family: "georgia" , "times new roman" , serif; font-size: large;"><br /></span></div>
<span style="font-family: Courier New, Courier, monospace;">lcg<-function(a,c,m) {<br /> return( function(x) {<br /> (a*x+c) %% m<br /> })<br />}<br />my_runif=lcg(214013,2531011, 2^32)<br />my_runif(2)</span><div class="line number10 index9 alt1" style="background: none white !important; border-radius: 0px !important; border: 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; color: #333333; direction: ltr !important; float: none !important; font-family: Consolas, 'Bitstream Vera Sans Mono', 'Courier New', Courier, monospace; font-size: 19px; height: auto !important; left: auto !important; line-height: 20.9px; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px 1em !important; position: static !important; right: auto !important; top: auto !important; vertical-align: baseline !important; white-space: pre !important; width: auto !important;">
<code class="r plain" style="background: none !important; border-radius: 0px !important; border: 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; color: black !important; direction: ltr !important; display: inline !important; float: none !important; font-family: Consolas, 'Bitstream Vera Sans Mono', 'Courier New', Courier, monospace !important; font-size: 1em !important; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px !important; position: static !important; right: auto !important; top: auto !important; vertical-align: baseline !important; width: auto !important;"><br /></code></div>
-------------------------------------------------------------------------------<br />
<br />
A well known question: "Does all computer languages converge to LISP?"
<br />
<br />Quantitative Coderhttp://www.blogger.com/profile/17386008753838448085noreply@blogger.com0tag:blogger.com,1999:blog-4600527051019200112.post-31486610996094508442015-04-21T07:59:00.001-07:002015-04-21T08:15:18.497-07:00Generic Programming in Java<br />
<span style="color: #3f5fbf;"><span style="color: #3f5fbf;"><span style="color: black;"></span></span></span><br />
<div align="LEFT">
<span style="color: #3f5fbf;"><span style="color: #3f5fbf;"><span style="color: black;"><span style="font-family: inherit; font-size: small;">Java first appeared in 1995, and it was designed to let the Java developers "write once, run anywhere". C++ was nearly at its peak while Java was born, so OOP and Portability have been fully supported since the very beginning. Any programming language has design principles, here I copied </span></span></span></span></div>
<span style="color: #3f5fbf; font-size: x-small;"><span style="color: #3f5fbf; font-size: x-small;"><span style="color: black;"><span style="font-size: small;">
</span><table border="0" cellpadding="0" cellspacing="0" style="border-collapse: collapse; width: 453px;">
<colgroup><col style="mso-width-alt: 16566; mso-width-source: userset; width: 340pt;" width="453"></col>
<tbody>
<tr height="20" style="height: 15pt;">
<td height="20" style="background-color: transparent; border-image: none; border: 0px blue; height: 15pt; width: 340pt;" width="453"><span style="color: blue; font-family: Calibri;"><a href="http://www.oracle.com/technetwork/java/intro-141325.html" target="_blank">the five primary goals in the creation of the Java language</a>:</span></td>
</tr>
<tr height="20" style="height: 15pt;">
<td height="20" style="background-color: transparent; border-image: none; border: 0px black; height: 15pt;"></td>
</tr>
<tr height="20" style="height: 15pt;">
<td height="20" style="background-color: transparent; border-image: none; border: 0px black; height: 15pt;"><span style="font-family: Calibri;"> 1. Simple,
Object-Oriented and Familiar</span></td>
</tr>
<tr height="20" style="height: 15pt;">
<td height="20" style="background-color: transparent; border-image: none; border: 0px black; height: 15pt;"><span style="font-family: Calibri;"> 2. Robust and
Secure</span></td>
</tr>
<tr height="20" style="height: 15pt;">
<td height="20" style="background-color: transparent; border-image: none; border: 0px black; height: 15pt;"><span style="font-family: Calibri;"> 3. Architecture-neutral and portable</span></td>
</tr>
<tr height="20" style="height: 15pt;">
<td height="20" style="background-color: transparent; border-image: none; border: 0px black; height: 15pt;"><span style="font-family: Calibri;"> 4. High performance</span></td>
</tr>
<tr height="20" style="height: 15pt;">
<td height="20" style="background-color: transparent; border-image: none; border: 0px black; height: 15pt;"><span style="font-family: Calibri;"> 5. Interpreted, Threaded, and Dynamic
</span></td>
</tr>
</tbody></colgroup></table>
<span style="font-size: small;">
</span></span></span></span><br />
<div align="LEFT">
<span style="color: #3f5fbf; font-size: x-small;"><span style="color: #3f5fbf; font-size: x-small;"><span style="color: black;"><span style="font-size: small;"></span></span></span></span><br />
<span style="color: #3f5fbf; font-size: x-small;"><span style="color: #3f5fbf; font-size: x-small;"><span style="color: black;"><span style="font-family: inherit; font-size: small;"></span></span></span></span><br /></div>
<span style="color: #3f5fbf; font-size: x-small;"><span style="color: #3f5fbf; font-size: x-small;"><span style="color: black;">
<span style="font-size: small;">
</span></span></span></span><br />
<div align="LEFT">
<span style="color: #3f5fbf; font-size: x-small;"><span style="color: #3f5fbf; font-size: x-small;"><span style="font-family: inherit;"><br /><span style="font-size: small;"></span></span></span></span></div>
<span style="color: #3f5fbf; font-size: x-small;"><span style="color: #3f5fbf; font-size: x-small;">
<span style="font-size: small;">
</span></span></span><br />
<div align="LEFT">
<span style="color: #3f5fbf; font-size: x-small;"><span style="color: #3f5fbf; font-size: x-small;"><span style="font-family: inherit;"><span style="font-size: small;"><span style="color: black;">Since then, Java has experienced lots of changes. One change is the </span><span style="color: black;">Generics or Generic Programming introduced in Java 5. Now Generics has been widely in practice, and the Functional Programming in Java 8 makes it even more indispensable. </span></span></span></span></span></div>
<span style="color: #3f5fbf; font-size: x-small;"><span style="color: #3f5fbf; font-size: x-small;">
<span style="font-size: small;">
</span></span></span><div align="LEFT">
<span style="color: #3f5fbf; font-size: x-small;"><span style="color: #3f5fbf; font-size: x-small;"><span style="color: black;"><br /><span style="font-family: inherit; font-size: small;"></span></span></span></span></div>
<span style="color: #3f5fbf; font-size: x-small;"><span style="color: #3f5fbf; font-size: x-small;">
<span style="font-size: small;">
</span><div align="LEFT">
<span style="color: black;"><span style="font-family: inherit; font-size: small;">It's uncommon to see a method in Java Stream API like this:</span></span><br />
<span style="color: black; font-size: small;"><br /></span><br />
<span style="font-family: "Courier New", Courier, monospace; font-size: small;">public static <T> </span><a href="https://docs.oracle.com/javase/8/docs/api/java/util/stream/Collector.html" title="interface in java.util.stream"><span style="font-family: "Courier New", Courier, monospace; font-size: small;">Collector</span></a><span style="font-family: "Courier New", Courier, monospace; font-size: small;"><T,?,</span><a href="https://docs.oracle.com/javase/8/docs/api/java/lang/Long.html" title="class in java.lang"><span style="font-family: "Courier New", Courier, monospace; font-size: small;">Long</span></a><span style="font-family: "Courier New", Courier, monospace; font-size: small;">> counting();</span><span style="color: black; font-size: small;"><br /></span></div>
<div align="LEFT">
<span style="color: black; font-size: small;"><br /></span><br />
<span style="color: black; font-size: small;">or,</span><br />
<span style="color: black; font-size: small;"></span><span style="font-family: "Courier New", Courier, monospace; font-size: small;"><br /></span></div>
<div align="LEFT">
<span style="font-family: "Courier New", Courier, monospace; font-size: small;">public static <T,K,D,A,M extends </span><a href="https://docs.oracle.com/javase/8/docs/api/java/util/Map.html" title="interface in java.util"><span style="font-family: "Courier New", Courier, monospace; font-size: small;">Map</span></a><span style="font-family: "Courier New", Courier, monospace; font-size: small;"><K,D>> </span><a href="https://docs.oracle.com/javase/8/docs/api/java/util/stream/Collector.html" title="interface in java.util.stream"><span style="font-family: "Courier New", Courier, monospace; font-size: small;">Collector</span></a><span style="font-family: "Courier New", Courier, monospace; font-size: small;"><T,?,M><br /> groupingBy(</span><a href="https://docs.oracle.com/javase/8/docs/api/java/util/function/Function.html" title="interface in java.util.function"><span style="font-family: "Courier New", Courier, monospace; font-size: small;">Function</span></a><span style="font-family: "Courier New", Courier, monospace; font-size: small;"><? super T,? extends K> classifier,<br /> </span><a href="https://docs.oracle.com/javase/8/docs/api/java/util/function/Supplier.html" title="interface in java.util.function"><span style="font-family: "Courier New", Courier, monospace; font-size: small;">Supplier</span></a><span style="font-size: small;"><span style="font-family: "Courier New", Courier, monospace;"><M> mapFactory,</span> </span><a href="https://docs.oracle.com/javase/8/docs/api/java/util/stream/Collector.html" title="interface in java.util.stream"><span style="font-family: "Courier New", Courier, monospace; font-size: small;">Collector</span></a><span style="font-family: "Courier New", Courier, monospace; font-size: small;"><? super T,A,D> downstream);</span></div>
<span style="font-size: small;">
</span><div align="LEFT">
<span style="font-size: small;"></span><br /></div>
<span style="font-size: small;">
</span><div align="LEFT">
<span style="font-size: small;"></span><br /></div>
<span style="color: black; font-size: small;"><br /></span><div align="LEFT">
<span style="color: black; font-size: small;">It's fun to write/play code. Here is some on Basics of Java Generics:</span><span style="font-family: "Courier New", Courier, monospace; font-size: small;"><br /></span></div>
<div align="LEFT">
<span style="font-family: "Courier New", Courier, monospace; font-size: small;">/**</span></div>
<span style="font-size: small;">
<span style="font-family: "Courier New", Courier, monospace;">
</span></span><div align="LEFT">
<span style="font-family: "Courier New", Courier, monospace; font-size: small;"> Code snippet to initialize the generic type</span></div>
<span style="font-size: small;">
<span style="font-family: "Courier New", Courier, monospace;">
</span></span><div align="LEFT">
<span style="font-family: "Courier New", Courier, monospace; font-size: small;"> There are 4 compilation errors:</span></div>
<span style="font-size: small;">
<span style="font-family: "Courier New", Courier, monospace;">
</span></span></span></span><br />
<div align="LEFT">
<span style="font-family: "Courier New", Courier, monospace;"><span style="color: #3f5fbf;"><span style="color: #3f5fbf;"> 1, Cannot make a static reference to the non</span></span><span style="color: #7f7f9f;"><span style="color: #7f7f9f;">-</span></span><span style="color: #3f5fbf;"><span style="color: #3f5fbf;">static type T</span></span></span></div>
<span style="color: #3f5fbf;"><span style="color: #3f5fbf; font-family: "Courier New", Courier, monospace;">
</span></span><br />
<div align="LEFT">
<span style="font-family: "Courier New", Courier, monospace;"><span style="color: #3f5fbf;"><span style="color: #3f5fbf;"> 2, Cannot make a static reference to the non</span></span><span style="color: #7f7f9f;"><span style="color: #7f7f9f;">-</span></span><span style="color: #3f5fbf;"><span style="color: #3f5fbf;">static type T</span></span></span></div>
<span style="color: #3f5fbf;"><span style="color: #3f5fbf;"><span style="font-family: "Courier New", Courier, monospace;">
</span></span></span><br />
<div align="LEFT">
<span style="color: #3f5fbf;"><span style="color: #3f5fbf;"><span style="font-family: "Courier New", Courier, monospace;"> 3, Cannot instantiate the type T</span></span></span></div>
<span style="color: #3f5fbf;"><span style="color: #3f5fbf;">
<span style="font-family: "Courier New", Courier, monospace;">
</span></span></span><br />
<div align="LEFT">
<span style="color: #3f5fbf;"><span style="color: #3f5fbf;"><span style="font-family: "Courier New", Courier, monospace;"> 4, Cannot create a generic array of T</span></span></span></div>
<span style="color: #3f5fbf;"><span style="color: #3f5fbf;">
<span style="font-family: "Courier New", Courier, monospace;">
</span></span></span><br />
<div align="LEFT">
<span style="color: #3f5fbf;"><span style="color: #3f5fbf;"><span style="font-family: "Courier New", Courier, monospace;">**/</span></span></span></div>
<span style="color: #3f5fbf;"><span style="color: #3f5fbf;">
<span style="font-family: "Courier New", Courier, monospace;">
</span></span></span><b><span style="color: #7f0055;"><span style="color: #7f0055;"></span></span></b><span style="color: #7f0055;"><span style="color: #7f0055;"></span></span><br />
<div align="LEFT">
<span style="font-family: "Courier New", Courier, monospace;"><span style="color: #7f0055;"><span style="color: #7f0055;"><b>public</b></span></span> <b><span style="color: #7f0055;"><span style="color: #7f0055;">class</span></span></b> Generic<T> {</span></div>
<span style="font-family: "Courier New", Courier, monospace;">
</span><br />
<div align="LEFT">
<br /></div>
<span style="font-family: "Courier New", Courier, monospace;">
</span><br />
<div align="LEFT">
<span style="font-family: "Courier New", Courier, monospace;"> <span style="color: #3f7f5f;"><span style="color: #3f7f5f;"> // returns an object of the generic type</span></span></span></div>
<span style="color: #3f7f5f;"><span style="color: #3f7f5f; font-family: "Courier New", Courier, monospace;">
</span></span><br />
<div align="LEFT">
<span style="font-family: "Courier New", Courier, monospace;"> <b><span style="color: #7f0055;"><span style="color: #7f0055;"> public</span></span></b> <b><span style="color: #7f0055;"><span style="color: #7f0055;">static</span></span></b> <u>T</u> returnGenericType_static() {</span></div>
<span style="font-family: "Courier New", Courier, monospace;">
</span><br />
<div align="LEFT">
<span style="font-family: "Courier New", Courier, monospace;"> <b><span style="color: #7f0055;"><span style="color: #7f0055;"> return</span></span></b> <b><span style="color: #7f0055;"><span style="color: #7f0055;">new</span></span></b> <u>T</u>();</span></div>
<span style="font-family: "Courier New", Courier, monospace;">
</span><br />
<div align="LEFT">
<span style="font-family: "Courier New", Courier, monospace;"> }</span><span style="font-family: "Courier New", Courier, monospace;"><br /></span></div>
<div align="LEFT">
<span style="color: #3f7f5f;"><span style="color: #3f7f5f; font-family: "Courier New", Courier, monospace;"> // returns an object of the generic type</span></span></div>
<span style="color: #3f7f5f;"><span style="color: #3f7f5f; font-family: "Courier New", Courier, monospace;">
</span></span><br />
<div align="LEFT">
<span style="font-family: "Courier New", Courier, monospace;"> <b><span style="color: #7f0055;"><span style="color: #7f0055;"> public</span></span></b> T returnGenericType() {</span></div>
<span style="font-family: "Courier New", Courier, monospace;">
</span><br />
<div align="LEFT">
<span style="font-family: "Courier New", Courier, monospace;"> <b><span style="color: #7f0055;"><span style="color: #7f0055;"> return</span></span></b> <b><span style="color: #7f0055;"><span style="color: #7f0055;">new</span></span></b> <u>T</u>();</span></div>
<span style="font-family: "Courier New", Courier, monospace;">
</span><br />
<div align="LEFT">
<span style="font-family: "Courier New", Courier, monospace;"> }</span></div>
<div align="LEFT">
<span style="color: #3f7f5f;"><span style="color: #3f7f5f; font-family: "Courier New", Courier, monospace;"> // returns an array of the generic type</span></span></div>
<span style="color: #3f7f5f;"><span style="color: #3f7f5f; font-family: "Courier New", Courier, monospace;">
</span></span><br />
<div align="LEFT">
<span style="font-family: "Courier New", Courier, monospace;"> <b><span style="color: #7f0055;"><span style="color: #7f0055;"> public</span></span></b> T[] returnGenericArray() {</span></div>
<span style="font-family: "Courier New", Courier, monospace;">
</span><br />
<div align="LEFT">
<span style="font-family: "Courier New", Courier, monospace;"> <b><span style="color: #7f0055;"><span style="color: #7f0055;"> return</span></span></b> <b><u><span style="color: #7f0055;"><span style="color: #7f0055;">new</span></span></u></b><u> T[2]</u>;</span></div>
<span style="font-family: "Courier New", Courier, monospace;">
</span><br />
<div align="LEFT">
<span style="font-family: "Courier New", Courier, monospace;"> }</span></div>
<span style="font-size: x-small;"><span style="font-size: small;">
<span style="font-family: "Courier New", Courier, monospace;">
</span></span></span><br />
<div align="LEFT">
<span style="font-size: x-small;"><span style="font-family: "Courier New", Courier, monospace; font-size: small;"> </span></span></div>
<span style="font-size: x-small;">
<span style="font-size: small;">
<span style="font-family: "Courier New", Courier, monospace;">
</span></span></span><br />
<div align="LEFT">
<span style="font-size: x-small;"><span style="font-family: "Courier New", Courier, monospace; font-size: small;">}</span></span></div>
<span style="font-size: x-small;">
</span><br />
<div align="LEFT">
<br /></div>
<span style="font-size: x-small;">
</span><div align="LEFT">
<br /></div>
<span style="font-size: x-small;">
</span><br />
Quantitative Coderhttp://www.blogger.com/profile/17386008753838448085noreply@blogger.com0