{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "This is a network constructor." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Import modules" ] }, { "cell_type": "code", "execution_count": 2, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "The snakeviz extension is already loaded. To reload it, use:\n", " %reload_ext snakeviz\n" ] } ], "source": [ "# This line configures matplotlib to show figures embedded in the notebook, \n", "# instead of opening a new window for each figure. More about that later. \n", "# If you are using an old version of IPython, try using '%pylab inline' instead.\n", "%matplotlib inline\n", "%load_ext snakeviz\n", "\n", "import numpy as np\n", "from scipy.optimize import minimize\n", "from scipy.special import expit\n", "import matplotlib.pyplot as plt\n", "\n", "from matplotlib.lines import Line2D\n", "\n", "import timeit\n", "\n", "import pandas as pd\n", "\n", "import plotly.plotly as py\n", "from plotly.graph_objs import *\n", "import plotly.tools as tls" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Predefine useful constants" ] }, { "cell_type": "code", "execution_count": 3, "metadata": { "collapsed": true }, "outputs": [], "source": [ "# hbar=1.054571726*10**(-34)\n", "hbar=1\n", "# delm2E=1\n", "# lamb=1 ## lambda for neutrinos\n", "# lambb=1 ## lambda for anti neutrinos\n", "# gF=1\n", "# nd=1 ## number density\n", "# ndb=1 ## number density\n", "omega=1\n", "omegab=-1\n", "\n", "## Here are some matrices to be used\n", "\n", "elM = np.array([[1,0],[0,0]])\n", "bM = 1/2*np.array( [ [ - 0.38729833462,0.31622776601] , [0.31622776601,0.38729833462] ] )\n", "\n", "## sqareroot of 2\n", "sqrt2=np.sqrt(2)" ] }, { "cell_type": "code", "execution_count": 4, "metadata": { "collapsed": true }, "outputs": [], "source": [ "def actf(x):\n", " #return 1/(1+np.exp(-x)) # It's not bad to define this function here for people could use other functions other than expit(x).\n", " return expit(x)" ] }, { "cell_type": "code", "execution_count": 5, "metadata": { "collapsed": true }, "outputs": [], "source": [ "## Very important notes\n", "## fOft(x,ti) should be specified, here ti is a scalar value not a list. and it should return a value.\n", "## Here I use t as the variables list and ti as the variable.\n", "\n", "\n", "\n", "def costODE(x,t,initialCondition,fOfArg): # x is a list of the order v,w,u. x will be splited to three equal parts.\n", " # initialCondition can only be constants.\n", "\n", " t = np.array(t)\n", " \n", " costODETotal = np.sum( costODETList(x,t,initialCondition,fOfArg) )\n", " \n", " return costODETotal\n", " \n", "\n", "def costODETList(x,t,initialCondition,fOfArg): ## This is the function WITHOUT the square!!! \n", " \n", " v,w,u = np.split(x,3)[:3]\n", " \n", " t = np.array(t)\n", " \n", " costList = np.asarray([])\n", " \n", " for temp in t:\n", " tempElement = costODETi(x,temp,initialCondition,fOfArg)\n", " costList = np.append(costList, tempElement)\n", " \n", " return np.array(costList)\n", "\n", " \n", "\n", "def costODETi(x,ti,initialCondition,fOfArg): # function for each t. here t is a single value \n", " # fOfArg is the function f(t) in the example\n", "\n", " v,w,u = np.split(x,3)[:3]\n", " \n", " args = np.array([x,ti,initialCondition])\n", " \n", " fvec = np.array(actf(ti*w + u) ) # This is a vector!!!\n", " ft = fOfArg(args) ## fOft should be specified in a problem!!!!!!!!!!!! And it takes a whole array of all the arguments\n", " # For a given t, this calculates the value of y(t), given the parameters, v, w, u. Notice this initialCondition.\n", " \n", " return ( np.sum (v*fvec + ti * v* fvec * ( 1 - fvec ) * w ) + ft ) ** 2\n", " \n", "\n", " \n", "## The funNNi(x,ti,initialCondition) takes a time ti and exports the function value with input x.\n", "\n", " \n", "def funNNi(x,ti,initialCondition): # for a single time stamp t\n", " \n", " v,w,u = np.split(x,3)[:3]\n", " \n", " return initialCondition + np.sum(ti * v * actf( ti*w +u ) )\n", "\n", "## funNNList(x,t,initialCondition) takes a list of time and exports the function values of these times.\n", "\n", "def funNNList(x,t,initialCondition):\n", " \n", " t = np.array(t)\n", " \n", " tempList = np.asarray([])\n", " \n", " for ti in t:\n", " tempElement = funNNi(x,ti,initialCondition)\n", " tempList = np.append(tempList,tempElement)\n", " \n", " return np.array(tempList)" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [], "source": [] } ], "metadata": { "kernelspec": { "display_name": "Python 2", "language": "python", "name": "python2" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 2 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython2", "version": "2.7.6" } }, "nbformat": 4, "nbformat_minor": 0 }