Python GUI Development Video Tutorial

Found this is at google+… Useful for anyone interested in Python GUI development. Found this to be a very helpful resource for learning to use PySide.

Sit back, watch and learn!!!

update:
if for some reason, the video do not work for you, please visit this youtube link.

Inspite of the environment and not because of it

20140717-194031.jpg

Here’s a stab from Rohit Chauhan’s model portfolio update.

As an investor, our portfolio cannot be built on hope. As a result, i am not picking or retaining stocks based on some macro event or grand thesis playing out (restructuring of PSU or removal of subsidies etc). I would like to hold companies in the portfolio which can perform inspite of the environment and not because of it.

All my investor/trader buddies are excited about the recent bull run and the prospect of Modi as PM.

Exact words that I want to convey to my enthusiastic investor buddies!!

Totally agree with him. And interestingly this applies not just to stocks! Just substitute portfolio with career!!

Foundation of FEM

Spring_FEM_in_Python
“Spring is the foundation of FEM. Understand this well and you are through in this course.” said the IISC professor as we settled on the hard benches on the first day of our 6 month FEM course at IISC, bangalore.

The course proved very useful. Learnt a lot. Fortunately , being in NAL had access to matlab and so converted the learning into matlab scripts. Today those scripts must be lying in some system in NAL.

Two months back, working on a problem in office, revisited all those notes and had this impulse to revisit the spring FEM problem in python…

So here’s the result. Two problem defined and solved in python.


"""
Created on Tue April 06 18:46:01 2014

@author: Sukhbinder Singh

Finite Element Analysis of spring system

problem 1

               #---^^^^^----#
#              #
#-----^^^^^----# ===> P
#              #
               #---^^^^^----#

1              2            3,4     

Spring Problem

P=100
ki=10
u1=u3=u4 =0


problem 2

#                             #
#------^^^^^^^^^^^^^^^^-------#
#                             #
#         |---^^^^--|         #
#---^^^^--|==>P     |--^^^^---#
#         |---^^^^--|         #
          |                   #
          |------^^^^^^-------#

1         2         3         4

k1=10,k2=15,k3=20,k4=25,k5=30,k6=35
P=100
u1=u4=0


Solution for problem 1
Displacement
0 0.0
1 3.33333333333
2 0.0
3 0.0

Reactions
0 -33.3333333333
2 -33.3333333333
3 -33.3333333333


Solution for problem 2
Displacement
0 0.0
1 1.54589371981
2 0.869565217391
3 0.0

Reactions
0 -23.1884057971
3 -76.8115942029


#usage of np.ix_
import numpy as np
A = np.array([ 1, 2, 3, 4, 4,
      2, 3, 4, 5, 3,
      4, 5, 6, 7, 2,
      5, 6, 7, 8, 9,
      6, 7, 8, 9, 0 ]).reshape(5, 5)

B = np.array([60, 70, 80, 90]).reshape(2, 2)
A[np.ix_([2, 4], [2, 4])] = B  


# usage of np.setdiff1d(ar1,ar2)
# Return the sorted, unique values in `ar1` that are not in `ar2`.
a = np.array([1, 2, 3, 2, 4, 1])
b = np.array([3, 4, 5, 6])
np.setdiff1d(a, b)

"""
import numpy as np

p1 = True # True for problem 1

if p1: # problem 1
    elemNodes= np.array([[0,1],[1,2],[1,3]])
    numElem = elemNodes.shape[0]
    numNodes=4
    presDof=np.array([0,2,3])
    k=np.array([10,10,10]) # Spring Stiffness
else: # problem 2
    elemNodes= np.array([[0,3],[0,1],[1,2],[1,2],[1,3],[2,3]])
    numElem = elemNodes.shape[0]
    numNodes=4
    presDof=np.array([0,3])
    k=np.array([10,15,20,25,30,35]) # Spring Stiffness

disp=np.zeros((numNodes,1))
force=np.zeros((numNodes,1))
stiffness=np.zeros((numNodes,numNodes))

force[1]=100.0

for e in xrange(numElem):
    stiffness[np.ix_(elemNodes[e,:],elemNodes[e,:])]+=np.array([[k[e],-k[e]],[-k[e],k[e]]])

actDof=np.setdiff1d(np.arange(numNodes),presDof)

disp1=np.linalg.solve(stiffness[np.ix_(actDof,actDof)],force[np.ix_(actDof)]);
disp[np.ix_(actDof)]=disp1

