Permalink
Browse files

Add physics problem example

  • Loading branch information...
1 parent cac528e commit 82bf8fa286d5b9826fd2c83bee577a481a8c6bf7 @dharmatech committed Jan 19, 2013
Showing with 342 additions and 1 deletion.
  1. +63 −0 Physics/Physics.csproj
  2. +205 −0 Physics/Program.cs
  3. +36 −0 Physics/Properties/AssemblyInfo.cs
  4. +6 −0 Symbolism.sln
  5. +30 −1 Symbolism/Program.cs
  6. +2 −0 Tests/Program.cs
View
@@ -0,0 +1,63 @@
+<?xml version="1.0" encoding="utf-8"?>
+<Project ToolsVersion="4.0" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
+ <PropertyGroup>
+ <Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
+ <Platform Condition=" '$(Platform)' == '' ">x86</Platform>
+ <ProductVersion>8.0.30703</ProductVersion>
+ <SchemaVersion>2.0</SchemaVersion>
+ <ProjectGuid>{73E0C26D-5333-4575-A3E9-5388E975446E}</ProjectGuid>
+ <OutputType>Exe</OutputType>
+ <AppDesignerFolder>Properties</AppDesignerFolder>
+ <RootNamespace>Physics</RootNamespace>
+ <AssemblyName>Physics</AssemblyName>
+ <TargetFrameworkVersion>v4.0</TargetFrameworkVersion>
+ <TargetFrameworkProfile>Client</TargetFrameworkProfile>
+ <FileAlignment>512</FileAlignment>
+ </PropertyGroup>
+ <PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|x86' ">
+ <PlatformTarget>x86</PlatformTarget>
+ <DebugSymbols>true</DebugSymbols>
+ <DebugType>full</DebugType>
+ <Optimize>false</Optimize>
+ <OutputPath>bin\Debug\</OutputPath>
+ <DefineConstants>DEBUG;TRACE</DefineConstants>
+ <ErrorReport>prompt</ErrorReport>
+ <WarningLevel>4</WarningLevel>
+ </PropertyGroup>
+ <PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|x86' ">
+ <PlatformTarget>x86</PlatformTarget>
+ <DebugType>pdbonly</DebugType>
+ <Optimize>true</Optimize>
+ <OutputPath>bin\Release\</OutputPath>
+ <DefineConstants>TRACE</DefineConstants>
+ <ErrorReport>prompt</ErrorReport>
+ <WarningLevel>4</WarningLevel>
+ </PropertyGroup>
+ <ItemGroup>
+ <Reference Include="System" />
+ <Reference Include="System.Core" />
+ <Reference Include="System.Xml.Linq" />
+ <Reference Include="System.Data.DataSetExtensions" />
+ <Reference Include="Microsoft.CSharp" />
+ <Reference Include="System.Data" />
+ <Reference Include="System.Xml" />
+ </ItemGroup>
+ <ItemGroup>
+ <Compile Include="Program.cs" />
+ <Compile Include="Properties\AssemblyInfo.cs" />
+ </ItemGroup>
+ <ItemGroup>
+ <ProjectReference Include="..\Symbolism\Symbolism.csproj">
+ <Project>{067278C5-2A0F-42DE-838B-855EFB652608}</Project>
+ <Name>Symbolism</Name>
+ </ProjectReference>
+ </ItemGroup>
+ <Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
+ <!-- To modify your build process, add your task inside one of the targets below and uncomment it.
+ Other similar extension points exist, see Microsoft.Common.targets.
+ <Target Name="BeforeBuild">
+ </Target>
+ <Target Name="AfterBuild">
+ </Target>
+ -->
+</Project>
View
@@ -0,0 +1,205 @@
+using System;
+using System.Collections.Generic;
+using System.Linq;
+using System.Text;
+
+using Symbolism;
+
+namespace Physics
+{
+ static class Trig
+ {
+ public static Symbol Pi = new Symbol("Pi");
+
+ public static MathObject ToRadians(this MathObject n) { return n * Pi / 180; }
+
+ public static MathObject ToDegrees(this MathObject n) { return 180 * n / Pi; }
+
+ public static MathObject ToRadians(this int n) { return new Integer(n) * Pi / 180; }
+
+ public static MathObject ToDegrees(this int n) { return 180 * new Integer(n) / Pi; }
+
+ public static MathObject Sin(MathObject arg)
+ { return new Symbolism.Sin(arg).Simplify(); }
+
+ public static MathObject Cos(MathObject arg)
+ { return new Symbolism.Cos(arg).Simplify(); }
+ }
+
+ class Point
+ {
+ public MathObject x;
+ public MathObject y;
+
+ public Point() { }
+
+ public Point(int x_val, int y_val)
+ { x = new Integer(x_val); y = new Integer(y_val); }
+
+ public Point(MathObject x_val, MathObject y_val)
+ { x = x_val; y = y_val; }
+
+ public static Point FromAngle(MathObject angle, MathObject mag)
+ { return new Point(Trig.Cos(angle) * mag, Trig.Sin(angle) * mag); }
+
+ public override string ToString()
+ { return "Point(" + x + ", " + y + ")"; }
+
+ public static Point operator +(Point a, Point b)
+ { return new Point(a.x + b.x, a.y + b.y); }
+
+ public static Point operator *(Point a, MathObject b)
+ { return new Point(a.x * b, a.y * b); }
+
+ public static Point operator /(Point a, MathObject b)
+ { return new Point(a.x / b, a.y / b); }
+ }
+
+ class Obj
+ {
+ public Point position;
+ public Point velocity;
+ public Point acceleration;
+
+ public MathObject time;
+
+ public void Print()
+ {
+ Console.WriteLine(
+ "time: " + time + "\n" +
+ "position.x: " + position.x + "\n" +
+ "position.y: " + position.y + "\n" +
+ "velocity.x: " + velocity.x + "\n" +
+ "velocity.y: " + velocity.y + "\n" +
+ "acceleration.x: " + acceleration.x + "\n" +
+ "acceleration.y: " + acceleration.y + "\n");
+ }
+
+ public Obj AtTime(MathObject t)
+ {
+ var dt = t - time;
+
+ return
+ new Obj()
+ {
+ time = t,
+ acceleration = acceleration,
+ velocity = velocity + acceleration * dt,
+ position = position + velocity * dt + acceleration * dt * dt / new Integer(2)
+ };
+ }
+ }
+
+ static class Calc
+ {
+ public static MathObject Time(Obj a, Obj b)
+ {
+ if (!(a.velocity.x == null) &&
+ !(b.velocity.x == null) &&
+ !(a.acceleration.x == null) &&
+ !(a.acceleration.x == new DoubleFloat(0.0)) &&
+ !(a.acceleration.x == new Integer(0)))
+ return (b.velocity.x - a.velocity.x) / a.acceleration.x;
+
+ if (!(a.velocity.y == null) &&
+ !(b.velocity.y == null) &&
+ !(a.acceleration.y == null) &&
+ !(a.acceleration.y == new DoubleFloat(0.0)) &&
+ !(a.acceleration.y == new Integer(0)))
+ return (b.velocity.y - a.velocity.y) / a.acceleration.y;
+
+ throw new Exception();
+ }
+ }
+
+ class Program
+ {
+ static void Main(string[] args)
+ {
+ // A long-jumper leaves the ground at an angle of 20.0° above
+ // the horizontal and at a speed of 11.0 m/s.
+ // (a) How far does he jump in the horizontal direction?
+ // (Assume his motion is equivalent to that of a particle.)
+ // (b) What is the maximum height reached?
+
+ // For the purposes of solving the problem, let's take
+ // point A as the initial position
+ // point B as the peak position
+ // point C as the final position.
+
+ // First we'll get the answer in a symbolic form.
+
+ var thA = new Symbol("thA"); // angle at point A
+ var vA = new Symbol("vA"); // velocity at point A
+
+ // var g = new DoubleFloat(9.8);
+
+ var g = new Symbol("g");
+
+ var _g = new Point(new Integer(0), -g);
+
+ // An Obj representing the object at A:
+
+ var objA =
+ new Obj()
+ {
+ position = new Point(0, 0),
+ velocity = Point.FromAngle(thA, vA),
+ acceleration = _g,
+ time = new Integer(0)
+ };
+
+ var objB =
+ new Obj()
+ {
+ position = new Point(),
+ velocity = new Point(objA.velocity.x, new Integer(0)),
+ acceleration = _g
+ };
+
+ var timeB = Calc.Time(objA, objB);
+ var timeC = timeB * 2;
+
+ objB = objA.AtTime(timeB);
+ var objC = objA.AtTime(timeC);
+
+ Console.WriteLine("How far does he dump in the horizontal direction?");
+ Console.WriteLine(objC.position.x);
+
+ Console.WriteLine();
+
+ Console.WriteLine("What is the maximum height reached?");
+ Console.WriteLine(objB.position.y);
+
+ Console.WriteLine();
+
+ Console.WriteLine("Now for the numerical solutions:");
+
+ Console.WriteLine();
+
+ Console.WriteLine("Distance jumped: ");
+
+ Console.WriteLine(
+ objC.position.x
+ .Substitute(thA, Trig.ToRadians(20))
+ .Substitute(g, new DoubleFloat(9.8))
+ .Substitute(Trig.Pi, new DoubleFloat(3.14159))
+ .Substitute(vA, new Integer(11))
+ );
+
+ Console.WriteLine();
+
+ Console.WriteLine("Maximum height reached: ");
+
+ Console.WriteLine(
+ objB.position.y
+ .Substitute(g, new DoubleFloat(9.8))
+ .Substitute(thA, Trig.ToRadians(20))
+ .Substitute(Trig.Pi, new DoubleFloat(3.14159))
+ .Substitute(vA, new Integer(11))
+ );
+
+ Console.ReadLine();
+ }
+ }
+}
@@ -0,0 +1,36 @@
+using System.Reflection;
+using System.Runtime.CompilerServices;
+using System.Runtime.InteropServices;
+
+// General Information about an assembly is controlled through the following
+// set of attributes. Change these attribute values to modify the information
+// associated with an assembly.
+[assembly: AssemblyTitle("Physics")]
+[assembly: AssemblyDescription("")]
+[assembly: AssemblyConfiguration("")]
+[assembly: AssemblyCompany("")]
+[assembly: AssemblyProduct("Physics")]
+[assembly: AssemblyCopyright("Copyright © 2013")]
+[assembly: AssemblyTrademark("")]
+[assembly: AssemblyCulture("")]
+
+// Setting ComVisible to false makes the types in this assembly not visible
+// to COM components. If you need to access a type in this assembly from
+// COM, set the ComVisible attribute to true on that type.
+[assembly: ComVisible(false)]
+
+// The following GUID is for the ID of the typelib if this project is exposed to COM
+[assembly: Guid("0e78c36f-903c-42e0-9ead-836cc8c190d0")]
+
+// Version information for an assembly consists of the following four values:
+//
+// Major Version
+// Minor Version
+// Build Number
+// Revision
+//
+// You can specify all the values or you can default the Build and Revision Numbers
+// by using the '*' as shown below:
+// [assembly: AssemblyVersion("1.0.*")]
+[assembly: AssemblyVersion("1.0.0.0")]
+[assembly: AssemblyFileVersion("1.0.0.0")]
View
@@ -5,6 +5,8 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Symbolism", "Symbolism\Symb
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Tests", "Tests\Tests.csproj", "{9E996759-C2E9-4265-A8CE-16341B26D1CC}"
EndProject
+Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Physics", "Physics\Physics.csproj", "{73E0C26D-5333-4575-A3E9-5388E975446E}"
+EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|x86 = Debug|x86
@@ -19,6 +21,10 @@ Global
{9E996759-C2E9-4265-A8CE-16341B26D1CC}.Debug|x86.Build.0 = Debug|x86
{9E996759-C2E9-4265-A8CE-16341B26D1CC}.Release|x86.ActiveCfg = Release|x86
{9E996759-C2E9-4265-A8CE-16341B26D1CC}.Release|x86.Build.0 = Release|x86
+ {73E0C26D-5333-4575-A3E9-5388E975446E}.Debug|x86.ActiveCfg = Debug|x86
+ {73E0C26D-5333-4575-A3E9-5388E975446E}.Debug|x86.Build.0 = Debug|x86
+ {73E0C26D-5333-4575-A3E9-5388E975446E}.Release|x86.ActiveCfg = Release|x86
+ {73E0C26D-5333-4575-A3E9-5388E975446E}.Release|x86.Build.0 = Release|x86
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
View
@@ -123,8 +123,10 @@ public MathObject Substitute(MathObject a, MathObject b)
public int Precedence()
{
if (this is Integer) return 1000;
+ if (this is DoubleFloat) return 1000;
if (this is Symbol) return 1000;
if (this is Function) return 1000;
+ if (this is Fraction) return 1000;
if (this is Product) return 120;
if (this is Sum) return 110;
if (this is Power) return 60;
@@ -160,6 +162,12 @@ public Boolean ToBoolean()
if (a is Power && b is Power) return ((Power)a).Equals(b);
if (a is Function && b is Function) return ((Function)a).Equals(b);
+ if ((((object)a) == null) && (((object)b) == null)) return true;
+
+ if (((object)a) == null) return false;
+
+ if (((object)b) == null) return false;
+
if (a.GetType() != b.GetType()) return false;
Console.WriteLine("" + a.GetType() + " " + b.GetType());
@@ -226,8 +234,11 @@ public class Fraction : Number
public Fraction(Integer a, Integer b)
{ numerator = a; denominator = b; }
+ //public override string ToString()
+ //{ return "Fraction(" + numerator + ", " + denominator + ")"; }
+
public override string ToString()
- { return "Fraction(" + numerator + ", " + denominator + ")"; }
+ { return numerator + "/" + denominator; }
public DoubleFloat ToDouble() { return new DoubleFloat((double)numerator.val / (double)denominator.val); }
//////////////////////////////////////////////////////////////////////
@@ -566,6 +577,24 @@ public Sin(MathObject param)
}
}
+ public class Cos : Function
+ {
+ MathObject CosProc(params MathObject[] ls)
+ {
+ if (ls[0] is DoubleFloat)
+ return new DoubleFloat(Math.Cos(((DoubleFloat)ls[0]).val));
+
+ return new Cos(ls[0]);
+ }
+
+ public Cos(MathObject param)
+ {
+ name = "cos";
+ args = new List<MathObject>() { param };
+ proc = CosProc;
+ }
+ }
+
public static class ListUtils
{
public static bool IsEmpty(this List<MathObject> obj)
View
@@ -141,6 +141,8 @@ static void Main(string[] args)
AssertIsTrue(x * 1 == x);
}
+ Console.WriteLine("Testing complete");
+
Console.ReadLine();
}

0 comments on commit 82bf8fa

Please sign in to comment.