print "\nDisplacement"
for i,d in enumerate(disp):
    print i,d[0]

# reactions
react = np.dot(stiffness,disp)
reaction = react[presDof]

print "\nReactions"
for (p,r) in zip(presDof,reaction):
    print p,r[0]


Toy Turbojet

Toy Turboject
Was reading this post From Apprentice to Guru….,

Annoy your cubicle mates every time they present you with a Python class. Claim it could be “better” implemented as a dictionary plus some functions. Embrace functional programming.

The 9th point made me wonder and decided to write something using dictionary. As an aeronautical engineer, the first thing that came to mind was the thermal cyclic analysis of turbojet engine.

So here’s the toy turbojet program. Change some parameter and see how it changes the thrust and the specific fuel consumption, implemented using two simple dictionaries.


prgcon={"llp":10,"llt":4,"cp":1005,"r":287,"g":9.81,"tlr":6.5,
        "cfuel":45868.0,"prc":2.0,"tet":800,"pa":1.1013,"ta":291.15,
        "masc":0.1289,"effc":0.95,"effcom":0.99,"plcom":0.05,
        "bleed":0.01,"efmech":0.98,"effct":0.84,"effn":0.95,"powreq":1.0
        }

def toyturbojet(prgcon):
    result={"temps":[],"prs":[],"thrust":0.0,"spthrust":0.0,"sfc":0.0}
    prc=prgcon["prc"]
    t03=prgcon["tet"]
    gam=(prgcon["cp"]/prgcon["r"])/(prgcon["cp"]/prgcon["r"]-1)
    gamx=prgcon["cp"]/prgcon["r"]
    gamy=(gam-1.0)/2.0

    macfl=0.0
    r1=1.0
    pa=prgcon["pa"]
    ta=prgcon["ta"]

    p01=pa*r1*((1.0+gamy*macfl**2)**gamx)
    t01=ta*(1.0+gamy*macfl**2)
    den1=(p01*10000.0*prgcon["g"])/(prgcon["r"]*t01)

#print p01,t01,den1
    trais=prc**(1.0/gamx)
    tdrpis=t01*(trais-1.0)
    tdrp=tdrpis/prgcon["effc"]
    powerc=prgcon["masc"]*prgcon["cp"]*tdrp/1000.0
    p02=p01*prc
    t02=t01+tdrp

    mascom=prgcon["masc"]-prgcon["bleed"]*prgcon["masc"]
    tracom=t03-t02

    fara1=1.0
    if(tracom > 000  and tracom < 400):
        fara1=0.99 *((t03-t02-10)*(1.+t02/3250.))/(prgcon["cfuel"]*prgcon["effcom"])
    elif(tracom > 400 and tracom < 9000):
       fara1=1.10*((t03-t02-50.)*(1.+t02/3250.))/(prgcon["cfuel"]*prgcon["effcom"])

        
    masfuel=fara1*mascom
    p03=p02*(1.0-prgcon["plcom"])

    powert=powerc/prgcon["efmech"]
    masct=mascom+masfuel
    cp34=950.0+0.21*t03
    tdrpct=powert/(masct*cp34/1000.0)
    gamxc=cp34/prgcon["r"]
    tdctis=tdrpct/prgcon["effct"]
    t04is=t03-tdctis
    pract=(t03/t04is)**gamxc
    p04=p03/pract
    t04=t03-tdrpct

#print p01,p02,p03,p04,t01,t02,t03,t04

    rthst=prgcon["powreq"]
    p05i=p04
    t05=t04
    cp45=950.0+0.21*t04
    gamxp=cp45/prgcon["r"]
    gamn=gamxp/(gamxp-1.0)
    popan=p05i/pa
    if p05i < 1.1*pa:
        result["temps"]=([t01,t02,t03,t04,t05])
        result["prs"]=([p01,p02,p03,p04,0.0])
        result["thrust"]=0.0
        result["sfc"]=0.0
        result["spthrust"]=0.0
        return result
    emach=math.sqrt(2*(popan**(1/gamxp)-1)/(gamn-1))
    t5i=t05/(1+0.5*(gamn-1)*emach*emach)
    t5=t05-prgcon["effn"]*(t05-t5i)
    vjet=math.sqrt(2*cp45*(t05-t5))
    p05=pa*((t05/t5)**(gamn/(gamn-1)))
    massj=masct+prgcon["bleed"]+prgcon["masc"]
    thst=massj*vjet/prgcon["g"]
    ssp=thst/prgcon["masc"]
    sfc=masfuel/thst*3600.

    result["temps"]=([t01,t02,t03,t04,t05])
    result["prs"]=([p01,p02,p03,p04,p05])
    result["thrust"]=thst
    result["sfc"]=sfc
    result["spthrust"]=ssp

    return result


view the entire code from github

or view the entire notebook by clicking this link

Uncertainty is the friend of the buyer of long-term values

I am half way with this excellent book, The Warren Buffett Portfolio by Robert G. Hagstrom. Here are some of the most popular highlights by Kindle users from amazon.com for this book.

Loved the idea of viewing building a portfolio to building a company!!

Buffett does not adjust the discount rate for uncertainty. If one investment appears riskier than another, he keeps the discount rate constant and, instead, adjusts the purchase price.

Review annual reports from a few years back, paying special attention to what management said then about strategies for the future. • Compare those plans to today’s results: How fully were they realized? • Compare the strategies of a few years ago to this year’s strategies and ideas: How has the thinking changed? • Compare the annual reports of the company you are interested in with reports from similar companies in the same industry. It is not always easy to find exact duplicates, but even relative performance comparison can yield insights.

Buffett tells us that, in a low-interest-rate environment, he adjusts the discount rate upward. When bond yields dipped below 7 percent, Buffett adjusted his discount rate up to 10 percent. If interest rates work themselves higher over time, he has successfully matched his discount rate to the long-term rate. If they do not, he has increased his margin of safety by three additional points.

“The goal of each investor,” says Buffett, “should be to create a portfolio (in effect, a `company’) that will deliver him or her the highest possible look-through earnings a decade or so from now.”

“Charlie and I let our marketable equities tell us by their operating results-not by their daily, or even yearly, price quotations-whether our investments are successful,” explains Buffett. “The market may ignore business success for a while, but it eventually will confirm it.”

“Take the probability of loss times the amount of possible loss from the probability of gain times the amount of possible gain. That is what we’re trying to do,” says Buffett. “It’s imperfect, but that’s what it is all about.”

“You are neither right nor wrong because the crowd disagrees with you. You are right because your data and reasoning are right.”

“Face up to two unpleasant facts: the future is never clear and you pay a very high price in the stock market for a cheery consensus. Uncertainty is the friend of the buyer of long-term values.”

“Your goal as an investor should be simply to purchase, at a rational price, a part interest in an easily understandable business whose earnings are virtually certain to be materially higher, five, ten, and twenty years from now,” explains Buffett. “Over time, you will find only a few companies that meet these standards-so when you see one that qualifies, you should buy a meaningful amount of stock.”

Warren Buffett applies three tenets about a company’s management: (1) rationality, (2) candor, and (3) resisting the institutional imperative.

And then there was this mention of uncertainty, how could I resist? :)

Reading List for the Rest of the Year!

A few weeks back, posted about the books I have read so far in 2014. Well here’s list of books that I am planning to read by the end of the year. Fingers crossed.

15. Imagine by Jonah Lehrer
16. Think Like a Freak – Stephen J. Dubner. , Steven D Levitt
17. Would You Kill the Fat Man? by David Edmonds.
18. Risk Savvy: How to Make Good Decisions. by Gerd Gigerenzer
19. The Warren Buffet’s portfolio by Robert G. Hagstrom
20. The Power of Forgetting by Mike Byster
21. Cooked: A Natural history of transformation by Michael Pollan
22. Think Better by Tim Hurson
23. Guns, Germs and Steel by Jared Diamond
24. Overwhelmed by Brigid Schulte
25. Botany of Desire by Michael Pollan
26. The Idea hunter by Bill Fischer
27. Buffett – The Making of an American Capitalist by Roger Lowenstein
28. The emperor of all maladies by Siddhartha Mukherjee

Well thats it. This is the plan…

Government and software.

Read this sometime in Jan. But unfortunately lost the link to where I read it.

Regulating a society is like writing software. It sounds easy to amateurs, but it can be amazingly difficult.

Software programmers typically cannot prove that their software will work. At best, in many practical cases, they can test it for typical cases. Unfortunately, government regulators don’t follow any of the good practices software programmers use. Worse: they never accept bug reports.

Any programmer will tell you what to do when testing is hard and failure is not an option: keep the software as simple as possible.

That’s not at all what government do. And, unsurprisingly, they are full of bugs

Follow

Get every new post delivered to your Inbox.

Join 950 other followers

%d bloggers like this: