Initial commit
This commit is contained in:
commit
9244de43f1
112 changed files with 29394 additions and 0 deletions
45
.gitignore
vendored
Normal file
45
.gitignore
vendored
Normal file
|
|
@ -0,0 +1,45 @@
|
|||
# Unity default ignore files
|
||||
/[Ll]ibrary/
|
||||
/[Tt]emp/
|
||||
/[Oo]bj/
|
||||
/[Bb]uild/
|
||||
/[Bb]uilds/
|
||||
/[Ll]ogs/
|
||||
/[Mm]emoryCaptures/
|
||||
/[Aa]rchive/
|
||||
|
||||
# Asset meta files for folders ignored above
|
||||
/[Ll]ibrary.meta
|
||||
/[Tt]emp.meta
|
||||
/[Oo]bj.meta
|
||||
/[Bb]uild.meta
|
||||
/[Bb]uilds.meta
|
||||
/[Ss]ave.meta
|
||||
|
||||
# Autogenerated VS/MD/Consulo solution and project files
|
||||
ExportedObj/
|
||||
.consulo/
|
||||
*.csproj
|
||||
*.unityproj
|
||||
*.sln
|
||||
*.suo
|
||||
*.tmp
|
||||
*.user
|
||||
*.userprefs
|
||||
*.pidb
|
||||
*.booproj
|
||||
*.svd
|
||||
*.pdb
|
||||
*.opendb
|
||||
*.tlog
|
||||
|
||||
# Unity3D generated meta files
|
||||
sysinfo.txt
|
||||
|
||||
# Builds and big binaries (GitHub has a 100MB limit per file, so we must exclude these)
|
||||
*.exe
|
||||
*.zip
|
||||
*.apk
|
||||
*.unitypackage
|
||||
*.app
|
||||
.vs/
|
||||
6
.vsconfig
Normal file
6
.vsconfig
Normal file
|
|
@ -0,0 +1,6 @@
|
|||
{
|
||||
"version": "1.0",
|
||||
"components": [
|
||||
"Microsoft.VisualStudio.Workload.ManagedGame"
|
||||
]
|
||||
}
|
||||
8
Assets/Prefabs.meta
Normal file
8
Assets/Prefabs.meta
Normal file
|
|
@ -0,0 +1,8 @@
|
|||
fileFormatVersion: 2
|
||||
guid: 5f0e3526b7b08a245a694381f466ff41
|
||||
folderAsset: yes
|
||||
DefaultImporter:
|
||||
externalObjects: {}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
299
Assets/Prefabs/INPTEquation.prefab
Normal file
299
Assets/Prefabs/INPTEquation.prefab
Normal file
|
|
@ -0,0 +1,299 @@
|
|||
%YAML 1.1
|
||||
%TAG !u! tag:unity3d.com,2011:
|
||||
--- !u!1 &985874966542180765
|
||||
GameObject:
|
||||
m_ObjectHideFlags: 0
|
||||
m_CorrespondingSourceObject: {fileID: 0}
|
||||
m_PrefabInstance: {fileID: 0}
|
||||
m_PrefabAsset: {fileID: 0}
|
||||
serializedVersion: 6
|
||||
m_Component:
|
||||
- component: {fileID: 985874966542180762}
|
||||
- component: {fileID: 985874966542180760}
|
||||
- component: {fileID: 985874966542180763}
|
||||
m_Layer: 5
|
||||
m_Name: Text
|
||||
m_TagString: Untagged
|
||||
m_Icon: {fileID: 0}
|
||||
m_NavMeshLayer: 0
|
||||
m_StaticEditorFlags: 0
|
||||
m_IsActive: 1
|
||||
--- !u!224 &985874966542180762
|
||||
RectTransform:
|
||||
m_ObjectHideFlags: 0
|
||||
m_CorrespondingSourceObject: {fileID: 0}
|
||||
m_PrefabInstance: {fileID: 0}
|
||||
m_PrefabAsset: {fileID: 0}
|
||||
m_GameObject: {fileID: 985874966542180765}
|
||||
m_LocalRotation: {x: -0, y: -0, z: -0, w: 1}
|
||||
m_LocalPosition: {x: 0, y: 0, z: 0}
|
||||
m_LocalScale: {x: 1, y: 1, z: 1}
|
||||
m_Children: []
|
||||
m_Father: {fileID: 985874967725327421}
|
||||
m_RootOrder: 1
|
||||
m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0}
|
||||
m_AnchorMin: {x: 0, y: 0}
|
||||
m_AnchorMax: {x: 1, y: 1}
|
||||
m_AnchoredPosition: {x: 0, y: -0.5}
|
||||
m_SizeDelta: {x: -20, y: -13}
|
||||
m_Pivot: {x: 0.5, y: 0.5}
|
||||
--- !u!222 &985874966542180760
|
||||
CanvasRenderer:
|
||||
m_ObjectHideFlags: 0
|
||||
m_CorrespondingSourceObject: {fileID: 0}
|
||||
m_PrefabInstance: {fileID: 0}
|
||||
m_PrefabAsset: {fileID: 0}
|
||||
m_GameObject: {fileID: 985874966542180765}
|
||||
m_CullTransparentMesh: 0
|
||||
--- !u!114 &985874966542180763
|
||||
MonoBehaviour:
|
||||
m_ObjectHideFlags: 0
|
||||
m_CorrespondingSourceObject: {fileID: 0}
|
||||
m_PrefabInstance: {fileID: 0}
|
||||
m_PrefabAsset: {fileID: 0}
|
||||
m_GameObject: {fileID: 985874966542180765}
|
||||
m_Enabled: 1
|
||||
m_EditorHideFlags: 0
|
||||
m_Script: {fileID: 11500000, guid: 5f7201a12d95ffc409449d95f23cf332, type: 3}
|
||||
m_Name:
|
||||
m_EditorClassIdentifier:
|
||||
m_Material: {fileID: 0}
|
||||
m_Color: {r: 0.19607843, g: 0.19607843, b: 0.19607843, a: 1}
|
||||
m_RaycastTarget: 1
|
||||
m_Maskable: 1
|
||||
m_OnCullStateChanged:
|
||||
m_PersistentCalls:
|
||||
m_Calls: []
|
||||
m_FontData:
|
||||
m_Font: {fileID: 10102, guid: 0000000000000000e000000000000000, type: 0}
|
||||
m_FontSize: 25
|
||||
m_FontStyle: 0
|
||||
m_BestFit: 0
|
||||
m_MinSize: 1
|
||||
m_MaxSize: 40
|
||||
m_Alignment: 3
|
||||
m_AlignByGeometry: 0
|
||||
m_RichText: 0
|
||||
m_HorizontalOverflow: 1
|
||||
m_VerticalOverflow: 0
|
||||
m_LineSpacing: 1
|
||||
m_Text:
|
||||
--- !u!1 &985874967524545953
|
||||
GameObject:
|
||||
m_ObjectHideFlags: 0
|
||||
m_CorrespondingSourceObject: {fileID: 0}
|
||||
m_PrefabInstance: {fileID: 0}
|
||||
m_PrefabAsset: {fileID: 0}
|
||||
serializedVersion: 6
|
||||
m_Component:
|
||||
- component: {fileID: 985874967524545966}
|
||||
- component: {fileID: 985874967524545964}
|
||||
- component: {fileID: 985874967524545967}
|
||||
m_Layer: 5
|
||||
m_Name: Placeholder
|
||||
m_TagString: Untagged
|
||||
m_Icon: {fileID: 0}
|
||||
m_NavMeshLayer: 0
|
||||
m_StaticEditorFlags: 0
|
||||
m_IsActive: 1
|
||||
--- !u!224 &985874967524545966
|
||||
RectTransform:
|
||||
m_ObjectHideFlags: 0
|
||||
m_CorrespondingSourceObject: {fileID: 0}
|
||||
m_PrefabInstance: {fileID: 0}
|
||||
m_PrefabAsset: {fileID: 0}
|
||||
m_GameObject: {fileID: 985874967524545953}
|
||||
m_LocalRotation: {x: -0, y: -0, z: -0, w: 1}
|
||||
m_LocalPosition: {x: 0, y: 0, z: 0}
|
||||
m_LocalScale: {x: 1, y: 1, z: 1}
|
||||
m_Children: []
|
||||
m_Father: {fileID: 985874967725327421}
|
||||
m_RootOrder: 0
|
||||
m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0}
|
||||
m_AnchorMin: {x: 0, y: 0}
|
||||
m_AnchorMax: {x: 1, y: 1}
|
||||
m_AnchoredPosition: {x: 0, y: -0.5}
|
||||
m_SizeDelta: {x: -20, y: -13}
|
||||
m_Pivot: {x: 0.5, y: 0.5}
|
||||
--- !u!222 &985874967524545964
|
||||
CanvasRenderer:
|
||||
m_ObjectHideFlags: 0
|
||||
m_CorrespondingSourceObject: {fileID: 0}
|
||||
m_PrefabInstance: {fileID: 0}
|
||||
m_PrefabAsset: {fileID: 0}
|
||||
m_GameObject: {fileID: 985874967524545953}
|
||||
m_CullTransparentMesh: 0
|
||||
--- !u!114 &985874967524545967
|
||||
MonoBehaviour:
|
||||
m_ObjectHideFlags: 0
|
||||
m_CorrespondingSourceObject: {fileID: 0}
|
||||
m_PrefabInstance: {fileID: 0}
|
||||
m_PrefabAsset: {fileID: 0}
|
||||
m_GameObject: {fileID: 985874967524545953}
|
||||
m_Enabled: 1
|
||||
m_EditorHideFlags: 0
|
||||
m_Script: {fileID: 11500000, guid: 5f7201a12d95ffc409449d95f23cf332, type: 3}
|
||||
m_Name:
|
||||
m_EditorClassIdentifier:
|
||||
m_Material: {fileID: 0}
|
||||
m_Color: {r: 0.19607843, g: 0.19607843, b: 0.19607843, a: 0.5}
|
||||
m_RaycastTarget: 1
|
||||
m_Maskable: 1
|
||||
m_OnCullStateChanged:
|
||||
m_PersistentCalls:
|
||||
m_Calls: []
|
||||
m_FontData:
|
||||
m_Font: {fileID: 10102, guid: 0000000000000000e000000000000000, type: 0}
|
||||
m_FontSize: 25
|
||||
m_FontStyle: 2
|
||||
m_BestFit: 0
|
||||
m_MinSize: 1
|
||||
m_MaxSize: 40
|
||||
m_Alignment: 3
|
||||
m_AlignByGeometry: 0
|
||||
m_RichText: 1
|
||||
m_HorizontalOverflow: 0
|
||||
m_VerticalOverflow: 0
|
||||
m_LineSpacing: 1
|
||||
m_Text: Enter equation...
|
||||
--- !u!1 &985874967725327420
|
||||
GameObject:
|
||||
m_ObjectHideFlags: 0
|
||||
m_CorrespondingSourceObject: {fileID: 0}
|
||||
m_PrefabInstance: {fileID: 0}
|
||||
m_PrefabAsset: {fileID: 0}
|
||||
serializedVersion: 6
|
||||
m_Component:
|
||||
- component: {fileID: 985874967725327421}
|
||||
- component: {fileID: 985874967725327416}
|
||||
- component: {fileID: 985874967725327419}
|
||||
- component: {fileID: 985874967725327418}
|
||||
m_Layer: 5
|
||||
m_Name: INPTEquation
|
||||
m_TagString: Untagged
|
||||
m_Icon: {fileID: 0}
|
||||
m_NavMeshLayer: 0
|
||||
m_StaticEditorFlags: 0
|
||||
m_IsActive: 1
|
||||
--- !u!224 &985874967725327421
|
||||
RectTransform:
|
||||
m_ObjectHideFlags: 0
|
||||
m_CorrespondingSourceObject: {fileID: 0}
|
||||
m_PrefabInstance: {fileID: 0}
|
||||
m_PrefabAsset: {fileID: 0}
|
||||
m_GameObject: {fileID: 985874967725327420}
|
||||
m_LocalRotation: {x: -0, y: -0, z: -0, w: 1}
|
||||
m_LocalPosition: {x: 0, y: 0, z: 0}
|
||||
m_LocalScale: {x: 1, y: 1, z: 1}
|
||||
m_Children:
|
||||
- {fileID: 985874967524545966}
|
||||
- {fileID: 985874966542180762}
|
||||
m_Father: {fileID: 0}
|
||||
m_RootOrder: 0
|
||||
m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0}
|
||||
m_AnchorMin: {x: 0, y: 0}
|
||||
m_AnchorMax: {x: 0, y: 0}
|
||||
m_AnchoredPosition: {x: 0, y: 0}
|
||||
m_SizeDelta: {x: 600, y: 75}
|
||||
m_Pivot: {x: 0.5, y: 0.5}
|
||||
--- !u!222 &985874967725327416
|
||||
CanvasRenderer:
|
||||
m_ObjectHideFlags: 0
|
||||
m_CorrespondingSourceObject: {fileID: 0}
|
||||
m_PrefabInstance: {fileID: 0}
|
||||
m_PrefabAsset: {fileID: 0}
|
||||
m_GameObject: {fileID: 985874967725327420}
|
||||
m_CullTransparentMesh: 0
|
||||
--- !u!114 &985874967725327419
|
||||
MonoBehaviour:
|
||||
m_ObjectHideFlags: 0
|
||||
m_CorrespondingSourceObject: {fileID: 0}
|
||||
m_PrefabInstance: {fileID: 0}
|
||||
m_PrefabAsset: {fileID: 0}
|
||||
m_GameObject: {fileID: 985874967725327420}
|
||||
m_Enabled: 1
|
||||
m_EditorHideFlags: 0
|
||||
m_Script: {fileID: 11500000, guid: fe87c0e1cc204ed48ad3b37840f39efc, type: 3}
|
||||
m_Name:
|
||||
m_EditorClassIdentifier:
|
||||
m_Material: {fileID: 0}
|
||||
m_Color: {r: 1, g: 1, b: 1, a: 1}
|
||||
m_RaycastTarget: 1
|
||||
m_Maskable: 1
|
||||
m_OnCullStateChanged:
|
||||
m_PersistentCalls:
|
||||
m_Calls: []
|
||||
m_Sprite: {fileID: 0}
|
||||
m_Type: 1
|
||||
m_PreserveAspect: 0
|
||||
m_FillCenter: 1
|
||||
m_FillMethod: 4
|
||||
m_FillAmount: 1
|
||||
m_FillClockwise: 1
|
||||
m_FillOrigin: 0
|
||||
m_UseSpriteMesh: 0
|
||||
m_PixelsPerUnitMultiplier: 1
|
||||
--- !u!114 &985874967725327418
|
||||
MonoBehaviour:
|
||||
m_ObjectHideFlags: 0
|
||||
m_CorrespondingSourceObject: {fileID: 0}
|
||||
m_PrefabInstance: {fileID: 0}
|
||||
m_PrefabAsset: {fileID: 0}
|
||||
m_GameObject: {fileID: 985874967725327420}
|
||||
m_Enabled: 1
|
||||
m_EditorHideFlags: 0
|
||||
m_Script: {fileID: 11500000, guid: d199490a83bb2b844b9695cbf13b01ef, type: 3}
|
||||
m_Name:
|
||||
m_EditorClassIdentifier:
|
||||
m_Navigation:
|
||||
m_Mode: 3
|
||||
m_SelectOnUp: {fileID: 0}
|
||||
m_SelectOnDown: {fileID: 0}
|
||||
m_SelectOnLeft: {fileID: 0}
|
||||
m_SelectOnRight: {fileID: 0}
|
||||
m_Transition: 1
|
||||
m_Colors:
|
||||
m_NormalColor: {r: 1, g: 1, b: 1, a: 1}
|
||||
m_HighlightedColor: {r: 0.9607843, g: 0.9607843, b: 0.9607843, a: 1}
|
||||
m_PressedColor: {r: 0.78431374, g: 0.78431374, b: 0.78431374, a: 1}
|
||||
m_SelectedColor: {r: 0.9607843, g: 0.9607843, b: 0.9607843, a: 1}
|
||||
m_DisabledColor: {r: 0.78431374, g: 0.78431374, b: 0.78431374, a: 0.5019608}
|
||||
m_ColorMultiplier: 1
|
||||
m_FadeDuration: 0.1
|
||||
m_SpriteState:
|
||||
m_HighlightedSprite: {fileID: 0}
|
||||
m_PressedSprite: {fileID: 0}
|
||||
m_SelectedSprite: {fileID: 0}
|
||||
m_DisabledSprite: {fileID: 0}
|
||||
m_AnimationTriggers:
|
||||
m_NormalTrigger: Normal
|
||||
m_HighlightedTrigger: Highlighted
|
||||
m_PressedTrigger: Pressed
|
||||
m_SelectedTrigger: Selected
|
||||
m_DisabledTrigger: Disabled
|
||||
m_Interactable: 1
|
||||
m_TargetGraphic: {fileID: 985874967725327419}
|
||||
m_TextComponent: {fileID: 985874966542180763}
|
||||
m_Placeholder: {fileID: 985874967524545967}
|
||||
m_ContentType: 0
|
||||
m_InputType: 0
|
||||
m_AsteriskChar: 42
|
||||
m_KeyboardType: 0
|
||||
m_LineType: 0
|
||||
m_HideMobileInput: 0
|
||||
m_CharacterValidation: 0
|
||||
m_CharacterLimit: 0
|
||||
m_OnEndEdit:
|
||||
m_PersistentCalls:
|
||||
m_Calls: []
|
||||
m_OnValueChanged:
|
||||
m_PersistentCalls:
|
||||
m_Calls: []
|
||||
m_CaretColor: {r: 0.19607843, g: 0.19607843, b: 0.19607843, a: 1}
|
||||
m_CustomCaretColor: 0
|
||||
m_SelectionColor: {r: 0.65882355, g: 0.80784315, b: 1, a: 0.7529412}
|
||||
m_Text:
|
||||
m_CaretBlinkRate: 0.85
|
||||
m_CaretWidth: 1
|
||||
m_ReadOnly: 0
|
||||
m_ShouldActivateOnSelect: 1
|
||||
7
Assets/Prefabs/INPTEquation.prefab.meta
Normal file
7
Assets/Prefabs/INPTEquation.prefab.meta
Normal file
|
|
@ -0,0 +1,7 @@
|
|||
fileFormatVersion: 2
|
||||
guid: fccb4ef9675556c439d94f41f15a8e1a
|
||||
PrefabImporter:
|
||||
externalObjects: {}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
299
Assets/Prefabs/INPTInitialPoint.prefab
Normal file
299
Assets/Prefabs/INPTInitialPoint.prefab
Normal file
|
|
@ -0,0 +1,299 @@
|
|||
%YAML 1.1
|
||||
%TAG !u! tag:unity3d.com,2011:
|
||||
--- !u!1 &2895230885503808811
|
||||
GameObject:
|
||||
m_ObjectHideFlags: 0
|
||||
m_CorrespondingSourceObject: {fileID: 0}
|
||||
m_PrefabInstance: {fileID: 0}
|
||||
m_PrefabAsset: {fileID: 0}
|
||||
serializedVersion: 6
|
||||
m_Component:
|
||||
- component: {fileID: 2895230885503808810}
|
||||
- component: {fileID: 2895230885503808807}
|
||||
- component: {fileID: 2895230885503808808}
|
||||
- component: {fileID: 2895230885503808809}
|
||||
m_Layer: 5
|
||||
m_Name: INPTInitialPoint
|
||||
m_TagString: Untagged
|
||||
m_Icon: {fileID: 0}
|
||||
m_NavMeshLayer: 0
|
||||
m_StaticEditorFlags: 0
|
||||
m_IsActive: 1
|
||||
--- !u!224 &2895230885503808810
|
||||
RectTransform:
|
||||
m_ObjectHideFlags: 0
|
||||
m_CorrespondingSourceObject: {fileID: 0}
|
||||
m_PrefabInstance: {fileID: 0}
|
||||
m_PrefabAsset: {fileID: 0}
|
||||
m_GameObject: {fileID: 2895230885503808811}
|
||||
m_LocalRotation: {x: -0, y: -0, z: -0, w: 1}
|
||||
m_LocalPosition: {x: 0, y: 0, z: 0}
|
||||
m_LocalScale: {x: 1, y: 1, z: 1}
|
||||
m_Children:
|
||||
- {fileID: 2895230886245064501}
|
||||
- {fileID: 2895230886262753959}
|
||||
m_Father: {fileID: 0}
|
||||
m_RootOrder: 0
|
||||
m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0}
|
||||
m_AnchorMin: {x: 0, y: 0}
|
||||
m_AnchorMax: {x: 0, y: 0}
|
||||
m_AnchoredPosition: {x: 0, y: 0}
|
||||
m_SizeDelta: {x: 200, y: 75}
|
||||
m_Pivot: {x: 0.5, y: 0.5}
|
||||
--- !u!222 &2895230885503808807
|
||||
CanvasRenderer:
|
||||
m_ObjectHideFlags: 0
|
||||
m_CorrespondingSourceObject: {fileID: 0}
|
||||
m_PrefabInstance: {fileID: 0}
|
||||
m_PrefabAsset: {fileID: 0}
|
||||
m_GameObject: {fileID: 2895230885503808811}
|
||||
m_CullTransparentMesh: 0
|
||||
--- !u!114 &2895230885503808808
|
||||
MonoBehaviour:
|
||||
m_ObjectHideFlags: 0
|
||||
m_CorrespondingSourceObject: {fileID: 0}
|
||||
m_PrefabInstance: {fileID: 0}
|
||||
m_PrefabAsset: {fileID: 0}
|
||||
m_GameObject: {fileID: 2895230885503808811}
|
||||
m_Enabled: 1
|
||||
m_EditorHideFlags: 0
|
||||
m_Script: {fileID: 11500000, guid: fe87c0e1cc204ed48ad3b37840f39efc, type: 3}
|
||||
m_Name:
|
||||
m_EditorClassIdentifier:
|
||||
m_Material: {fileID: 0}
|
||||
m_Color: {r: 1, g: 1, b: 1, a: 1}
|
||||
m_RaycastTarget: 1
|
||||
m_Maskable: 1
|
||||
m_OnCullStateChanged:
|
||||
m_PersistentCalls:
|
||||
m_Calls: []
|
||||
m_Sprite: {fileID: 0}
|
||||
m_Type: 1
|
||||
m_PreserveAspect: 0
|
||||
m_FillCenter: 1
|
||||
m_FillMethod: 4
|
||||
m_FillAmount: 1
|
||||
m_FillClockwise: 1
|
||||
m_FillOrigin: 0
|
||||
m_UseSpriteMesh: 0
|
||||
m_PixelsPerUnitMultiplier: 1
|
||||
--- !u!114 &2895230885503808809
|
||||
MonoBehaviour:
|
||||
m_ObjectHideFlags: 0
|
||||
m_CorrespondingSourceObject: {fileID: 0}
|
||||
m_PrefabInstance: {fileID: 0}
|
||||
m_PrefabAsset: {fileID: 0}
|
||||
m_GameObject: {fileID: 2895230885503808811}
|
||||
m_Enabled: 1
|
||||
m_EditorHideFlags: 0
|
||||
m_Script: {fileID: 11500000, guid: d199490a83bb2b844b9695cbf13b01ef, type: 3}
|
||||
m_Name:
|
||||
m_EditorClassIdentifier:
|
||||
m_Navigation:
|
||||
m_Mode: 3
|
||||
m_SelectOnUp: {fileID: 0}
|
||||
m_SelectOnDown: {fileID: 0}
|
||||
m_SelectOnLeft: {fileID: 0}
|
||||
m_SelectOnRight: {fileID: 0}
|
||||
m_Transition: 1
|
||||
m_Colors:
|
||||
m_NormalColor: {r: 1, g: 1, b: 1, a: 1}
|
||||
m_HighlightedColor: {r: 0.9607843, g: 0.9607843, b: 0.9607843, a: 1}
|
||||
m_PressedColor: {r: 0.78431374, g: 0.78431374, b: 0.78431374, a: 1}
|
||||
m_SelectedColor: {r: 0.9607843, g: 0.9607843, b: 0.9607843, a: 1}
|
||||
m_DisabledColor: {r: 0.78431374, g: 0.78431374, b: 0.78431374, a: 0.5019608}
|
||||
m_ColorMultiplier: 1
|
||||
m_FadeDuration: 0.1
|
||||
m_SpriteState:
|
||||
m_HighlightedSprite: {fileID: 0}
|
||||
m_PressedSprite: {fileID: 0}
|
||||
m_SelectedSprite: {fileID: 0}
|
||||
m_DisabledSprite: {fileID: 0}
|
||||
m_AnimationTriggers:
|
||||
m_NormalTrigger: Normal
|
||||
m_HighlightedTrigger: Highlighted
|
||||
m_PressedTrigger: Pressed
|
||||
m_SelectedTrigger: Selected
|
||||
m_DisabledTrigger: Disabled
|
||||
m_Interactable: 1
|
||||
m_TargetGraphic: {fileID: 2895230885503808808}
|
||||
m_TextComponent: {fileID: 2895230886262753958}
|
||||
m_Placeholder: {fileID: 2895230886245064500}
|
||||
m_ContentType: 3
|
||||
m_InputType: 0
|
||||
m_AsteriskChar: 42
|
||||
m_KeyboardType: 2
|
||||
m_LineType: 0
|
||||
m_HideMobileInput: 0
|
||||
m_CharacterValidation: 2
|
||||
m_CharacterLimit: 15
|
||||
m_OnEndEdit:
|
||||
m_PersistentCalls:
|
||||
m_Calls: []
|
||||
m_OnValueChanged:
|
||||
m_PersistentCalls:
|
||||
m_Calls: []
|
||||
m_CaretColor: {r: 0.19607843, g: 0.19607843, b: 0.19607843, a: 1}
|
||||
m_CustomCaretColor: 0
|
||||
m_SelectionColor: {r: 0.65882355, g: 0.80784315, b: 1, a: 0.7529412}
|
||||
m_Text:
|
||||
m_CaretBlinkRate: 0.85
|
||||
m_CaretWidth: 1
|
||||
m_ReadOnly: 0
|
||||
m_ShouldActivateOnSelect: 1
|
||||
--- !u!1 &2895230886245064502
|
||||
GameObject:
|
||||
m_ObjectHideFlags: 0
|
||||
m_CorrespondingSourceObject: {fileID: 0}
|
||||
m_PrefabInstance: {fileID: 0}
|
||||
m_PrefabAsset: {fileID: 0}
|
||||
serializedVersion: 6
|
||||
m_Component:
|
||||
- component: {fileID: 2895230886245064501}
|
||||
- component: {fileID: 2895230886245064499}
|
||||
- component: {fileID: 2895230886245064500}
|
||||
m_Layer: 5
|
||||
m_Name: Placeholder
|
||||
m_TagString: Untagged
|
||||
m_Icon: {fileID: 0}
|
||||
m_NavMeshLayer: 0
|
||||
m_StaticEditorFlags: 0
|
||||
m_IsActive: 1
|
||||
--- !u!224 &2895230886245064501
|
||||
RectTransform:
|
||||
m_ObjectHideFlags: 0
|
||||
m_CorrespondingSourceObject: {fileID: 0}
|
||||
m_PrefabInstance: {fileID: 0}
|
||||
m_PrefabAsset: {fileID: 0}
|
||||
m_GameObject: {fileID: 2895230886245064502}
|
||||
m_LocalRotation: {x: -0, y: -0, z: -0, w: 1}
|
||||
m_LocalPosition: {x: 0, y: 0, z: 0}
|
||||
m_LocalScale: {x: 1, y: 1, z: 1}
|
||||
m_Children: []
|
||||
m_Father: {fileID: 2895230885503808810}
|
||||
m_RootOrder: 0
|
||||
m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0}
|
||||
m_AnchorMin: {x: 0, y: 0}
|
||||
m_AnchorMax: {x: 1, y: 1}
|
||||
m_AnchoredPosition: {x: 0, y: -0.5}
|
||||
m_SizeDelta: {x: -20, y: -13}
|
||||
m_Pivot: {x: 0.5, y: 0.5}
|
||||
--- !u!222 &2895230886245064499
|
||||
CanvasRenderer:
|
||||
m_ObjectHideFlags: 0
|
||||
m_CorrespondingSourceObject: {fileID: 0}
|
||||
m_PrefabInstance: {fileID: 0}
|
||||
m_PrefabAsset: {fileID: 0}
|
||||
m_GameObject: {fileID: 2895230886245064502}
|
||||
m_CullTransparentMesh: 0
|
||||
--- !u!114 &2895230886245064500
|
||||
MonoBehaviour:
|
||||
m_ObjectHideFlags: 0
|
||||
m_CorrespondingSourceObject: {fileID: 0}
|
||||
m_PrefabInstance: {fileID: 0}
|
||||
m_PrefabAsset: {fileID: 0}
|
||||
m_GameObject: {fileID: 2895230886245064502}
|
||||
m_Enabled: 1
|
||||
m_EditorHideFlags: 0
|
||||
m_Script: {fileID: 11500000, guid: 5f7201a12d95ffc409449d95f23cf332, type: 3}
|
||||
m_Name:
|
||||
m_EditorClassIdentifier:
|
||||
m_Material: {fileID: 0}
|
||||
m_Color: {r: 0.19607843, g: 0.19607843, b: 0.19607843, a: 0.5}
|
||||
m_RaycastTarget: 1
|
||||
m_Maskable: 1
|
||||
m_OnCullStateChanged:
|
||||
m_PersistentCalls:
|
||||
m_Calls: []
|
||||
m_FontData:
|
||||
m_Font: {fileID: 10102, guid: 0000000000000000e000000000000000, type: 0}
|
||||
m_FontSize: 25
|
||||
m_FontStyle: 2
|
||||
m_BestFit: 0
|
||||
m_MinSize: 1
|
||||
m_MaxSize: 40
|
||||
m_Alignment: 3
|
||||
m_AlignByGeometry: 0
|
||||
m_RichText: 1
|
||||
m_HorizontalOverflow: 0
|
||||
m_VerticalOverflow: 0
|
||||
m_LineSpacing: 1
|
||||
m_Text: Initial point...
|
||||
--- !u!1 &2895230886262753960
|
||||
GameObject:
|
||||
m_ObjectHideFlags: 0
|
||||
m_CorrespondingSourceObject: {fileID: 0}
|
||||
m_PrefabInstance: {fileID: 0}
|
||||
m_PrefabAsset: {fileID: 0}
|
||||
serializedVersion: 6
|
||||
m_Component:
|
||||
- component: {fileID: 2895230886262753959}
|
||||
- component: {fileID: 2895230886262753957}
|
||||
- component: {fileID: 2895230886262753958}
|
||||
m_Layer: 5
|
||||
m_Name: Text
|
||||
m_TagString: Untagged
|
||||
m_Icon: {fileID: 0}
|
||||
m_NavMeshLayer: 0
|
||||
m_StaticEditorFlags: 0
|
||||
m_IsActive: 1
|
||||
--- !u!224 &2895230886262753959
|
||||
RectTransform:
|
||||
m_ObjectHideFlags: 0
|
||||
m_CorrespondingSourceObject: {fileID: 0}
|
||||
m_PrefabInstance: {fileID: 0}
|
||||
m_PrefabAsset: {fileID: 0}
|
||||
m_GameObject: {fileID: 2895230886262753960}
|
||||
m_LocalRotation: {x: -0, y: -0, z: -0, w: 1}
|
||||
m_LocalPosition: {x: 0, y: 0, z: 0}
|
||||
m_LocalScale: {x: 1, y: 1, z: 1}
|
||||
m_Children: []
|
||||
m_Father: {fileID: 2895230885503808810}
|
||||
m_RootOrder: 1
|
||||
m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0}
|
||||
m_AnchorMin: {x: 0, y: 0}
|
||||
m_AnchorMax: {x: 1, y: 1}
|
||||
m_AnchoredPosition: {x: 0, y: -0.5}
|
||||
m_SizeDelta: {x: -20, y: -13}
|
||||
m_Pivot: {x: 0.5, y: 0.5}
|
||||
--- !u!222 &2895230886262753957
|
||||
CanvasRenderer:
|
||||
m_ObjectHideFlags: 0
|
||||
m_CorrespondingSourceObject: {fileID: 0}
|
||||
m_PrefabInstance: {fileID: 0}
|
||||
m_PrefabAsset: {fileID: 0}
|
||||
m_GameObject: {fileID: 2895230886262753960}
|
||||
m_CullTransparentMesh: 0
|
||||
--- !u!114 &2895230886262753958
|
||||
MonoBehaviour:
|
||||
m_ObjectHideFlags: 0
|
||||
m_CorrespondingSourceObject: {fileID: 0}
|
||||
m_PrefabInstance: {fileID: 0}
|
||||
m_PrefabAsset: {fileID: 0}
|
||||
m_GameObject: {fileID: 2895230886262753960}
|
||||
m_Enabled: 1
|
||||
m_EditorHideFlags: 0
|
||||
m_Script: {fileID: 11500000, guid: 5f7201a12d95ffc409449d95f23cf332, type: 3}
|
||||
m_Name:
|
||||
m_EditorClassIdentifier:
|
||||
m_Material: {fileID: 0}
|
||||
m_Color: {r: 0.19607843, g: 0.19607843, b: 0.19607843, a: 1}
|
||||
m_RaycastTarget: 1
|
||||
m_Maskable: 1
|
||||
m_OnCullStateChanged:
|
||||
m_PersistentCalls:
|
||||
m_Calls: []
|
||||
m_FontData:
|
||||
m_Font: {fileID: 10102, guid: 0000000000000000e000000000000000, type: 0}
|
||||
m_FontSize: 25
|
||||
m_FontStyle: 0
|
||||
m_BestFit: 0
|
||||
m_MinSize: 1
|
||||
m_MaxSize: 40
|
||||
m_Alignment: 3
|
||||
m_AlignByGeometry: 0
|
||||
m_RichText: 0
|
||||
m_HorizontalOverflow: 1
|
||||
m_VerticalOverflow: 0
|
||||
m_LineSpacing: 1
|
||||
m_Text:
|
||||
7
Assets/Prefabs/INPTInitialPoint.prefab.meta
Normal file
7
Assets/Prefabs/INPTInitialPoint.prefab.meta
Normal file
|
|
@ -0,0 +1,7 @@
|
|||
fileFormatVersion: 2
|
||||
guid: dce03982ff0627e459979d3e508a5463
|
||||
PrefabImporter:
|
||||
externalObjects: {}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
299
Assets/Prefabs/INPTVariable.prefab
Normal file
299
Assets/Prefabs/INPTVariable.prefab
Normal file
|
|
@ -0,0 +1,299 @@
|
|||
%YAML 1.1
|
||||
%TAG !u! tag:unity3d.com,2011:
|
||||
--- !u!1 &2006212224380440147
|
||||
GameObject:
|
||||
m_ObjectHideFlags: 0
|
||||
m_CorrespondingSourceObject: {fileID: 0}
|
||||
m_PrefabInstance: {fileID: 0}
|
||||
m_PrefabAsset: {fileID: 0}
|
||||
serializedVersion: 6
|
||||
m_Component:
|
||||
- component: {fileID: 2006212224380440146}
|
||||
- component: {fileID: 2006212224380440148}
|
||||
- component: {fileID: 2006212224380440149}
|
||||
m_Layer: 5
|
||||
m_Name: Placeholder
|
||||
m_TagString: Untagged
|
||||
m_Icon: {fileID: 0}
|
||||
m_NavMeshLayer: 0
|
||||
m_StaticEditorFlags: 0
|
||||
m_IsActive: 1
|
||||
--- !u!224 &2006212224380440146
|
||||
RectTransform:
|
||||
m_ObjectHideFlags: 0
|
||||
m_CorrespondingSourceObject: {fileID: 0}
|
||||
m_PrefabInstance: {fileID: 0}
|
||||
m_PrefabAsset: {fileID: 0}
|
||||
m_GameObject: {fileID: 2006212224380440147}
|
||||
m_LocalRotation: {x: -0, y: -0, z: -0, w: 1}
|
||||
m_LocalPosition: {x: 0, y: 0, z: 0}
|
||||
m_LocalScale: {x: 1, y: 1, z: 1}
|
||||
m_Children: []
|
||||
m_Father: {fileID: 2006212224861606533}
|
||||
m_RootOrder: 0
|
||||
m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0}
|
||||
m_AnchorMin: {x: 0, y: 0}
|
||||
m_AnchorMax: {x: 1, y: 1}
|
||||
m_AnchoredPosition: {x: 0, y: -0.5}
|
||||
m_SizeDelta: {x: -20, y: -13}
|
||||
m_Pivot: {x: 0.5, y: 0.5}
|
||||
--- !u!222 &2006212224380440148
|
||||
CanvasRenderer:
|
||||
m_ObjectHideFlags: 0
|
||||
m_CorrespondingSourceObject: {fileID: 0}
|
||||
m_PrefabInstance: {fileID: 0}
|
||||
m_PrefabAsset: {fileID: 0}
|
||||
m_GameObject: {fileID: 2006212224380440147}
|
||||
m_CullTransparentMesh: 0
|
||||
--- !u!114 &2006212224380440149
|
||||
MonoBehaviour:
|
||||
m_ObjectHideFlags: 0
|
||||
m_CorrespondingSourceObject: {fileID: 0}
|
||||
m_PrefabInstance: {fileID: 0}
|
||||
m_PrefabAsset: {fileID: 0}
|
||||
m_GameObject: {fileID: 2006212224380440147}
|
||||
m_Enabled: 1
|
||||
m_EditorHideFlags: 0
|
||||
m_Script: {fileID: 11500000, guid: 5f7201a12d95ffc409449d95f23cf332, type: 3}
|
||||
m_Name:
|
||||
m_EditorClassIdentifier:
|
||||
m_Material: {fileID: 0}
|
||||
m_Color: {r: 0.19607843, g: 0.19607843, b: 0.19607843, a: 0.5}
|
||||
m_RaycastTarget: 1
|
||||
m_Maskable: 1
|
||||
m_OnCullStateChanged:
|
||||
m_PersistentCalls:
|
||||
m_Calls: []
|
||||
m_FontData:
|
||||
m_Font: {fileID: 10102, guid: 0000000000000000e000000000000000, type: 0}
|
||||
m_FontSize: 22
|
||||
m_FontStyle: 2
|
||||
m_BestFit: 0
|
||||
m_MinSize: 1
|
||||
m_MaxSize: 40
|
||||
m_Alignment: 3
|
||||
m_AlignByGeometry: 0
|
||||
m_RichText: 1
|
||||
m_HorizontalOverflow: 0
|
||||
m_VerticalOverflow: 0
|
||||
m_LineSpacing: 1
|
||||
m_Text: Variable
|
||||
--- !u!1 &2006212224861606530
|
||||
GameObject:
|
||||
m_ObjectHideFlags: 0
|
||||
m_CorrespondingSourceObject: {fileID: 0}
|
||||
m_PrefabInstance: {fileID: 0}
|
||||
m_PrefabAsset: {fileID: 0}
|
||||
serializedVersion: 6
|
||||
m_Component:
|
||||
- component: {fileID: 2006212224861606533}
|
||||
- component: {fileID: 2006212224861606534}
|
||||
- component: {fileID: 2006212224861606535}
|
||||
- component: {fileID: 2006212224861606532}
|
||||
m_Layer: 5
|
||||
m_Name: INPTVariable
|
||||
m_TagString: Untagged
|
||||
m_Icon: {fileID: 0}
|
||||
m_NavMeshLayer: 0
|
||||
m_StaticEditorFlags: 0
|
||||
m_IsActive: 1
|
||||
--- !u!224 &2006212224861606533
|
||||
RectTransform:
|
||||
m_ObjectHideFlags: 0
|
||||
m_CorrespondingSourceObject: {fileID: 0}
|
||||
m_PrefabInstance: {fileID: 0}
|
||||
m_PrefabAsset: {fileID: 0}
|
||||
m_GameObject: {fileID: 2006212224861606530}
|
||||
m_LocalRotation: {x: -0, y: -0, z: -0, w: 1}
|
||||
m_LocalPosition: {x: 0, y: 0, z: 0}
|
||||
m_LocalScale: {x: 1, y: 1, z: 1}
|
||||
m_Children:
|
||||
- {fileID: 2006212224380440146}
|
||||
- {fileID: 2006212225912600286}
|
||||
m_Father: {fileID: 0}
|
||||
m_RootOrder: 0
|
||||
m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0}
|
||||
m_AnchorMin: {x: 0, y: 0}
|
||||
m_AnchorMax: {x: 0, y: 0}
|
||||
m_AnchoredPosition: {x: 0, y: 0}
|
||||
m_SizeDelta: {x: 100, y: 75}
|
||||
m_Pivot: {x: 0.5, y: 0.5}
|
||||
--- !u!222 &2006212224861606534
|
||||
CanvasRenderer:
|
||||
m_ObjectHideFlags: 0
|
||||
m_CorrespondingSourceObject: {fileID: 0}
|
||||
m_PrefabInstance: {fileID: 0}
|
||||
m_PrefabAsset: {fileID: 0}
|
||||
m_GameObject: {fileID: 2006212224861606530}
|
||||
m_CullTransparentMesh: 0
|
||||
--- !u!114 &2006212224861606535
|
||||
MonoBehaviour:
|
||||
m_ObjectHideFlags: 0
|
||||
m_CorrespondingSourceObject: {fileID: 0}
|
||||
m_PrefabInstance: {fileID: 0}
|
||||
m_PrefabAsset: {fileID: 0}
|
||||
m_GameObject: {fileID: 2006212224861606530}
|
||||
m_Enabled: 1
|
||||
m_EditorHideFlags: 0
|
||||
m_Script: {fileID: 11500000, guid: fe87c0e1cc204ed48ad3b37840f39efc, type: 3}
|
||||
m_Name:
|
||||
m_EditorClassIdentifier:
|
||||
m_Material: {fileID: 0}
|
||||
m_Color: {r: 1, g: 1, b: 1, a: 1}
|
||||
m_RaycastTarget: 1
|
||||
m_Maskable: 1
|
||||
m_OnCullStateChanged:
|
||||
m_PersistentCalls:
|
||||
m_Calls: []
|
||||
m_Sprite: {fileID: 0}
|
||||
m_Type: 1
|
||||
m_PreserveAspect: 0
|
||||
m_FillCenter: 1
|
||||
m_FillMethod: 4
|
||||
m_FillAmount: 1
|
||||
m_FillClockwise: 1
|
||||
m_FillOrigin: 0
|
||||
m_UseSpriteMesh: 0
|
||||
m_PixelsPerUnitMultiplier: 1
|
||||
--- !u!114 &2006212224861606532
|
||||
MonoBehaviour:
|
||||
m_ObjectHideFlags: 0
|
||||
m_CorrespondingSourceObject: {fileID: 0}
|
||||
m_PrefabInstance: {fileID: 0}
|
||||
m_PrefabAsset: {fileID: 0}
|
||||
m_GameObject: {fileID: 2006212224861606530}
|
||||
m_Enabled: 1
|
||||
m_EditorHideFlags: 0
|
||||
m_Script: {fileID: 11500000, guid: d199490a83bb2b844b9695cbf13b01ef, type: 3}
|
||||
m_Name:
|
||||
m_EditorClassIdentifier:
|
||||
m_Navigation:
|
||||
m_Mode: 3
|
||||
m_SelectOnUp: {fileID: 0}
|
||||
m_SelectOnDown: {fileID: 0}
|
||||
m_SelectOnLeft: {fileID: 0}
|
||||
m_SelectOnRight: {fileID: 0}
|
||||
m_Transition: 1
|
||||
m_Colors:
|
||||
m_NormalColor: {r: 1, g: 1, b: 1, a: 1}
|
||||
m_HighlightedColor: {r: 0.9607843, g: 0.9607843, b: 0.9607843, a: 1}
|
||||
m_PressedColor: {r: 0.78431374, g: 0.78431374, b: 0.78431374, a: 1}
|
||||
m_SelectedColor: {r: 0.9607843, g: 0.9607843, b: 0.9607843, a: 1}
|
||||
m_DisabledColor: {r: 0.78431374, g: 0.78431374, b: 0.78431374, a: 0.5019608}
|
||||
m_ColorMultiplier: 1
|
||||
m_FadeDuration: 0.1
|
||||
m_SpriteState:
|
||||
m_HighlightedSprite: {fileID: 0}
|
||||
m_PressedSprite: {fileID: 0}
|
||||
m_SelectedSprite: {fileID: 0}
|
||||
m_DisabledSprite: {fileID: 0}
|
||||
m_AnimationTriggers:
|
||||
m_NormalTrigger: Normal
|
||||
m_HighlightedTrigger: Highlighted
|
||||
m_PressedTrigger: Pressed
|
||||
m_SelectedTrigger: Selected
|
||||
m_DisabledTrigger: Disabled
|
||||
m_Interactable: 1
|
||||
m_TargetGraphic: {fileID: 2006212224861606535}
|
||||
m_TextComponent: {fileID: 2006212225912600097}
|
||||
m_Placeholder: {fileID: 2006212224380440149}
|
||||
m_ContentType: 4
|
||||
m_InputType: 0
|
||||
m_AsteriskChar: 42
|
||||
m_KeyboardType: 1
|
||||
m_LineType: 0
|
||||
m_HideMobileInput: 0
|
||||
m_CharacterValidation: 3
|
||||
m_CharacterLimit: 1
|
||||
m_OnEndEdit:
|
||||
m_PersistentCalls:
|
||||
m_Calls: []
|
||||
m_OnValueChanged:
|
||||
m_PersistentCalls:
|
||||
m_Calls: []
|
||||
m_CaretColor: {r: 0.19607843, g: 0.19607843, b: 0.19607843, a: 1}
|
||||
m_CustomCaretColor: 0
|
||||
m_SelectionColor: {r: 0.65882355, g: 0.80784315, b: 1, a: 0.7529412}
|
||||
m_Text:
|
||||
m_CaretBlinkRate: 0.85
|
||||
m_CaretWidth: 1
|
||||
m_ReadOnly: 0
|
||||
m_ShouldActivateOnSelect: 1
|
||||
--- !u!1 &2006212225912600287
|
||||
GameObject:
|
||||
m_ObjectHideFlags: 0
|
||||
m_CorrespondingSourceObject: {fileID: 0}
|
||||
m_PrefabInstance: {fileID: 0}
|
||||
m_PrefabAsset: {fileID: 0}
|
||||
serializedVersion: 6
|
||||
m_Component:
|
||||
- component: {fileID: 2006212225912600286}
|
||||
- component: {fileID: 2006212225912600096}
|
||||
- component: {fileID: 2006212225912600097}
|
||||
m_Layer: 5
|
||||
m_Name: Text
|
||||
m_TagString: Untagged
|
||||
m_Icon: {fileID: 0}
|
||||
m_NavMeshLayer: 0
|
||||
m_StaticEditorFlags: 0
|
||||
m_IsActive: 1
|
||||
--- !u!224 &2006212225912600286
|
||||
RectTransform:
|
||||
m_ObjectHideFlags: 0
|
||||
m_CorrespondingSourceObject: {fileID: 0}
|
||||
m_PrefabInstance: {fileID: 0}
|
||||
m_PrefabAsset: {fileID: 0}
|
||||
m_GameObject: {fileID: 2006212225912600287}
|
||||
m_LocalRotation: {x: -0, y: -0, z: -0, w: 1}
|
||||
m_LocalPosition: {x: 0, y: 0, z: 0}
|
||||
m_LocalScale: {x: 1, y: 1, z: 1}
|
||||
m_Children: []
|
||||
m_Father: {fileID: 2006212224861606533}
|
||||
m_RootOrder: 1
|
||||
m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0}
|
||||
m_AnchorMin: {x: 0, y: 0}
|
||||
m_AnchorMax: {x: 1, y: 1}
|
||||
m_AnchoredPosition: {x: 0, y: -0.5}
|
||||
m_SizeDelta: {x: -20, y: -13}
|
||||
m_Pivot: {x: 0.5, y: 0.5}
|
||||
--- !u!222 &2006212225912600096
|
||||
CanvasRenderer:
|
||||
m_ObjectHideFlags: 0
|
||||
m_CorrespondingSourceObject: {fileID: 0}
|
||||
m_PrefabInstance: {fileID: 0}
|
||||
m_PrefabAsset: {fileID: 0}
|
||||
m_GameObject: {fileID: 2006212225912600287}
|
||||
m_CullTransparentMesh: 0
|
||||
--- !u!114 &2006212225912600097
|
||||
MonoBehaviour:
|
||||
m_ObjectHideFlags: 0
|
||||
m_CorrespondingSourceObject: {fileID: 0}
|
||||
m_PrefabInstance: {fileID: 0}
|
||||
m_PrefabAsset: {fileID: 0}
|
||||
m_GameObject: {fileID: 2006212225912600287}
|
||||
m_Enabled: 1
|
||||
m_EditorHideFlags: 0
|
||||
m_Script: {fileID: 11500000, guid: 5f7201a12d95ffc409449d95f23cf332, type: 3}
|
||||
m_Name:
|
||||
m_EditorClassIdentifier:
|
||||
m_Material: {fileID: 0}
|
||||
m_Color: {r: 0.19607843, g: 0.19607843, b: 0.19607843, a: 1}
|
||||
m_RaycastTarget: 1
|
||||
m_Maskable: 1
|
||||
m_OnCullStateChanged:
|
||||
m_PersistentCalls:
|
||||
m_Calls: []
|
||||
m_FontData:
|
||||
m_Font: {fileID: 10102, guid: 0000000000000000e000000000000000, type: 0}
|
||||
m_FontSize: 30
|
||||
m_FontStyle: 0
|
||||
m_BestFit: 0
|
||||
m_MinSize: 1
|
||||
m_MaxSize: 40
|
||||
m_Alignment: 3
|
||||
m_AlignByGeometry: 0
|
||||
m_RichText: 0
|
||||
m_HorizontalOverflow: 1
|
||||
m_VerticalOverflow: 0
|
||||
m_LineSpacing: 1
|
||||
m_Text:
|
||||
7
Assets/Prefabs/INPTVariable.prefab.meta
Normal file
7
Assets/Prefabs/INPTVariable.prefab.meta
Normal file
|
|
@ -0,0 +1,7 @@
|
|||
fileFormatVersion: 2
|
||||
guid: e7b5d763b32e0e44494864207f0d5275
|
||||
PrefabImporter:
|
||||
externalObjects: {}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
8
Assets/Scenes.meta
Normal file
8
Assets/Scenes.meta
Normal file
|
|
@ -0,0 +1,8 @@
|
|||
fileFormatVersion: 2
|
||||
guid: 4eb5aeac669694c45af64b5730fe82ca
|
||||
folderAsset: yes
|
||||
DefaultImporter:
|
||||
externalObjects: {}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
6374
Assets/Scenes/SampleScene.unity
Normal file
6374
Assets/Scenes/SampleScene.unity
Normal file
File diff suppressed because it is too large
Load diff
7
Assets/Scenes/SampleScene.unity.meta
Normal file
7
Assets/Scenes/SampleScene.unity.meta
Normal file
|
|
@ -0,0 +1,7 @@
|
|||
fileFormatVersion: 2
|
||||
guid: 9fc0d4010bbf28b4594072e72b8655ab
|
||||
DefaultImporter:
|
||||
externalObjects: {}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
8
Assets/Scripts.meta
Normal file
8
Assets/Scripts.meta
Normal file
|
|
@ -0,0 +1,8 @@
|
|||
fileFormatVersion: 2
|
||||
guid: 45eed84873cca754480c39540b8f1e50
|
||||
folderAsset: yes
|
||||
DefaultImporter:
|
||||
externalObjects: {}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
539
Assets/Scripts/DecimalMath.cs
Normal file
539
Assets/Scripts/DecimalMath.cs
Normal file
|
|
@ -0,0 +1,539 @@
|
|||
using System;
|
||||
|
||||
/// <summary>
|
||||
/// Analogy of Syatem.Math class for decimal types
|
||||
/// </summary>
|
||||
public static class DecimalMath
|
||||
{
|
||||
/// <summary>
|
||||
/// represents PI
|
||||
/// </summary>
|
||||
public const decimal PI = 3.14159265358979323846264338327950288419716939937510M;
|
||||
|
||||
/// <summary>
|
||||
/// represents PI
|
||||
/// </summary>
|
||||
public const decimal Epsilon = 0.0000000000000000001M;
|
||||
|
||||
/// <summary>
|
||||
/// represents 2*PI
|
||||
/// </summary>
|
||||
private const decimal PIx2 = 6.28318530717958647692528676655900576839433879875021M;
|
||||
|
||||
/// <summary>
|
||||
/// represents E
|
||||
/// </summary>
|
||||
public const decimal E = 2.7182818284590452353602874713526624977572470936999595749M;
|
||||
|
||||
/// <summary>
|
||||
/// represents PI/2
|
||||
/// </summary>
|
||||
private const decimal PIdiv2 = 1.570796326794896619231321691639751442098584699687552910487M;
|
||||
|
||||
/// <summary>
|
||||
/// represents PI/4
|
||||
/// </summary>
|
||||
private const decimal PIdiv4 = 0.785398163397448309615660845819875721049292349843776455243M;
|
||||
|
||||
/// <summary>
|
||||
/// represents 1.0/E
|
||||
/// </summary>
|
||||
private const decimal Einv = 0.3678794411714423215955237701614608674458111310317678M;
|
||||
|
||||
/// <summary>
|
||||
/// log(10,E) factor
|
||||
/// </summary>
|
||||
private const decimal Log10Inv = 0.434294481903251827651128918916605082294397005803666566114M;
|
||||
|
||||
/// <summary>
|
||||
/// Zero
|
||||
/// </summary>
|
||||
public const decimal Zero = 0.0M;
|
||||
|
||||
/// <summary>
|
||||
/// One
|
||||
/// </summary>
|
||||
public const decimal One = 1.0M;
|
||||
|
||||
/// <summary>
|
||||
/// Represents 0.5M
|
||||
/// </summary>
|
||||
private const decimal Half = 0.5M;
|
||||
|
||||
/// <summary>
|
||||
/// Max iterations count in Taylor series
|
||||
/// </summary>
|
||||
private const int MaxIteration = 100;
|
||||
|
||||
/// <summary>
|
||||
/// Analogy of Math.Exp method
|
||||
/// </summary>
|
||||
/// <param name="x"></param>
|
||||
/// <returns></returns>
|
||||
public static decimal Exp(decimal x)
|
||||
{
|
||||
var count = 0;
|
||||
|
||||
if (x > One)
|
||||
{
|
||||
count = decimal.ToInt32(decimal.Truncate(x));
|
||||
x -= decimal.Truncate(x);
|
||||
}
|
||||
|
||||
if (x < Zero)
|
||||
{
|
||||
count = decimal.ToInt32(decimal.Truncate(x) - 1);
|
||||
x = One + (x - decimal.Truncate(x));
|
||||
}
|
||||
|
||||
var iteration = 1;
|
||||
var result = One;
|
||||
var factorial = One;
|
||||
decimal cachedResult;
|
||||
do
|
||||
{
|
||||
cachedResult = result;
|
||||
factorial *= x / iteration++;
|
||||
result += factorial;
|
||||
} while (cachedResult != result);
|
||||
|
||||
if (count == 0)
|
||||
return result;
|
||||
return result * PowerN(E, count);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Analogy of Math.Pow method
|
||||
/// </summary>
|
||||
/// <param name="value"></param>
|
||||
/// <param name="pow"></param>
|
||||
/// <returns></returns>
|
||||
public static decimal Pow(decimal value, decimal pow)
|
||||
{
|
||||
if (pow == Zero) return One;
|
||||
if (pow == One) return value;
|
||||
if (value == One) return One;
|
||||
|
||||
if (value == Zero)
|
||||
{
|
||||
if (pow > Zero)
|
||||
{
|
||||
return Zero;
|
||||
}
|
||||
|
||||
throw new Exception("Invalid Operation: zero base and negative power");
|
||||
}
|
||||
|
||||
if (pow == -One) return One / value;
|
||||
|
||||
var isPowerInteger = IsInteger(pow);
|
||||
if (value < Zero && !isPowerInteger)
|
||||
{
|
||||
throw new Exception("Invalid Operation: negative base and non-integer power");
|
||||
}
|
||||
|
||||
if (isPowerInteger && value > Zero)
|
||||
{
|
||||
int powerInt = (int)pow;
|
||||
return PowerN(value, powerInt);
|
||||
}
|
||||
|
||||
if (isPowerInteger && value < Zero)
|
||||
{
|
||||
int powerInt = (int)pow;
|
||||
if (powerInt % 2 == 0)
|
||||
{
|
||||
return Exp(pow * Log(-value));
|
||||
}
|
||||
|
||||
return -Exp(pow * Log(-value));
|
||||
}
|
||||
|
||||
return Exp(pow * Log(value));
|
||||
}
|
||||
|
||||
private static bool IsInteger(decimal value)
|
||||
{
|
||||
var longValue = (long)value;
|
||||
return Abs(value - longValue) <= Epsilon;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Power to the integer value
|
||||
/// </summary>
|
||||
/// <param name="value"></param>
|
||||
/// <param name="power"></param>
|
||||
/// <returns></returns>
|
||||
public static decimal PowerN(decimal value, int power)
|
||||
{
|
||||
while (true)
|
||||
{
|
||||
if (power == Zero) return One;
|
||||
if (power < Zero)
|
||||
{
|
||||
value = One / value;
|
||||
power = -power;
|
||||
continue;
|
||||
}
|
||||
|
||||
var q = power;
|
||||
var prod = One;
|
||||
var current = value;
|
||||
while (q > 0)
|
||||
{
|
||||
if (q % 2 == 1)
|
||||
{
|
||||
// detects the 1s in the binary expression of power
|
||||
prod = current * prod; // picks up the relevant power
|
||||
q--;
|
||||
}
|
||||
|
||||
current *= current; // value^i -> value^(2*i)
|
||||
q >>= 1;
|
||||
}
|
||||
|
||||
return prod;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Analogy of Math.Log10
|
||||
/// </summary>
|
||||
/// <param name="x"></param>
|
||||
/// <returns></returns>
|
||||
public static decimal Log10(decimal x)
|
||||
{
|
||||
return Log(x) * Log10Inv;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Analogy of Math.Log
|
||||
/// </summary>
|
||||
/// <param name="x"></param>
|
||||
/// <returns></returns>
|
||||
public static decimal Log(decimal x)
|
||||
{
|
||||
if (x <= Zero)
|
||||
{
|
||||
throw new ArgumentException("x must be greater than zero");
|
||||
}
|
||||
var count = 0;
|
||||
while (x >= One)
|
||||
{
|
||||
x *= Einv;
|
||||
count++;
|
||||
}
|
||||
while (x <= Einv)
|
||||
{
|
||||
x *= E;
|
||||
count--;
|
||||
}
|
||||
x--;
|
||||
if (x == Zero) return count;
|
||||
var result = Zero;
|
||||
var iteration = 0;
|
||||
var y = One;
|
||||
var cacheResult = result - One;
|
||||
while (cacheResult != result && iteration < MaxIteration)
|
||||
{
|
||||
iteration++;
|
||||
cacheResult = result;
|
||||
y *= -x;
|
||||
result += y / iteration;
|
||||
}
|
||||
return count - result;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Analogy of Math.Cos
|
||||
/// </summary>
|
||||
/// <param name="x"></param>
|
||||
/// <returns></returns>
|
||||
public static decimal Cos(decimal x)
|
||||
{
|
||||
//truncating to [-2*PI;2*PI]
|
||||
TruncateToPeriodicInterval(ref x);
|
||||
|
||||
// now x in (-2pi,2pi)
|
||||
if (x >= PI && x <= PIx2)
|
||||
{
|
||||
return -Cos(x - PI);
|
||||
}
|
||||
if (x >= -PIx2 && x <= -PI)
|
||||
{
|
||||
return -Cos(x + PI);
|
||||
}
|
||||
|
||||
x *= x;
|
||||
//y=1-x/2!+x^2/4!-x^3/6!...
|
||||
var xx = -x * Half;
|
||||
var y = One + xx;
|
||||
var cachedY = y - One;//init cache with different value
|
||||
for (var i = 1; cachedY != y && i < MaxIteration; i++)
|
||||
{
|
||||
cachedY = y;
|
||||
decimal factor = i * ((i << 1) + 3) + 1; //2i^2+2i+i+1=2i^2+3i+1
|
||||
factor = -Half / factor;
|
||||
xx *= x * factor;
|
||||
y += xx;
|
||||
}
|
||||
return y;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Analogy of Math.Tan
|
||||
/// </summary>
|
||||
/// <param name="x"></param>
|
||||
/// <returns></returns>
|
||||
public static decimal Tan(decimal x)
|
||||
{
|
||||
var cos = Cos(x);
|
||||
if (cos == Zero) throw new ArgumentException(nameof(x));
|
||||
//calculate sin using cos
|
||||
var sin = CalculateSinFromCos(x, cos);
|
||||
return sin / cos;
|
||||
}
|
||||
/// <summary>
|
||||
/// Helper function for calculating sin(x) from cos(x)
|
||||
/// </summary>
|
||||
/// <param name="x"></param>
|
||||
/// <param name="cos"></param>
|
||||
/// <returns></returns>
|
||||
private static decimal CalculateSinFromCos(decimal x, decimal cos)
|
||||
{
|
||||
var moduleOfSin = Sqrt(One - (cos * cos));
|
||||
var sineIsPositive = IsSignOfSinePositive(x);
|
||||
if (sineIsPositive) return moduleOfSin;
|
||||
return -moduleOfSin;
|
||||
}
|
||||
/// <summary>
|
||||
/// Analogy of Math.Sin
|
||||
/// </summary>
|
||||
/// <param name="x"></param>
|
||||
/// <returns></returns>
|
||||
public static decimal Sin(decimal x)
|
||||
{
|
||||
var cos = Cos(x);
|
||||
return CalculateSinFromCos(x, cos);
|
||||
}
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// Truncates to [-2*PI;2*PI]
|
||||
/// </summary>
|
||||
/// <param name="x"></param>
|
||||
private static void TruncateToPeriodicInterval(ref decimal x)
|
||||
{
|
||||
while (x >= PIx2)
|
||||
{
|
||||
var divide = Math.Abs(decimal.ToInt32(x / PIx2));
|
||||
x -= divide * PIx2;
|
||||
}
|
||||
|
||||
while (x <= -PIx2)
|
||||
{
|
||||
var divide = Math.Abs(decimal.ToInt32(x / PIx2));
|
||||
x += divide * PIx2;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
private static bool IsSignOfSinePositive(decimal x)
|
||||
{
|
||||
//truncating to [-2*PI;2*PI]
|
||||
TruncateToPeriodicInterval(ref x);
|
||||
|
||||
//now x in [-2*PI;2*PI]
|
||||
if (x >= -PIx2 && x <= -PI) return true;
|
||||
if (x >= -PI && x <= Zero) return false;
|
||||
if (x >= Zero && x <= PI) return true;
|
||||
if (x >= PI && x <= PIx2) return false;
|
||||
|
||||
//will not be reached
|
||||
throw new ArgumentException(nameof(x));
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Analogy of Math.Sqrt
|
||||
/// </summary>
|
||||
/// <param name="x"></param>
|
||||
/// <param name="epsilon">lasts iteration while error less than this epsilon</param>
|
||||
/// <returns></returns>
|
||||
public static decimal Sqrt(decimal x, decimal epsilon = Zero)
|
||||
{
|
||||
if (x < Zero) throw new OverflowException("Cannot calculate square root from a negative number");
|
||||
//initial approximation
|
||||
decimal current = (decimal)Math.Sqrt((double)x), previous;
|
||||
do
|
||||
{
|
||||
previous = current;
|
||||
if (previous == Zero) return Zero;
|
||||
current = (previous + x / previous) * Half;
|
||||
} while (Abs(previous - current) > epsilon);
|
||||
return current;
|
||||
}
|
||||
/// <summary>
|
||||
/// Analogy of Math.Sinh
|
||||
/// </summary>
|
||||
/// <param name="x"></param>
|
||||
/// <returns></returns>
|
||||
public static decimal Sinh(decimal x)
|
||||
{
|
||||
var y = Exp(x);
|
||||
var yy = One / y;
|
||||
return (y - yy) * Half;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Analogy of Math.Cosh
|
||||
/// </summary>
|
||||
/// <param name="x"></param>
|
||||
/// <returns></returns>
|
||||
public static decimal Cosh(decimal x)
|
||||
{
|
||||
var y = Exp(x);
|
||||
var yy = One / y;
|
||||
return (y + yy) * Half;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Analogy of Math.Sign
|
||||
/// </summary>
|
||||
/// <param name="x"></param>
|
||||
/// <returns></returns>
|
||||
public static int Sign(decimal x)
|
||||
{
|
||||
return x < Zero ? -1 : (x > Zero ? 1 : 0);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Analogy of Math.Tanh
|
||||
/// </summary>
|
||||
/// <param name="x"></param>
|
||||
/// <returns></returns>
|
||||
public static decimal Tanh(decimal x)
|
||||
{
|
||||
var y = Exp(x);
|
||||
var yy = One / y;
|
||||
return (y - yy) / (y + yy);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Analogy of Math.Abs
|
||||
/// </summary>
|
||||
/// <param name="x"></param>
|
||||
/// <returns></returns>
|
||||
public static decimal Abs(decimal x)
|
||||
{
|
||||
if (x <= Zero)
|
||||
{
|
||||
return -x;
|
||||
}
|
||||
return x;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Analogy of Math.Asin
|
||||
/// </summary>
|
||||
/// <param name="x"></param>
|
||||
/// <returns></returns>
|
||||
public static decimal Asin(decimal x)
|
||||
{
|
||||
if (x > One || x < -One)
|
||||
{
|
||||
throw new ArgumentException("x must be in [-1,1]");
|
||||
}
|
||||
//known values
|
||||
if (x == Zero) return Zero;
|
||||
if (x == One) return PIdiv2;
|
||||
//asin function is odd function
|
||||
if (x < Zero) return -Asin(-x);
|
||||
|
||||
//my optimize trick here
|
||||
|
||||
// used a math formula to speed up :
|
||||
// asin(x)=0.5*(pi/2-asin(1-2*x*x))
|
||||
// if x>=0 is true
|
||||
|
||||
var newX = One - 2 * x * x;
|
||||
|
||||
//for calculating new value near to zero than current
|
||||
//because we gain more speed with values near to zero
|
||||
if (Abs(x) > Abs(newX))
|
||||
{
|
||||
var t = Asin(newX);
|
||||
return Half * (PIdiv2 - t);
|
||||
}
|
||||
var y = Zero;
|
||||
var result = x;
|
||||
decimal cachedResult;
|
||||
var i = 1;
|
||||
y += result;
|
||||
var xx = x * x;
|
||||
do
|
||||
{
|
||||
cachedResult = result;
|
||||
result *= xx * (One - Half / (i));
|
||||
y += result / ((i << 1) + 1);
|
||||
i++;
|
||||
} while (cachedResult != result);
|
||||
return y;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Analogy of Math.Atan
|
||||
/// </summary>
|
||||
/// <param name="x"></param>
|
||||
/// <returns></returns>
|
||||
public static decimal Atan(decimal x)
|
||||
{
|
||||
if (x == Zero) return Zero;
|
||||
if (x == One) return PIdiv4;
|
||||
return Asin(x / Sqrt(One + x * x));
|
||||
}
|
||||
/// <summary>
|
||||
/// Analogy of Math.Acos
|
||||
/// </summary>
|
||||
/// <param name="x"></param>
|
||||
/// <returns></returns>
|
||||
public static decimal Acos(decimal x)
|
||||
{
|
||||
if (x == Zero) return PIdiv2;
|
||||
if (x == One) return Zero;
|
||||
if (x < Zero) return PI - Acos(-x);
|
||||
return PIdiv2 - Asin(x);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Analogy of Math.Atan2
|
||||
/// for more see this
|
||||
/// <seealso cref="http://i.imgur.com/TRLjs8R.png"/>
|
||||
/// </summary>
|
||||
/// <param name="y"></param>
|
||||
/// <param name="x"></param>
|
||||
/// <returns></returns>
|
||||
public static decimal Atan2(decimal y, decimal x)
|
||||
{
|
||||
if (x > Zero)
|
||||
{
|
||||
return Atan(y / x);
|
||||
}
|
||||
if (x < Zero && y >= Zero)
|
||||
{
|
||||
return Atan(y / x) + PI;
|
||||
}
|
||||
if (x < Zero && y < Zero)
|
||||
{
|
||||
return Atan(y / x) - PI;
|
||||
}
|
||||
if (x == Zero && y > Zero)
|
||||
{
|
||||
return PIdiv2;
|
||||
}
|
||||
if (x == Zero && y < Zero)
|
||||
{
|
||||
return -PIdiv2;
|
||||
}
|
||||
throw new ArgumentException("invalid atan2 arguments");
|
||||
}
|
||||
}
|
||||
11
Assets/Scripts/DecimalMath.cs.meta
Normal file
11
Assets/Scripts/DecimalMath.cs.meta
Normal file
|
|
@ -0,0 +1,11 @@
|
|||
fileFormatVersion: 2
|
||||
guid: bcc23a5940935d5449776ed5bdf22e57
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
720
Assets/Scripts/Function.cs
Normal file
720
Assets/Scripts/Function.cs
Normal file
|
|
@ -0,0 +1,720 @@
|
|||
using System;
|
||||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// </summary>
|
||||
///
|
||||
public enum Type { Variable, Value, UnaryOperator, Operator, Function, Result, Bracket, Comma, Error }
|
||||
public struct Symbol
|
||||
{
|
||||
public string m_name;
|
||||
public decimal m_value;
|
||||
public Type m_type;
|
||||
public override string ToString()
|
||||
{
|
||||
return m_name;
|
||||
}
|
||||
}
|
||||
public delegate Symbol EvaluateFunctionDelegate(string name, params Object[] args);
|
||||
public class Function
|
||||
{
|
||||
public decimal Result
|
||||
{
|
||||
get
|
||||
{
|
||||
return m_result;
|
||||
}
|
||||
}
|
||||
|
||||
public ArrayList Equation
|
||||
{
|
||||
get
|
||||
{
|
||||
return (ArrayList)m_equation.Clone();
|
||||
}
|
||||
}
|
||||
public ArrayList Postfix
|
||||
{
|
||||
get
|
||||
{
|
||||
return (ArrayList)m_postfix.Clone();
|
||||
}
|
||||
}
|
||||
|
||||
public EvaluateFunctionDelegate DefaultFunctionEvaluation
|
||||
{
|
||||
set
|
||||
{
|
||||
m_defaultFunctionEvaluation = value;
|
||||
}
|
||||
}
|
||||
|
||||
public bool Error
|
||||
{
|
||||
get
|
||||
{
|
||||
return m_bError;
|
||||
}
|
||||
}
|
||||
|
||||
public string ErrorDescription
|
||||
{
|
||||
get
|
||||
{
|
||||
return m_sErrorDescription;
|
||||
}
|
||||
}
|
||||
|
||||
public ArrayList Variables
|
||||
{
|
||||
get
|
||||
{
|
||||
ArrayList var = new ArrayList();
|
||||
foreach (Symbol sym in m_equation)
|
||||
{
|
||||
if ((sym.m_type == Type.Variable) && (!var.Contains(sym)))
|
||||
var.Add(sym);
|
||||
}
|
||||
return var;
|
||||
}
|
||||
set
|
||||
{
|
||||
foreach (Symbol sym in value)
|
||||
{
|
||||
for (int i = 0; i < m_postfix.Count; i++)
|
||||
{
|
||||
if ((sym.m_name == ((Symbol)m_postfix[i]).m_name) && (((Symbol)m_postfix[i]).m_type == Type.Variable))
|
||||
{
|
||||
Symbol sym1 = (Symbol)m_postfix[i];
|
||||
sym1.m_value = sym.m_value;
|
||||
m_postfix[i] = sym1;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public Function()
|
||||
{ }
|
||||
|
||||
public void Parse(string equation)
|
||||
{
|
||||
int state = 1;
|
||||
string temp = "";
|
||||
Symbol ctSymbol;
|
||||
|
||||
m_bError = false;
|
||||
m_sErrorDescription = "None";
|
||||
|
||||
m_equation.Clear();
|
||||
m_postfix.Clear();
|
||||
|
||||
int nPos = 0;
|
||||
//-- Remove all white spaces from the equation string --
|
||||
equation = equation.Trim();
|
||||
while ((nPos = equation.IndexOf(' ')) != -1)
|
||||
equation = equation.Remove(nPos, 1);
|
||||
|
||||
for (int i = 0; i < equation.Length; i++)
|
||||
{
|
||||
switch (state)
|
||||
{
|
||||
case 1:
|
||||
if (Char.IsNumber(equation[i]))
|
||||
{
|
||||
state = 2;
|
||||
temp += equation[i];
|
||||
}
|
||||
else if (Char.IsLetter(equation[i]))
|
||||
{
|
||||
state = 3;
|
||||
temp += equation[i];
|
||||
}
|
||||
else
|
||||
{
|
||||
ctSymbol.m_name = equation[i].ToString();
|
||||
ctSymbol.m_value = 0;
|
||||
switch (ctSymbol.m_name)
|
||||
{
|
||||
case ",":
|
||||
case ".":
|
||||
ctSymbol.m_type = Type.Comma;
|
||||
break;
|
||||
case "(":
|
||||
case ")":
|
||||
case "[":
|
||||
case "]":
|
||||
case "{":
|
||||
case "}":
|
||||
ctSymbol.m_type = Type.Bracket;
|
||||
break;
|
||||
default:
|
||||
if (m_equation.Count == 0)
|
||||
ctSymbol.m_type = Type.UnaryOperator;
|
||||
else if ((((Symbol)m_equation[m_equation.Count - 1]).m_type == Type.Value) || (((Symbol)m_equation[m_equation.Count - 1]).m_type == Type.Variable)
|
||||
|| (((Symbol)m_equation[m_equation.Count - 1]).m_name == ")") || (((Symbol)m_equation[m_equation.Count - 1]).m_name == "]") || (((Symbol)m_equation[m_equation.Count - 1]).m_name == "}"))
|
||||
ctSymbol.m_type = Type.Operator;
|
||||
else
|
||||
ctSymbol.m_type = Type.UnaryOperator;
|
||||
break;
|
||||
}
|
||||
m_equation.Add(ctSymbol);
|
||||
}
|
||||
break;
|
||||
case 2:
|
||||
if ((Char.IsNumber(equation[i])) || (equation[i] == '.') || (equation[i] == ','))
|
||||
temp += equation[i];
|
||||
else if (!Char.IsLetter(equation[i]))
|
||||
{
|
||||
state = 1;
|
||||
ctSymbol.m_name = temp;
|
||||
ctSymbol.m_value = Decimal.Parse(temp);
|
||||
ctSymbol.m_type = Type.Value;
|
||||
m_equation.Add(ctSymbol);
|
||||
ctSymbol.m_name = equation[i].ToString();
|
||||
ctSymbol.m_value = 0;
|
||||
switch (ctSymbol.m_name)
|
||||
{
|
||||
case ",":
|
||||
ctSymbol.m_type = Type.Comma;
|
||||
break;
|
||||
case "(":
|
||||
case ")":
|
||||
case "[":
|
||||
case "]":
|
||||
case "{":
|
||||
case "}":
|
||||
ctSymbol.m_type = Type.Bracket;
|
||||
break;
|
||||
default:
|
||||
if (m_equation.Count == 0)
|
||||
ctSymbol.m_type = Type.UnaryOperator;
|
||||
else if ((((Symbol)m_equation[m_equation.Count - 1]).m_type == Type.Value) || (((Symbol)m_equation[m_equation.Count - 1]).m_type == Type.Variable)
|
||||
|| (((Symbol)m_equation[m_equation.Count - 1]).m_name == ")") || (((Symbol)m_equation[m_equation.Count - 1]).m_name == "]") || (((Symbol)m_equation[m_equation.Count - 1]).m_name == "}"))
|
||||
ctSymbol.m_type = Type.Operator;
|
||||
else
|
||||
ctSymbol.m_type = Type.UnaryOperator;
|
||||
break;
|
||||
}
|
||||
m_equation.Add(ctSymbol);
|
||||
temp = "";
|
||||
}
|
||||
break;
|
||||
case 3:
|
||||
if (Char.IsLetterOrDigit(equation[i]))
|
||||
temp += equation[i];
|
||||
else
|
||||
{
|
||||
state = 1;
|
||||
ctSymbol.m_name = temp;
|
||||
ctSymbol.m_value = 0;
|
||||
if (equation[i] == '(')
|
||||
ctSymbol.m_type = Type.Function;
|
||||
else
|
||||
{
|
||||
if (ctSymbol.m_name == "pi")
|
||||
ctSymbol.m_value = DecimalMath.PI;
|
||||
else if (ctSymbol.m_name == "e")
|
||||
ctSymbol.m_value = DecimalMath.E;
|
||||
ctSymbol.m_type = Type.Variable;
|
||||
}
|
||||
m_equation.Add(ctSymbol);
|
||||
ctSymbol.m_name = equation[i].ToString();
|
||||
ctSymbol.m_value = 0;
|
||||
switch (ctSymbol.m_name)
|
||||
{
|
||||
case ",":
|
||||
ctSymbol.m_type = Type.Comma;
|
||||
break;
|
||||
case "(":
|
||||
case ")":
|
||||
case "[":
|
||||
case "]":
|
||||
case "{":
|
||||
case "}":
|
||||
ctSymbol.m_type = Type.Bracket;
|
||||
break;
|
||||
default:
|
||||
if (m_equation.Count == 0)
|
||||
ctSymbol.m_type = Type.UnaryOperator;
|
||||
else if ((((Symbol)m_equation[m_equation.Count - 1]).m_type == Type.Value) || (((Symbol)m_equation[m_equation.Count - 1]).m_type == Type.Variable)
|
||||
|| (((Symbol)m_equation[m_equation.Count - 1]).m_name == ")") || (((Symbol)m_equation[m_equation.Count - 1]).m_name == "]") || (((Symbol)m_equation[m_equation.Count - 1]).m_name == "}"))
|
||||
ctSymbol.m_type = Type.Operator;
|
||||
else
|
||||
ctSymbol.m_type = Type.UnaryOperator;
|
||||
break;
|
||||
}
|
||||
m_equation.Add(ctSymbol);
|
||||
temp = "";
|
||||
}
|
||||
break;
|
||||
}
|
||||
}
|
||||
if (temp != "")
|
||||
{
|
||||
ctSymbol.m_name = temp;
|
||||
if (state == 2)
|
||||
{
|
||||
ctSymbol.m_value = Decimal.Parse(temp);
|
||||
ctSymbol.m_type = Type.Value;
|
||||
}
|
||||
else
|
||||
{
|
||||
if (ctSymbol.m_name == "pi")
|
||||
ctSymbol.m_value = DecimalMath.PI;
|
||||
else if (ctSymbol.m_name == "e")
|
||||
ctSymbol.m_value = DecimalMath.E;
|
||||
else
|
||||
ctSymbol.m_value = 0;
|
||||
ctSymbol.m_type = Type.Variable;
|
||||
}
|
||||
m_equation.Add(ctSymbol);
|
||||
}
|
||||
}
|
||||
|
||||
public void Infix2Postfix()
|
||||
{
|
||||
Symbol tpSym;
|
||||
Stack tpStack = new Stack();
|
||||
foreach (Symbol sym in m_equation)
|
||||
{
|
||||
if ((sym.m_type == Type.Value) || (sym.m_type == Type.Variable))
|
||||
m_postfix.Add(sym);
|
||||
else if ((sym.m_name == "(") || (sym.m_name == "[") || (sym.m_name == "{"))
|
||||
tpStack.Push(sym);
|
||||
else if ((sym.m_name == ")") || (sym.m_name == "]") || (sym.m_name == "}"))
|
||||
{
|
||||
if (tpStack.Count > 0)
|
||||
{
|
||||
tpSym = (Symbol)tpStack.Pop();
|
||||
while ((tpSym.m_name != "(") && (tpSym.m_name != "[") && (tpSym.m_name != "{"))
|
||||
{
|
||||
m_postfix.Add(tpSym);
|
||||
tpSym = (Symbol)tpStack.Pop();
|
||||
}
|
||||
}
|
||||
}
|
||||
else if (sym.m_type == Type.UnaryOperator)
|
||||
{
|
||||
tpStack.Push(sym);
|
||||
}
|
||||
else
|
||||
{
|
||||
if (tpStack.Count > 0)
|
||||
{
|
||||
tpSym = (Symbol)tpStack.Pop();
|
||||
while ((tpStack.Count != 0) && ((tpSym.m_type == Type.UnaryOperator) || (tpSym.m_type == Type.Operator) || (tpSym.m_type == Type.Function) || (tpSym.m_type == Type.Comma)) && (Precedence(tpSym) >= Precedence(sym)))
|
||||
{
|
||||
m_postfix.Add(tpSym);
|
||||
tpSym = (Symbol)tpStack.Pop();
|
||||
}
|
||||
if (((tpSym.m_type == Type.UnaryOperator) || (tpSym.m_type == Type.Operator) || (tpSym.m_type == Type.Function) || (tpSym.m_type == Type.Comma)) && (Precedence(tpSym) >= Precedence(sym)))
|
||||
m_postfix.Add(tpSym);
|
||||
else
|
||||
tpStack.Push(tpSym);
|
||||
}
|
||||
tpStack.Push(sym);
|
||||
}
|
||||
}
|
||||
while (tpStack.Count > 0)
|
||||
{
|
||||
tpSym = (Symbol)tpStack.Pop();
|
||||
m_postfix.Add(tpSym);
|
||||
}
|
||||
}
|
||||
|
||||
public void EvaluatePostfix()
|
||||
{
|
||||
Symbol tpSym1, tpSym2, tpResult;
|
||||
Stack tpStack = new Stack();
|
||||
ArrayList fnParam = new ArrayList();
|
||||
m_bError = false;
|
||||
foreach (Symbol sym in m_postfix)
|
||||
{
|
||||
if ((sym.m_type == Type.Value) || (sym.m_type == Type.Variable) || (sym.m_type == Type.Result))
|
||||
tpStack.Push(sym);
|
||||
else if (sym.m_type == Type.UnaryOperator)
|
||||
{
|
||||
tpSym1 = (Symbol)tpStack.Pop();
|
||||
tpResult = Evaluate(sym, tpSym1);
|
||||
if (tpResult.m_type == Type.Error)
|
||||
{
|
||||
m_bError = true;
|
||||
m_sErrorDescription = tpResult.m_name;
|
||||
return;
|
||||
}
|
||||
tpStack.Push(tpResult);
|
||||
}
|
||||
else if (sym.m_type == Type.Operator)
|
||||
{
|
||||
tpSym1 = (Symbol)tpStack.Pop();
|
||||
tpSym2 = (Symbol)tpStack.Pop();
|
||||
tpResult = Evaluate(tpSym2, sym, tpSym1);
|
||||
if (tpResult.m_type == Type.Error)
|
||||
{
|
||||
m_bError = true;
|
||||
m_sErrorDescription = tpResult.m_name;
|
||||
return;
|
||||
}
|
||||
tpStack.Push(tpResult);
|
||||
}
|
||||
else if (sym.m_type == Type.Comma)
|
||||
{
|
||||
tpStack.Push(sym);
|
||||
}
|
||||
else if (sym.m_type == Type.Function)
|
||||
{
|
||||
fnParam.Clear();
|
||||
tpSym1 = (Symbol)tpStack.Pop();
|
||||
if ((tpSym1.m_type == Type.Value) || (tpSym1.m_type == Type.Variable) || (tpSym1.m_type == Type.Result))
|
||||
{
|
||||
tpResult = EvaluateFunction(sym.m_name, tpSym1);
|
||||
if (tpResult.m_type == Type.Error)
|
||||
{
|
||||
m_bError = true;
|
||||
m_sErrorDescription = tpResult.m_name;
|
||||
return;
|
||||
}
|
||||
tpStack.Push(tpResult);
|
||||
}
|
||||
else if (tpSym1.m_type == Type.Comma)
|
||||
{
|
||||
while (tpSym1.m_type == Type.Comma)
|
||||
{
|
||||
tpSym1 = (Symbol)tpStack.Pop();
|
||||
fnParam.Add(tpSym1);
|
||||
tpSym1 = (Symbol)tpStack.Pop();
|
||||
}
|
||||
fnParam.Add(tpSym1);
|
||||
tpResult = EvaluateFunction(sym.m_name, fnParam.ToArray());
|
||||
if (tpResult.m_type == Type.Error)
|
||||
{
|
||||
m_bError = true;
|
||||
m_sErrorDescription = tpResult.m_name;
|
||||
return;
|
||||
}
|
||||
tpStack.Push(tpResult);
|
||||
}
|
||||
else
|
||||
{
|
||||
tpStack.Push(tpSym1);
|
||||
tpResult = EvaluateFunction(sym.m_name);
|
||||
if (tpResult.m_type == Type.Error)
|
||||
{
|
||||
m_bError = true;
|
||||
m_sErrorDescription = tpResult.m_name;
|
||||
return;
|
||||
}
|
||||
tpStack.Push(tpResult);
|
||||
}
|
||||
}
|
||||
}
|
||||
if (tpStack.Count == 1)
|
||||
{
|
||||
tpResult = (Symbol)tpStack.Pop();
|
||||
m_result = tpResult.m_value;
|
||||
}
|
||||
}
|
||||
|
||||
protected int Precedence(Symbol sym)
|
||||
{
|
||||
switch (sym.m_type)
|
||||
{
|
||||
case Type.Bracket:
|
||||
return 7;
|
||||
case Type.Function:
|
||||
return 5;
|
||||
case Type.UnaryOperator:
|
||||
return 4;
|
||||
case Type.Comma:
|
||||
return 0;
|
||||
}
|
||||
switch (sym.m_name)
|
||||
{
|
||||
case "^":
|
||||
return 3;
|
||||
case "/":
|
||||
case "*":
|
||||
case "%":
|
||||
return 2;
|
||||
case "+":
|
||||
case "-":
|
||||
return 1;
|
||||
}
|
||||
return -1;
|
||||
}
|
||||
|
||||
protected Symbol Evaluate(Symbol opr, Symbol sym1)
|
||||
{
|
||||
Symbol result;
|
||||
result.m_name = opr.m_name + sym1.m_name;
|
||||
result.m_type = Type.Result;
|
||||
result.m_value = 0;
|
||||
switch (opr.m_name)
|
||||
{
|
||||
case "+":
|
||||
result.m_value = sym1.m_value; ;
|
||||
break;
|
||||
case "-":
|
||||
result.m_value = -1 * sym1.m_value;
|
||||
break;
|
||||
default:
|
||||
result.m_type = Type.Error;
|
||||
result.m_name = "Undefine unary operator: " + opr.m_name + ".";
|
||||
break;
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
protected Symbol Evaluate(Symbol sym1, Symbol opr, Symbol sym2)
|
||||
{
|
||||
Symbol result;
|
||||
result.m_name = sym1.m_name + opr.m_name + sym2.m_name;
|
||||
result.m_type = Type.Result;
|
||||
result.m_value = 0;
|
||||
switch (opr.m_name)
|
||||
{
|
||||
case "^":
|
||||
result.m_value = DecimalMath.Pow(sym1.m_value, sym2.m_value);
|
||||
break;
|
||||
case "/":
|
||||
{
|
||||
if (sym2.m_value != 0)
|
||||
result.m_value = sym1.m_value / sym2.m_value;
|
||||
else
|
||||
{
|
||||
result.m_name = "Divide by Zero.";
|
||||
result.m_type = Type.Error;
|
||||
}
|
||||
break;
|
||||
}
|
||||
case "*":
|
||||
result.m_value = sym1.m_value * sym2.m_value;
|
||||
break;
|
||||
case "%":
|
||||
result.m_value = sym1.m_value % sym2.m_value;
|
||||
break;
|
||||
case "+":
|
||||
result.m_value = sym1.m_value + sym2.m_value;
|
||||
break;
|
||||
case "-":
|
||||
result.m_value = sym1.m_value - sym2.m_value;
|
||||
break;
|
||||
default:
|
||||
result.m_type = Type.Error;
|
||||
result.m_name = "Undefine operator: " + opr.m_name + ".";
|
||||
break;
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
protected Symbol EvaluateFunction(string name, params Object[] args)
|
||||
{
|
||||
Symbol result;
|
||||
result.m_name = "";
|
||||
result.m_type = Type.Result;
|
||||
result.m_value = 0;
|
||||
switch (name)
|
||||
{
|
||||
case "cos":
|
||||
if (args.Length == 1)
|
||||
{
|
||||
result.m_name = name + "(" + ((Symbol)args[0]).m_value.ToString() + ")";
|
||||
result.m_value = DecimalMath.Cos(((Symbol)args[0]).m_value * DecimalMath.PI / 180);
|
||||
}
|
||||
else
|
||||
{
|
||||
result.m_name = "Invalid number of parameters in: " + name + ".";
|
||||
result.m_type = Type.Error;
|
||||
}
|
||||
break;
|
||||
case "sin":
|
||||
if (args.Length == 1)
|
||||
{
|
||||
result.m_name = name + "(" + ((Symbol)args[0]).m_value.ToString() + ")";
|
||||
result.m_value = DecimalMath.Sin(((Symbol)args[0]).m_value * DecimalMath.PI / 180);
|
||||
}
|
||||
else
|
||||
{
|
||||
result.m_name = "Invalid number of parameters in: " + name + ".";
|
||||
result.m_type = Type.Error;
|
||||
}
|
||||
break;
|
||||
case "tan":
|
||||
if (args.Length == 1)
|
||||
{
|
||||
result.m_name = name + "(" + ((Symbol)args[0]).m_value.ToString() + ")";
|
||||
result.m_value = DecimalMath.Tan(((Symbol)args[0]).m_value * DecimalMath.PI / 180);
|
||||
}
|
||||
else
|
||||
{
|
||||
result.m_name = "Invalid number of parameters in: " + name + ".";
|
||||
result.m_type = Type.Error;
|
||||
}
|
||||
break;
|
||||
case "cosh":
|
||||
if (args.Length == 1)
|
||||
{
|
||||
result.m_name = name + "(" + ((Symbol)args[0]).m_value.ToString() + ")";
|
||||
result.m_value = DecimalMath.Cosh(((Symbol)args[0]).m_value);
|
||||
}
|
||||
else
|
||||
{
|
||||
result.m_name = "Invalid number of parameters in: " + name + ".";
|
||||
result.m_type = Type.Error;
|
||||
}
|
||||
break;
|
||||
case "sinh":
|
||||
if (args.Length == 1)
|
||||
{
|
||||
result.m_name = name + "(" + ((Symbol)args[0]).m_value.ToString() + ")";
|
||||
result.m_value = DecimalMath.Sinh(((Symbol)args[0]).m_value);
|
||||
}
|
||||
else
|
||||
{
|
||||
result.m_name = "Invalid number of parameters in: " + name + ".";
|
||||
result.m_type = Type.Error;
|
||||
}
|
||||
break;
|
||||
case "tanh":
|
||||
if (args.Length == 1)
|
||||
{
|
||||
result.m_name = name + "(" + ((Symbol)args[0]).m_value.ToString() + ")";
|
||||
result.m_value = DecimalMath.Tanh(((Symbol)args[0]).m_value);
|
||||
}
|
||||
else
|
||||
{
|
||||
result.m_name = "Invalid number of parameters in: " + name + ".";
|
||||
result.m_type = Type.Error;
|
||||
}
|
||||
break;
|
||||
case "log":
|
||||
if (args.Length == 1)
|
||||
{
|
||||
result.m_name = name + "(" + ((Symbol)args[0]).m_value.ToString() + ")";
|
||||
result.m_value = DecimalMath.Log10(((Symbol)args[0]).m_value);
|
||||
}
|
||||
else
|
||||
{
|
||||
result.m_name = "Invalid number of parameters in: " + name + ".";
|
||||
result.m_type = Type.Error;
|
||||
}
|
||||
break;
|
||||
case "ln":
|
||||
if (args.Length == 1)
|
||||
{
|
||||
result.m_name = name + "(" + ((Symbol)args[0]).m_value.ToString() + ")";
|
||||
result.m_value = DecimalMath.Log(((Symbol)args[0]).m_value);
|
||||
}
|
||||
else
|
||||
{
|
||||
result.m_name = "Invalid number of parameters in: " + name + ".";
|
||||
result.m_type = Type.Error;
|
||||
}
|
||||
break;
|
||||
case "logn":
|
||||
if (args.Length == 2)
|
||||
{
|
||||
result.m_name = name + "(" + ((Symbol)args[0]).m_value.ToString() + "'" + ((Symbol)args[1]).m_value.ToString() + ")";
|
||||
result.m_value = DecimalMath.Log(((Symbol)args[0]).m_value);
|
||||
}
|
||||
else
|
||||
{
|
||||
result.m_name = "Invalid number of parameters in: " + name + ".";
|
||||
result.m_type = Type.Error;
|
||||
}
|
||||
break;
|
||||
case "sqrt":
|
||||
if (args.Length == 1)
|
||||
{
|
||||
result.m_name = name + "(" + ((Symbol)args[0]).m_value.ToString() + ")";
|
||||
result.m_value = DecimalMath.Sqrt(((Symbol)args[0]).m_value);
|
||||
}
|
||||
else
|
||||
{
|
||||
result.m_name = "Invalid number of parameters in: " + name + ".";
|
||||
result.m_type = Type.Error;
|
||||
}
|
||||
break;
|
||||
case "abs":
|
||||
if (args.Length == 1)
|
||||
{
|
||||
result.m_name = name + "(" + ((Symbol)args[0]).m_value.ToString() + ")";
|
||||
result.m_value = DecimalMath.Abs(((Symbol)args[0]).m_value);
|
||||
}
|
||||
else
|
||||
{
|
||||
result.m_name = "Invalid number of parameters in: " + name + ".";
|
||||
result.m_type = Type.Error;
|
||||
}
|
||||
break;
|
||||
case "acos":
|
||||
if (args.Length == 1)
|
||||
{
|
||||
result.m_name = name + "(" + ((Symbol)args[0]).m_value.ToString() + ")";
|
||||
result.m_value = DecimalMath.Acos(((Symbol)args[0]).m_value) * 180 / DecimalMath.PI;
|
||||
}
|
||||
else
|
||||
{
|
||||
result.m_name = "Invalid number of parameters in: " + name + ".";
|
||||
result.m_type = Type.Error;
|
||||
}
|
||||
break;
|
||||
case "asin":
|
||||
if (args.Length == 1)
|
||||
{
|
||||
result.m_name = name + "(" + ((Symbol)args[0]).m_value.ToString() + ")";
|
||||
result.m_value = DecimalMath.Asin(((Symbol)args[0]).m_value);
|
||||
}
|
||||
else
|
||||
{
|
||||
result.m_name = "Invalid number of parameters in: " + name + ".";
|
||||
result.m_type = Type.Error;
|
||||
}
|
||||
break;
|
||||
case "atan":
|
||||
if (args.Length == 1)
|
||||
{
|
||||
result.m_name = name + "(" + ((Symbol)args[0]).m_value.ToString() + ")";
|
||||
result.m_value = DecimalMath.Atan(((Symbol)args[0]).m_value) * 180 / DecimalMath.PI;
|
||||
}
|
||||
else
|
||||
{
|
||||
result.m_name = "Invalid number of parameters in: " + name + ".";
|
||||
result.m_type = Type.Error;
|
||||
}
|
||||
break;
|
||||
case "exp":
|
||||
if (args.Length == 1)
|
||||
{
|
||||
result.m_name = name + "(" + ((Symbol)args[0]).m_value.ToString() + ")";
|
||||
result.m_value = DecimalMath.Exp(((Symbol)args[0]).m_value);
|
||||
}
|
||||
else
|
||||
{
|
||||
result.m_name = "Invalid number of parameters in: " + name + ".";
|
||||
result.m_type = Type.Error;
|
||||
}
|
||||
break;
|
||||
default:
|
||||
if (m_defaultFunctionEvaluation != null)
|
||||
result = m_defaultFunctionEvaluation(name, args);
|
||||
else
|
||||
{
|
||||
result.m_name = "Function: " + name + ", not found.";
|
||||
result.m_type = Type.Error;
|
||||
}
|
||||
break;
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
protected bool m_bError = false;
|
||||
protected string m_sErrorDescription = "None";
|
||||
public decimal m_result = 0;
|
||||
public ArrayList m_equation = new ArrayList();
|
||||
public ArrayList m_postfix = new ArrayList();
|
||||
protected EvaluateFunctionDelegate m_defaultFunctionEvaluation;
|
||||
}
|
||||
11
Assets/Scripts/Function.cs.meta
Normal file
11
Assets/Scripts/Function.cs.meta
Normal file
|
|
@ -0,0 +1,11 @@
|
|||
fileFormatVersion: 2
|
||||
guid: 4624df48b44672048904abdf49a34331
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
171
Assets/Scripts/FunctionEvaluator.cs
Normal file
171
Assets/Scripts/FunctionEvaluator.cs
Normal file
|
|
@ -0,0 +1,171 @@
|
|||
using System;
|
||||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
using UnityEngine;
|
||||
|
||||
[System.Serializable]
|
||||
public class FunctionEvaluator : MonoBehaviour
|
||||
{
|
||||
public bool Check = false;
|
||||
public string MainEquation1, MainEquation2, MainEquation3, MainEquation4, MainEquation5;
|
||||
public int Count;
|
||||
public double var1;
|
||||
public int var2;
|
||||
|
||||
|
||||
public List<string> Equations = new List<string>(), VariableNames = new List<string>();
|
||||
public List<double> VariableInitialValues = new List<double>();
|
||||
|
||||
Dictionary<string, decimal> dict = new Dictionary<string, decimal>();
|
||||
|
||||
|
||||
private void Update()
|
||||
{
|
||||
if (Check)
|
||||
{
|
||||
Check = false;
|
||||
|
||||
//Debug.Log(NumericalMethods.Method_Derivative(MainEquation1, var1, "x", 0).ToString("0." + new string('#', 339)));
|
||||
//Debug.Log(NumericalMethods.Method_TaylorSeries(MainEquation1, 6, "x", 0));
|
||||
|
||||
dict.Clear();
|
||||
for (int i = 0; i < VariableNames.Count; i++)
|
||||
{
|
||||
dict.Add(VariableNames[i], (decimal)VariableInitialValues[i]);
|
||||
}
|
||||
|
||||
Debug.Log(DecimalMath.Cos(90));
|
||||
string res = NumericalMethods.Method_TaylorSeries(MainEquation1, Count, "x", 1, 0.01m).Replace(',', '.');
|
||||
Debug.Log(res);
|
||||
|
||||
|
||||
|
||||
//Debug.Log(NumericalMethods.Method_NumericalPartialDifferentiation("x^2+2*x*y", 2, "x", dict, 6, 0.01m));
|
||||
//Debug.Log(NumericalMethods.Method_NumericalDifferentiation("log(x)", 3, "x", 2, 2));
|
||||
|
||||
/*Dictionary<string, decimal> results = NumericalMethods.MethodMultiVar_SolveEquationSystem(Equations, dict, Count);
|
||||
|
||||
string sum = "";
|
||||
foreach (KeyValuePair<string, decimal> p in results)
|
||||
{
|
||||
sum += p.Key + ": " + p.Value + ", ";
|
||||
}
|
||||
Debug.Log(sum);*/
|
||||
|
||||
|
||||
|
||||
|
||||
/*string res = NumericalMethods.Method_TaylorSeries(MainEquation1, Count, "x", 0).Replace(',', '.');
|
||||
Debug.Log(res);*/
|
||||
|
||||
//Debug.Log(NumericalMethods.Method_Derivative(MainEquation1, Count, "x", (decimal)var1));
|
||||
|
||||
|
||||
//Debug.Log(NumericalMethods.Method_BisectionMethod(MainEquation1, "x", -4, -2, 20));
|
||||
|
||||
|
||||
|
||||
|
||||
/*Dictionary<string, float> root = NumericalMethods.MethodMultiVar_BisectionMethod(new List<string> { MainEquation1, MainEquation2 }, new List<string> { "x", "y" }, var1, var2, taylorCount);
|
||||
foreach (KeyValuePair<string, float> r in root)
|
||||
{
|
||||
Debug.Log(r.Key + ": " + r.Value);
|
||||
}*/
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
/*string str = NumericalMethods.GenerateCombinations("-+", "s", 3);
|
||||
Debug.Log(str);
|
||||
string[] test = str.Split('s');
|
||||
|
||||
string str2 = "";
|
||||
for (int i = 0; i < test.Length; i++)
|
||||
{
|
||||
str2 += test[i];
|
||||
}
|
||||
Debug.Log(str2);*/
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
/*string e1 = NumericalMethods.MethodMultiVar_TaylorSeries(MainEquation1, 1, dict);
|
||||
string e2 = NumericalMethods.MethodMultiVar_TaylorSeries(MainEquation2, 1, dict);
|
||||
string e3 = NumericalMethods.MethodMultiVar_TaylorSeries(MainEquation3, 1, dict);
|
||||
Debug.Log(e1 + " | " + e2 + " | " + e3);
|
||||
|
||||
float[][] tea = new float[][] { new float[] { 3, 7, -2 }, new float[] { -1, 1, 2 }, new float[] { 3, -2, -1 } };
|
||||
float[] leftv = new float[] { -5, 2, -1 };
|
||||
float[] res = Matrix.SolveEquation(tea, leftv);
|
||||
string sum = "";
|
||||
for (int i = 0; i < res.Length; i++) sum += res[i] + ", ";
|
||||
Debug.Log(sum);*/
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
/*Dictionary<string, float> testt = new Dictionary<string, float>();
|
||||
testt.Add("x", 1);
|
||||
testt.Add("y", 2);
|
||||
Debug.Log(NumericalMethods.Method_PartialDerivative("x^(3*y)*y^x", 1, "x", testt, 0.001f));*/
|
||||
|
||||
|
||||
|
||||
|
||||
/*List<string> LES = new List<string>();
|
||||
LES.Add(MainEquation1);
|
||||
LES.Add(MainEquation2);
|
||||
LES.Add(MainEquation3);
|
||||
List<string> VARS = new List<string>();
|
||||
VARS.Add("x");
|
||||
VARS.Add("y");
|
||||
VARS.Add("z");
|
||||
float[][] test = Matrix.ConvertLinearEquationsToMatrix(LES, VARS);
|
||||
|
||||
string deb = "";
|
||||
for (int i = 0; i < test.Length; i++)
|
||||
{
|
||||
for (int j = 0; j < test[i].Length; j++)
|
||||
{
|
||||
deb += test[i][j].ToString() + ", ";
|
||||
}
|
||||
deb += "\n";
|
||||
}
|
||||
Debug.Log(deb);*/
|
||||
|
||||
|
||||
|
||||
|
||||
//Debug.Log(NumericalMethods.MethodMultiVar_TaylorSeries(MainEquation2, 1, dict));
|
||||
|
||||
|
||||
|
||||
|
||||
//Debug.Log(NumericalMethods.Method_PartialDerivative(MainEquation, 1, "x", dict, 0.01f));
|
||||
|
||||
|
||||
|
||||
|
||||
/*Function f = new Function();
|
||||
f.Parse(MainEquation1);
|
||||
f.Infix2Postfix();
|
||||
f.EvaluatePostfix();
|
||||
Debug.Log(f.m_result);
|
||||
|
||||
|
||||
|
||||
|
||||
for (int i = 0; i < f.m_equation.Count; i++)
|
||||
{
|
||||
Debug.Log("EEEEE: " + f.m_equation[i].ToString());
|
||||
}
|
||||
for (int i = 0; i < f.m_postfix.Count; i++)
|
||||
{
|
||||
Debug.Log("PPPPP: " + f.m_postfix[i].ToString());
|
||||
}*/
|
||||
}
|
||||
}
|
||||
}
|
||||
11
Assets/Scripts/FunctionEvaluator.cs.meta
Normal file
11
Assets/Scripts/FunctionEvaluator.cs.meta
Normal file
|
|
@ -0,0 +1,11 @@
|
|||
fileFormatVersion: 2
|
||||
guid: d781ba518f05a794a879f2df79d6c6b8
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
279
Assets/Scripts/Matrix.cs
Normal file
279
Assets/Scripts/Matrix.cs
Normal file
|
|
@ -0,0 +1,279 @@
|
|||
using System;
|
||||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
using UnityEngine;
|
||||
|
||||
public class Matrix
|
||||
{
|
||||
public static decimal[][] GenerateRandomMatrix(int rows, int columns, decimal min, decimal max)
|
||||
{
|
||||
decimal[][] res = new decimal[rows][];
|
||||
for (int i = 0; i < rows; i++)
|
||||
{
|
||||
res[i] = new decimal[columns];
|
||||
for (int j = 0; j < columns; j++)
|
||||
{
|
||||
decimal value = (decimal)UnityEngine.Random.Range((float)min, (float)max);
|
||||
value = (decimal)System.Math.Round(value, 2);
|
||||
res[i][j] = value;
|
||||
}
|
||||
}
|
||||
return res;
|
||||
}
|
||||
|
||||
public static decimal[][] Transpoze(decimal[][] _m)
|
||||
{
|
||||
decimal[][] res = new decimal[_m[0].Length][];
|
||||
for (int i = 0; i < _m[0].Length; i++)
|
||||
{
|
||||
res[i] = new decimal[_m.Length];
|
||||
}
|
||||
for (int i = 0; i < _m.Length; i++)
|
||||
{
|
||||
for (int j = 0; j < _m[0].Length; j++)
|
||||
{
|
||||
res[j][i] = _m[i][j];
|
||||
}
|
||||
}
|
||||
return res;
|
||||
}
|
||||
|
||||
public static decimal MatrixDeterminant(decimal[][] _m)
|
||||
{
|
||||
if (_m.Length == _m[0].Length)
|
||||
{
|
||||
int c = _m.Length;
|
||||
if (c == 2)
|
||||
{
|
||||
return (decimal)Math.Round(_m[0][0] * _m[1][1] - _m[0][1] * _m[1][0], 5);
|
||||
}
|
||||
else if (c == 1)
|
||||
{
|
||||
return (decimal)Math.Round(_m[0][0], 5);
|
||||
}
|
||||
else if (c == 0)
|
||||
{
|
||||
Debug.LogError("Matrix is empty!");
|
||||
return 0;
|
||||
}
|
||||
else
|
||||
{
|
||||
decimal resDet = 0;
|
||||
for (int i = 0; i < c; i++)
|
||||
{
|
||||
for (int j = 0; j < c; j++)
|
||||
{
|
||||
decimal[][] newM = new decimal[c - 1][];
|
||||
for (int k = 0; k < c - 1; k++)
|
||||
{
|
||||
newM[k] = new decimal[c - 1];
|
||||
}
|
||||
|
||||
bool isAddedK = false, isAddedL = false;
|
||||
int offsetK = 0, offsetL = 0;
|
||||
|
||||
for (int k = 0; k < c; k++)
|
||||
{
|
||||
isAddedL = false;
|
||||
offsetL = 0;
|
||||
for (int l = 0; l < c; l++)
|
||||
{
|
||||
if (!(k == i || l == j))
|
||||
{
|
||||
newM[k + offsetK][l + offsetL] = _m[k][l];
|
||||
}
|
||||
else
|
||||
{
|
||||
if (!isAddedK && k == i)
|
||||
{
|
||||
offsetK = -1;
|
||||
isAddedK = true;
|
||||
}
|
||||
if (!isAddedL && l == j)
|
||||
{
|
||||
offsetL = -1;
|
||||
isAddedL = true;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
decimal cofactor = (decimal)Math.Pow(-1, 0 + j + 2) * MatrixDeterminant(newM);
|
||||
resDet += cofactor * _m[0][j];
|
||||
}
|
||||
}
|
||||
return (decimal)Math.Round(resDet, 5);
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
Debug.LogError("This matrix is not a square matrix!");
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
|
||||
public static decimal[] SolveEquation(decimal[][] _m, decimal[] _v)
|
||||
{
|
||||
int len = _m.Length;
|
||||
|
||||
decimal[] res = new decimal[len];
|
||||
decimal d0 = MatrixDeterminant(_m);
|
||||
|
||||
for (int i = 0; i < len; i++)
|
||||
{
|
||||
decimal[][] testMa = new decimal[len][];
|
||||
for (int j = 0; j < len; j++)
|
||||
{
|
||||
testMa[j] = new decimal[len];
|
||||
for (int k = 0; k < len; k++)
|
||||
{
|
||||
testMa[j][k] = _m[j][k];
|
||||
}
|
||||
}
|
||||
for (int j = 0; j < len; j++)
|
||||
{
|
||||
testMa[j][i] = _v[j];
|
||||
}
|
||||
decimal di = MatrixDeterminant(testMa);
|
||||
res[i] = (decimal)Math.Round(di / d0, 5);
|
||||
}
|
||||
|
||||
return res;
|
||||
}
|
||||
|
||||
public static decimal[][] ConvertLinearEquationsToMatrix(List<string> equations, List<string> variables)
|
||||
{
|
||||
if (equations.Count == variables.Count)
|
||||
{
|
||||
decimal[][] res = new decimal[equations.Count][];
|
||||
for (int i = 0; i < res.Length; i++)
|
||||
{
|
||||
res[i] = new decimal[equations.Count];
|
||||
}
|
||||
for (int i = 0; i < equations.Count; i++)
|
||||
{
|
||||
for (int j = 0; j < equations[i].Length; j++)
|
||||
{
|
||||
for (int k = 0; k < variables.Count; k++)
|
||||
{
|
||||
if (equations[i][j] == variables[k][0])
|
||||
{
|
||||
string coeff = "";
|
||||
for (int l = j - 1; l >= 0; l--)
|
||||
{
|
||||
if (equations[i][l] != '*' && equations[i][l] != '+' && equations[i][l] != '-')
|
||||
{
|
||||
coeff += equations[i][l];
|
||||
}
|
||||
if (equations[i][l] == '-')
|
||||
{
|
||||
coeff += '-';
|
||||
break;
|
||||
}
|
||||
if (equations[i][l] == '+')
|
||||
{
|
||||
break;
|
||||
}
|
||||
}
|
||||
char[] charArray = coeff.ToCharArray();
|
||||
Array.Reverse(charArray);
|
||||
coeff = new string(charArray);
|
||||
res[i][k] = decimal.Parse(coeff);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
return res;
|
||||
}
|
||||
else return null;
|
||||
}
|
||||
|
||||
public static decimal[][] MatrixMultiplication(decimal[][] _m1, decimal[][] _m2)
|
||||
{
|
||||
if (_m1[0].Length == _m2.Length)
|
||||
{
|
||||
decimal[][] res = new decimal[_m1.Length][];
|
||||
for (int i = 0; i < _m1.Length; i++)
|
||||
{
|
||||
res[i] = new decimal[_m2[0].Length];
|
||||
}
|
||||
|
||||
for (int i = 0; i < res.Length; i++)
|
||||
{
|
||||
for (int j = 0; j < res[0].Length; j++)
|
||||
{
|
||||
decimal sum = 0;
|
||||
for (int k = 0; k < _m2.Length; k++)
|
||||
{
|
||||
sum += _m1[i][k] * _m2[k][j];
|
||||
}
|
||||
res[i][j] = sum;
|
||||
}
|
||||
}
|
||||
return res;
|
||||
}
|
||||
else
|
||||
{
|
||||
Debug.LogError("Matrices cannot be multiplied!");
|
||||
return null;
|
||||
}
|
||||
}
|
||||
public static decimal[][] MatrixMultiplication(decimal[][] _m, decimal _c)
|
||||
{
|
||||
decimal[][] res = _m;
|
||||
for (int i = 0; i < _m.Length; i++)
|
||||
{
|
||||
for (int j = 0; j < _m[0].Length; j++)
|
||||
{
|
||||
res[i][j] *= _c;
|
||||
}
|
||||
}
|
||||
return res;
|
||||
}
|
||||
|
||||
public static decimal[] TurnListToVector(List<decimal> _vec)
|
||||
{
|
||||
decimal[] res = new decimal[_vec.Count];
|
||||
for (int i = 0; i < _vec.Count; i++)
|
||||
{
|
||||
res[i] = _vec[i];
|
||||
}
|
||||
return res;
|
||||
}
|
||||
public static decimal[][] TurnListToMatrix(List<List<decimal>> _m)
|
||||
{
|
||||
decimal[][] res = new decimal[_m.Count][];
|
||||
for (int i = 0; i < _m.Count; i++)
|
||||
{
|
||||
res[i] = new decimal[_m[i].Count];
|
||||
for (int j = 0; j < _m[i].Count; j++)
|
||||
{
|
||||
res[i][j] = _m[i][j];
|
||||
}
|
||||
}
|
||||
return res;
|
||||
}
|
||||
|
||||
public static string DebugMatrix(decimal[][] _m, string _sep = " ")
|
||||
{
|
||||
string sum = "";
|
||||
for (int i = 0; i < _m.Length; i++)
|
||||
{
|
||||
for (int j = 0; j < _m[0].Length; j++)
|
||||
{
|
||||
sum += _m[i][j] + ((j == _m[0].Length - 1) ? "\n" : _sep);
|
||||
}
|
||||
}
|
||||
return sum;
|
||||
}
|
||||
public static string DebugVector(decimal[] _v, string _sep = " ")
|
||||
{
|
||||
string sum = "";
|
||||
for (int i = 0; i < _v.Length; i++)
|
||||
{
|
||||
sum += _v[i] + "\n";
|
||||
}
|
||||
return sum;
|
||||
}
|
||||
|
||||
}
|
||||
11
Assets/Scripts/Matrix.cs.meta
Normal file
11
Assets/Scripts/Matrix.cs.meta
Normal file
|
|
@ -0,0 +1,11 @@
|
|||
fileFormatVersion: 2
|
||||
guid: 2462d4b55c2c2d3438a6fc61d3b6445a
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
8
Assets/Scripts/Matrix.meta
Normal file
8
Assets/Scripts/Matrix.meta
Normal file
|
|
@ -0,0 +1,8 @@
|
|||
fileFormatVersion: 2
|
||||
guid: bf225744eb8d4a743bc0a97d95a0d137
|
||||
folderAsset: yes
|
||||
DefaultImporter:
|
||||
externalObjects: {}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
174
Assets/Scripts/Matrix/CholeskyDecomposition.cs
Normal file
174
Assets/Scripts/Matrix/CholeskyDecomposition.cs
Normal file
|
|
@ -0,0 +1,174 @@
|
|||
using System;
|
||||
using System.Runtime.Serialization;
|
||||
|
||||
namespace DotNetMatrix
|
||||
{
|
||||
|
||||
/// <summary>Cholesky Decomposition.
|
||||
/// For a symmetric, positive definite matrix A, the Cholesky decomposition
|
||||
/// is an lower triangular matrix L so that A = L*L'.
|
||||
/// If the matrix is not symmetric or positive definite, the constructor
|
||||
/// returns a partial decomposition and sets an internal flag that may
|
||||
/// be queried by the isSPD() method.
|
||||
/// </summary>
|
||||
|
||||
[Serializable]
|
||||
public class CholeskyDecomposition : System.Runtime.Serialization.ISerializable
|
||||
{
|
||||
#region Class variables
|
||||
|
||||
/// <summary>Array for internal storage of decomposition.
|
||||
/// @serial internal array storage.
|
||||
/// </summary>
|
||||
private decimal[][] L;
|
||||
|
||||
/// <summary>Row and column dimension (square matrix).
|
||||
/// @serial matrix dimension.
|
||||
/// </summary>
|
||||
private int n;
|
||||
|
||||
/// <summary>Symmetric and positive definite flag.
|
||||
/// @serial is symmetric and positive definite flag.
|
||||
/// </summary>
|
||||
private bool isspd;
|
||||
|
||||
#endregion // Class variables
|
||||
|
||||
#region Constructor
|
||||
|
||||
/// <summary>Cholesky algorithm for symmetric and positive definite matrix.</summary>
|
||||
/// <param name="Arg"> Square, symmetric matrix.
|
||||
/// </param>
|
||||
/// <returns> Structure to access L and isspd flag.
|
||||
/// </returns>
|
||||
|
||||
public CholeskyDecomposition(GeneralMatrix Arg)
|
||||
{
|
||||
// Initialize.
|
||||
decimal[][] A = Arg.Array;
|
||||
n = Arg.RowDimension;
|
||||
L = new decimal[n][];
|
||||
for (int i = 0; i < n; i++)
|
||||
{
|
||||
L[i] = new decimal[n];
|
||||
}
|
||||
isspd = (Arg.ColumnDimension == n);
|
||||
// Main loop.
|
||||
for (int j = 0; j < n; j++)
|
||||
{
|
||||
decimal[] Lrowj = L[j];
|
||||
decimal d = 0.0m;
|
||||
for (int k = 0; k < j; k++)
|
||||
{
|
||||
decimal[] Lrowk = L[k];
|
||||
decimal s = 0.0m;
|
||||
for (int i = 0; i < k; i++)
|
||||
{
|
||||
s += Lrowk[i] * Lrowj[i];
|
||||
}
|
||||
Lrowj[k] = s = (A[j][k] - s) / L[k][k];
|
||||
d = d + s * s;
|
||||
isspd = isspd & (A[k][j] == A[j][k]);
|
||||
}
|
||||
d = A[j][j] - d;
|
||||
isspd = isspd & (d > 0.0m);
|
||||
L[j][j] = (decimal)Math.Sqrt(Math.Max((double)d, 0));
|
||||
for (int k = j + 1; k < n; k++)
|
||||
{
|
||||
L[j][k] = 0.0m;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#endregion // Constructor
|
||||
|
||||
#region Public Properties
|
||||
/// <summary>Is the matrix symmetric and positive definite?</summary>
|
||||
/// <returns> true if A is symmetric and positive definite.
|
||||
/// </returns>
|
||||
virtual public bool SPD
|
||||
{
|
||||
get
|
||||
{
|
||||
return isspd;
|
||||
}
|
||||
}
|
||||
#endregion // Public Properties
|
||||
|
||||
#region Public Methods
|
||||
|
||||
/// <summary>Return triangular factor.</summary>
|
||||
/// <returns> L
|
||||
/// </returns>
|
||||
|
||||
public virtual GeneralMatrix GetL()
|
||||
{
|
||||
return new GeneralMatrix(L, n, n);
|
||||
}
|
||||
|
||||
/// <summary>Solve A*X = B</summary>
|
||||
/// <param name="B"> A Matrix with as many rows as A and any number of columns.
|
||||
/// </param>
|
||||
/// <returns> X so that L*L'*X = B
|
||||
/// </returns>
|
||||
/// <exception cref="System.ArgumentException"> Matrix row dimensions must agree.
|
||||
/// </exception>
|
||||
/// <exception cref="System.SystemException"> Matrix is not symmetric positive definite.
|
||||
/// </exception>
|
||||
|
||||
public virtual GeneralMatrix Solve(GeneralMatrix B)
|
||||
{
|
||||
if (B.RowDimension != n)
|
||||
{
|
||||
throw new System.ArgumentException("Matrix row dimensions must agree.");
|
||||
}
|
||||
if (!isspd)
|
||||
{
|
||||
throw new System.SystemException("Matrix is not symmetric positive definite.");
|
||||
}
|
||||
|
||||
// Copy right hand side.
|
||||
decimal[][] X = B.ArrayCopy;
|
||||
int nx = B.ColumnDimension;
|
||||
|
||||
// Solve L*Y = B;
|
||||
for (int k = 0; k < n; k++)
|
||||
{
|
||||
for (int i = k + 1; i < n; i++)
|
||||
{
|
||||
for (int j = 0; j < nx; j++)
|
||||
{
|
||||
X[i][j] -= X[k][j] * L[i][k];
|
||||
}
|
||||
}
|
||||
for (int j = 0; j < nx; j++)
|
||||
{
|
||||
X[k][j] /= L[k][k];
|
||||
}
|
||||
}
|
||||
|
||||
// Solve L'*X = Y;
|
||||
for (int k = n - 1; k >= 0; k--)
|
||||
{
|
||||
for (int j = 0; j < nx; j++)
|
||||
{
|
||||
X[k][j] /= L[k][k];
|
||||
}
|
||||
for (int i = 0; i < k; i++)
|
||||
{
|
||||
for (int j = 0; j < nx; j++)
|
||||
{
|
||||
X[i][j] -= X[k][j] * L[k][i];
|
||||
}
|
||||
}
|
||||
}
|
||||
return new GeneralMatrix(X, n, nx);
|
||||
}
|
||||
#endregion // Public Methods
|
||||
|
||||
// A method called when serializing this class.
|
||||
void ISerializable.GetObjectData(SerializationInfo info, StreamingContext context)
|
||||
{
|
||||
}
|
||||
}
|
||||
}
|
||||
11
Assets/Scripts/Matrix/CholeskyDecomposition.cs.meta
Normal file
11
Assets/Scripts/Matrix/CholeskyDecomposition.cs.meta
Normal file
|
|
@ -0,0 +1,11 @@
|
|||
fileFormatVersion: 2
|
||||
guid: e3a2b7302aee27d49b7dae10fc2c43b2
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
1143
Assets/Scripts/Matrix/EigenvalueDecomposition.cs
Normal file
1143
Assets/Scripts/Matrix/EigenvalueDecomposition.cs
Normal file
File diff suppressed because it is too large
Load diff
11
Assets/Scripts/Matrix/EigenvalueDecomposition.cs.meta
Normal file
11
Assets/Scripts/Matrix/EigenvalueDecomposition.cs.meta
Normal file
|
|
@ -0,0 +1,11 @@
|
|||
fileFormatVersion: 2
|
||||
guid: 8fa5c66b38ce32f47b0959bb0315cd3b
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
1297
Assets/Scripts/Matrix/GeneralMatrix.cs
Normal file
1297
Assets/Scripts/Matrix/GeneralMatrix.cs
Normal file
File diff suppressed because it is too large
Load diff
11
Assets/Scripts/Matrix/GeneralMatrix.cs.meta
Normal file
11
Assets/Scripts/Matrix/GeneralMatrix.cs.meta
Normal file
|
|
@ -0,0 +1,11 @@
|
|||
fileFormatVersion: 2
|
||||
guid: 724de4fb0b68d7641852aedf09ccc5c2
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
322
Assets/Scripts/Matrix/LUDecomposition.cs
Normal file
322
Assets/Scripts/Matrix/LUDecomposition.cs
Normal file
|
|
@ -0,0 +1,322 @@
|
|||
using System;
|
||||
using System.Runtime.Serialization;
|
||||
|
||||
namespace DotNetMatrix
|
||||
{
|
||||
|
||||
/// <summary>LU Decomposition.
|
||||
/// For an m-by-n matrix A with m >= n, the LU decomposition is an m-by-n
|
||||
/// unit lower triangular matrix L, an n-by-n upper triangular matrix U,
|
||||
/// and a permutation vector piv of length m so that A(piv,:) = L*U.
|
||||
/// <code> If m < n, then L is m-by-m and U is m-by-n. </code>
|
||||
/// The LU decompostion with pivoting always exists, even if the matrix is
|
||||
/// singular, so the constructor will never fail. The primary use of the
|
||||
/// LU decomposition is in the solution of square systems of simultaneous
|
||||
/// linear equations. This will fail if IsNonSingular() returns false.
|
||||
/// </summary>
|
||||
|
||||
[Serializable]
|
||||
public class LUDecomposition : System.Runtime.Serialization.ISerializable
|
||||
{
|
||||
#region Class variables
|
||||
|
||||
/// <summary>Array for internal storage of decomposition.
|
||||
/// @serial internal array storage.
|
||||
/// </summary>
|
||||
private decimal[][] LU;
|
||||
|
||||
/// <summary>Row and column dimensions, and pivot sign.
|
||||
/// @serial column dimension.
|
||||
/// @serial row dimension.
|
||||
/// @serial pivot sign.
|
||||
/// </summary>
|
||||
private int m, n, pivsign;
|
||||
|
||||
/// <summary>Internal storage of pivot vector.
|
||||
/// @serial pivot vector.
|
||||
/// </summary>
|
||||
private int[] piv;
|
||||
|
||||
#endregion // Class variables
|
||||
|
||||
#region Constructor
|
||||
|
||||
/// <summary>LU Decomposition</summary>
|
||||
/// <param name="A"> Rectangular matrix
|
||||
/// </param>
|
||||
/// <returns> Structure to access L, U and piv.
|
||||
/// </returns>
|
||||
|
||||
public LUDecomposition(GeneralMatrix A)
|
||||
{
|
||||
// Use a "left-looking", dot-product, Crout/Doolittle algorithm.
|
||||
|
||||
LU = A.ArrayCopy;
|
||||
m = A.RowDimension;
|
||||
n = A.ColumnDimension;
|
||||
piv = new int[m];
|
||||
for (int i = 0; i < m; i++)
|
||||
{
|
||||
piv[i] = i;
|
||||
}
|
||||
pivsign = 1;
|
||||
decimal[] LUrowi;
|
||||
decimal[] LUcolj = new decimal[m];
|
||||
|
||||
// Outer loop.
|
||||
|
||||
for (int j = 0; j < n; j++)
|
||||
{
|
||||
|
||||
// Make a copy of the j-th column to localize references.
|
||||
|
||||
for (int i = 0; i < m; i++)
|
||||
{
|
||||
LUcolj[i] = LU[i][j];
|
||||
}
|
||||
|
||||
// Apply previous transformations.
|
||||
|
||||
for (int i = 0; i < m; i++)
|
||||
{
|
||||
LUrowi = LU[i];
|
||||
|
||||
// Most of the time is spent in the following dot product.
|
||||
|
||||
int kmax = System.Math.Min(i, j);
|
||||
decimal s = 0.0m;
|
||||
for (int k = 0; k < kmax; k++)
|
||||
{
|
||||
s += LUrowi[k] * LUcolj[k];
|
||||
}
|
||||
|
||||
LUrowi[j] = LUcolj[i] -= s;
|
||||
}
|
||||
|
||||
// Find pivot and exchange if necessary.
|
||||
|
||||
int p = j;
|
||||
for (int i = j + 1; i < m; i++)
|
||||
{
|
||||
if (System.Math.Abs(LUcolj[i]) > System.Math.Abs(LUcolj[p]))
|
||||
{
|
||||
p = i;
|
||||
}
|
||||
}
|
||||
if (p != j)
|
||||
{
|
||||
for (int k = 0; k < n; k++)
|
||||
{
|
||||
decimal t = LU[p][k]; LU[p][k] = LU[j][k]; LU[j][k] = t;
|
||||
}
|
||||
int k2 = piv[p]; piv[p] = piv[j]; piv[j] = k2;
|
||||
pivsign = - pivsign;
|
||||
}
|
||||
|
||||
// Compute multipliers.
|
||||
|
||||
if (j < m & LU[j][j] != 0.0m)
|
||||
{
|
||||
for (int i = j + 1; i < m; i++)
|
||||
{
|
||||
LU[i][j] /= LU[j][j];
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
#endregion // Constructor
|
||||
|
||||
#region Public Properties
|
||||
/// <summary>Is the matrix nonsingular?</summary>
|
||||
/// <returns> true if U, and hence A, is nonsingular.
|
||||
/// </returns>
|
||||
virtual public bool IsNonSingular
|
||||
{
|
||||
get
|
||||
{
|
||||
for (int j = 0; j < n; j++)
|
||||
{
|
||||
if (LU[j][j] == 0)
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>Return lower triangular factor</summary>
|
||||
/// <returns> L
|
||||
/// </returns>
|
||||
virtual public GeneralMatrix L
|
||||
{
|
||||
get
|
||||
{
|
||||
GeneralMatrix X = new GeneralMatrix(m, n);
|
||||
decimal[][] L = X.Array;
|
||||
for (int i = 0; i < m; i++)
|
||||
{
|
||||
for (int j = 0; j < n; j++)
|
||||
{
|
||||
if (i > j)
|
||||
{
|
||||
L[i][j] = LU[i][j];
|
||||
}
|
||||
else if (i == j)
|
||||
{
|
||||
L[i][j] = 1.0m;
|
||||
}
|
||||
else
|
||||
{
|
||||
L[i][j] = 0.0m;
|
||||
}
|
||||
}
|
||||
}
|
||||
return X;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>Return upper triangular factor</summary>
|
||||
/// <returns> U
|
||||
/// </returns>
|
||||
virtual public GeneralMatrix U
|
||||
{
|
||||
get
|
||||
{
|
||||
GeneralMatrix X = new GeneralMatrix(n, n);
|
||||
decimal[][] U = X.Array;
|
||||
for (int i = 0; i < n; i++)
|
||||
{
|
||||
for (int j = 0; j < n; j++)
|
||||
{
|
||||
if (i <= j)
|
||||
{
|
||||
U[i][j] = LU[i][j];
|
||||
}
|
||||
else
|
||||
{
|
||||
U[i][j] = 0.0m;
|
||||
}
|
||||
}
|
||||
}
|
||||
return X;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>Return pivot permutation vector</summary>
|
||||
/// <returns> piv
|
||||
/// </returns>
|
||||
virtual public int[] Pivot
|
||||
{
|
||||
get
|
||||
{
|
||||
int[] p = new int[m];
|
||||
for (int i = 0; i < m; i++)
|
||||
{
|
||||
p[i] = piv[i];
|
||||
}
|
||||
return p;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>Return pivot permutation vector as a one-dimensional decimal array</summary>
|
||||
/// <returns> (decimal) piv
|
||||
/// </returns>
|
||||
virtual public decimal[] decimalPivot
|
||||
{
|
||||
get
|
||||
{
|
||||
decimal[] vals = new decimal[m];
|
||||
for (int i = 0; i < m; i++)
|
||||
{
|
||||
vals[i] = (decimal) piv[i];
|
||||
}
|
||||
return vals;
|
||||
}
|
||||
}
|
||||
|
||||
#endregion // Public Properties
|
||||
|
||||
#region Public Methods
|
||||
|
||||
/// <summary>Determinant</summary>
|
||||
/// <returns> det(A)
|
||||
/// </returns>
|
||||
/// <exception cref="System.ArgumentException"> Matrix must be square
|
||||
/// </exception>
|
||||
|
||||
public virtual decimal Determinant()
|
||||
{
|
||||
if (m != n)
|
||||
{
|
||||
throw new System.ArgumentException("Matrix must be square.");
|
||||
}
|
||||
decimal d = (decimal) pivsign;
|
||||
for (int j = 0; j < n; j++)
|
||||
{
|
||||
d *= LU[j][j];
|
||||
}
|
||||
return d;
|
||||
}
|
||||
|
||||
/// <summary>Solve A*X = B</summary>
|
||||
/// <param name="B"> A Matrix with as many rows as A and any number of columns.
|
||||
/// </param>
|
||||
/// <returns> X so that L*U*X = B(piv,:)
|
||||
/// </returns>
|
||||
/// <exception cref="System.ArgumentException"> Matrix row dimensions must agree.
|
||||
/// </exception>
|
||||
/// <exception cref="System.SystemException"> Matrix is singular.
|
||||
/// </exception>
|
||||
|
||||
public virtual GeneralMatrix Solve(GeneralMatrix B)
|
||||
{
|
||||
if (B.RowDimension != m)
|
||||
{
|
||||
throw new System.ArgumentException("Matrix row dimensions must agree.");
|
||||
}
|
||||
if (!this.IsNonSingular)
|
||||
{
|
||||
throw new System.SystemException("Matrix is singular.");
|
||||
}
|
||||
|
||||
// Copy right hand side with pivoting
|
||||
int nx = B.ColumnDimension;
|
||||
GeneralMatrix Xmat = B.GetMatrix(piv, 0, nx - 1);
|
||||
decimal[][] X = Xmat.Array;
|
||||
|
||||
// Solve L*Y = B(piv,:)
|
||||
for (int k = 0; k < n; k++)
|
||||
{
|
||||
for (int i = k + 1; i < n; i++)
|
||||
{
|
||||
for (int j = 0; j < nx; j++)
|
||||
{
|
||||
X[i][j] -= X[k][j] * LU[i][k];
|
||||
}
|
||||
}
|
||||
}
|
||||
// Solve U*X = Y;
|
||||
for (int k = n - 1; k >= 0; k--)
|
||||
{
|
||||
for (int j = 0; j < nx; j++)
|
||||
{
|
||||
X[k][j] /= LU[k][k];
|
||||
}
|
||||
for (int i = 0; i < k; i++)
|
||||
{
|
||||
for (int j = 0; j < nx; j++)
|
||||
{
|
||||
X[i][j] -= X[k][j] * LU[i][k];
|
||||
}
|
||||
}
|
||||
}
|
||||
return Xmat;
|
||||
}
|
||||
|
||||
#endregion // Public Methods
|
||||
|
||||
// A method called when serializing this class.
|
||||
void ISerializable.GetObjectData(SerializationInfo info, StreamingContext context)
|
||||
{
|
||||
}
|
||||
}
|
||||
}
|
||||
11
Assets/Scripts/Matrix/LUDecomposition.cs.meta
Normal file
11
Assets/Scripts/Matrix/LUDecomposition.cs.meta
Normal file
|
|
@ -0,0 +1,11 @@
|
|||
fileFormatVersion: 2
|
||||
guid: 17b819c1b38065c4383c9c8112d2e300
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
287
Assets/Scripts/Matrix/QRDecomposition.cs
Normal file
287
Assets/Scripts/Matrix/QRDecomposition.cs
Normal file
|
|
@ -0,0 +1,287 @@
|
|||
using System;
|
||||
using System.Runtime.Serialization;
|
||||
|
||||
namespace DotNetMatrix
|
||||
{
|
||||
|
||||
/// <summary>QR Decomposition.
|
||||
/// For an m-by-n matrix A with m >= n, the QR decomposition is an m-by-n
|
||||
/// orthogonal matrix Q and an n-by-n upper triangular matrix R so that
|
||||
/// A = Q*R.
|
||||
///
|
||||
/// The QR decompostion always exists, even if the matrix does not have
|
||||
/// full rank, so the constructor will never fail. The primary use of the
|
||||
/// QR decomposition is in the least squares solution of nonsquare systems
|
||||
/// of simultaneous linear equations. This will fail if IsFullRank()
|
||||
/// returns false.
|
||||
/// </summary>
|
||||
|
||||
[Serializable]
|
||||
public class QRDecomposition : System.Runtime.Serialization.ISerializable
|
||||
{
|
||||
#region Class variables
|
||||
|
||||
/// <summary>Array for internal storage of decomposition.
|
||||
/// @serial internal array storage.
|
||||
/// </summary>
|
||||
private decimal[][] QR;
|
||||
|
||||
/// <summary>Row and column dimensions.
|
||||
/// @serial column dimension.
|
||||
/// @serial row dimension.
|
||||
/// </summary>
|
||||
private int m, n;
|
||||
|
||||
/// <summary>Array for internal storage of diagonal of R.
|
||||
/// @serial diagonal of R.
|
||||
/// </summary>
|
||||
private decimal[] Rdiag;
|
||||
|
||||
#endregion // Class variables
|
||||
|
||||
#region Constructor
|
||||
|
||||
/// <summary>QR Decomposition, computed by Householder reflections.</summary>
|
||||
/// <param name="A"> Rectangular matrix
|
||||
/// </param>
|
||||
/// <returns> Structure to access R and the Householder vectors and compute Q.
|
||||
/// </returns>
|
||||
|
||||
public QRDecomposition(GeneralMatrix A)
|
||||
{
|
||||
// Initialize.
|
||||
QR = A.ArrayCopy;
|
||||
m = A.RowDimension;
|
||||
n = A.ColumnDimension;
|
||||
Rdiag = new decimal[n];
|
||||
|
||||
// Main loop.
|
||||
for (int k = 0; k < n; k++)
|
||||
{
|
||||
// Compute 2-norm of k-th column without under/overflow.
|
||||
decimal nrm = 0;
|
||||
for (int i = k; i < m; i++)
|
||||
{
|
||||
nrm = Maths.Hypot(nrm, QR[i][k]);
|
||||
}
|
||||
|
||||
if (nrm != 0.0m)
|
||||
{
|
||||
// Form k-th Householder vector.
|
||||
if (QR[k][k] < 0)
|
||||
{
|
||||
nrm = - nrm;
|
||||
}
|
||||
for (int i = k; i < m; i++)
|
||||
{
|
||||
QR[i][k] /= nrm;
|
||||
}
|
||||
QR[k][k] += 1.0m;
|
||||
|
||||
// Apply transformation to remaining columns.
|
||||
for (int j = k + 1; j < n; j++)
|
||||
{
|
||||
decimal s = 0.0m;
|
||||
for (int i = k; i < m; i++)
|
||||
{
|
||||
s += QR[i][k] * QR[i][j];
|
||||
}
|
||||
s = (- s) / QR[k][k];
|
||||
for (int i = k; i < m; i++)
|
||||
{
|
||||
QR[i][j] += s * QR[i][k];
|
||||
}
|
||||
}
|
||||
}
|
||||
Rdiag[k] = - nrm;
|
||||
}
|
||||
}
|
||||
|
||||
#endregion // Constructor
|
||||
|
||||
#region Public Properties
|
||||
|
||||
/// <summary>Is the matrix full rank?</summary>
|
||||
/// <returns> true if R, and hence A, has full rank.
|
||||
/// </returns>
|
||||
virtual public bool FullRank
|
||||
{
|
||||
get
|
||||
{
|
||||
for (int j = 0; j < n; j++)
|
||||
{
|
||||
if (Rdiag[j] == 0)
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>Return the Householder vectors</summary>
|
||||
/// <returns> Lower trapezoidal matrix whose columns define the reflections
|
||||
/// </returns>
|
||||
virtual public GeneralMatrix H
|
||||
{
|
||||
get
|
||||
{
|
||||
GeneralMatrix X = new GeneralMatrix(m, n);
|
||||
decimal[][] H = X.Array;
|
||||
for (int i = 0; i < m; i++)
|
||||
{
|
||||
for (int j = 0; j < n; j++)
|
||||
{
|
||||
if (i >= j)
|
||||
{
|
||||
H[i][j] = QR[i][j];
|
||||
}
|
||||
else
|
||||
{
|
||||
H[i][j] = 0.0m;
|
||||
}
|
||||
}
|
||||
}
|
||||
return X;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
/// <summary>Return the upper triangular factor</summary>
|
||||
/// <returns> R
|
||||
/// </returns>
|
||||
virtual public GeneralMatrix R
|
||||
{
|
||||
get
|
||||
{
|
||||
GeneralMatrix X = new GeneralMatrix(n, n);
|
||||
decimal[][] R = X.Array;
|
||||
for (int i = 0; i < n; i++)
|
||||
{
|
||||
for (int j = 0; j < n; j++)
|
||||
{
|
||||
if (i < j)
|
||||
{
|
||||
R[i][j] = QR[i][j];
|
||||
}
|
||||
else if (i == j)
|
||||
{
|
||||
R[i][j] = Rdiag[i];
|
||||
}
|
||||
else
|
||||
{
|
||||
R[i][j] = 0.0m;
|
||||
}
|
||||
}
|
||||
}
|
||||
return X;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>Generate and return the (economy-sized) orthogonal factor</summary>
|
||||
/// <returns> Q
|
||||
/// </returns>
|
||||
virtual public GeneralMatrix Q
|
||||
{
|
||||
get
|
||||
{
|
||||
GeneralMatrix X = new GeneralMatrix(m, n);
|
||||
decimal[][] Q = X.Array;
|
||||
for (int k = n - 1; k >= 0; k--)
|
||||
{
|
||||
for (int i = 0; i < m; i++)
|
||||
{
|
||||
Q[i][k] = 0.0m;
|
||||
}
|
||||
Q[k][k] = 1.0m;
|
||||
for (int j = k; j < n; j++)
|
||||
{
|
||||
if (QR[k][k] != 0)
|
||||
{
|
||||
decimal s = 0.0m;
|
||||
for (int i = k; i < m; i++)
|
||||
{
|
||||
s += QR[i][k] * Q[i][j];
|
||||
}
|
||||
s = (- s) / QR[k][k];
|
||||
for (int i = k; i < m; i++)
|
||||
{
|
||||
Q[i][j] += s * QR[i][k];
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
return X;
|
||||
}
|
||||
}
|
||||
#endregion // Public Properties
|
||||
|
||||
#region Public Methods
|
||||
|
||||
/// <summary>Least squares solution of A*X = B</summary>
|
||||
/// <param name="B"> A Matrix with as many rows as A and any number of columns.
|
||||
/// </param>
|
||||
/// <returns> X that minimizes the two norm of Q*R*X-B.
|
||||
/// </returns>
|
||||
/// <exception cref="System.ArgumentException"> Matrix row dimensions must agree.
|
||||
/// </exception>
|
||||
/// <exception cref="System.SystemException"> Matrix is rank deficient.
|
||||
/// </exception>
|
||||
|
||||
public virtual GeneralMatrix Solve(GeneralMatrix B)
|
||||
{
|
||||
if (B.RowDimension != m)
|
||||
{
|
||||
throw new System.ArgumentException("GeneralMatrix row dimensions must agree.");
|
||||
}
|
||||
if (!this.FullRank)
|
||||
{
|
||||
throw new System.SystemException("Matrix is rank deficient.");
|
||||
}
|
||||
|
||||
// Copy right hand side
|
||||
int nx = B.ColumnDimension;
|
||||
decimal[][] X = B.ArrayCopy;
|
||||
|
||||
// Compute Y = transpose(Q)*B
|
||||
for (int k = 0; k < n; k++)
|
||||
{
|
||||
for (int j = 0; j < nx; j++)
|
||||
{
|
||||
decimal s = 0.0m;
|
||||
for (int i = k; i < m; i++)
|
||||
{
|
||||
s += QR[i][k] * X[i][j];
|
||||
}
|
||||
s = (- s) / QR[k][k];
|
||||
for (int i = k; i < m; i++)
|
||||
{
|
||||
X[i][j] += s * QR[i][k];
|
||||
}
|
||||
}
|
||||
}
|
||||
// Solve R*X = Y;
|
||||
for (int k = n - 1; k >= 0; k--)
|
||||
{
|
||||
for (int j = 0; j < nx; j++)
|
||||
{
|
||||
X[k][j] /= Rdiag[k];
|
||||
}
|
||||
for (int i = 0; i < k; i++)
|
||||
{
|
||||
for (int j = 0; j < nx; j++)
|
||||
{
|
||||
X[i][j] -= X[k][j] * QR[i][k];
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return (new GeneralMatrix(X, n, nx).GetMatrix(0, n - 1, 0, nx - 1));
|
||||
}
|
||||
|
||||
#endregion // Public Methods
|
||||
|
||||
// A method called when serializing this class.
|
||||
void ISerializable.GetObjectData(SerializationInfo info, StreamingContext context)
|
||||
{
|
||||
}
|
||||
}
|
||||
}
|
||||
11
Assets/Scripts/Matrix/QRDecomposition.cs.meta
Normal file
11
Assets/Scripts/Matrix/QRDecomposition.cs.meta
Normal file
|
|
@ -0,0 +1,11 @@
|
|||
fileFormatVersion: 2
|
||||
guid: 80c5f324489884149b7fd041dd174bf8
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
663
Assets/Scripts/Matrix/SingularValueDecomposition.cs
Normal file
663
Assets/Scripts/Matrix/SingularValueDecomposition.cs
Normal file
|
|
@ -0,0 +1,663 @@
|
|||
using System;
|
||||
using System.Runtime.Serialization;
|
||||
|
||||
namespace DotNetMatrix
|
||||
{
|
||||
|
||||
/// <summary>Singular Value Decomposition.
|
||||
/// <P>
|
||||
/// For an m-by-n matrix A with m >= n, the singular value decomposition is
|
||||
/// an m-by-n orthogonal matrix U, an n-by-n diagonal matrix S, and
|
||||
/// an n-by-n orthogonal matrix V so that A = U*S*V'.
|
||||
/// <P>
|
||||
/// The singular values, sigma[k] = S[k][k], are ordered so that
|
||||
/// sigma[0] >= sigma[1] >= ... >= sigma[n-1].
|
||||
/// <P>
|
||||
/// The singular value decompostion always exists, so the constructor will
|
||||
/// never fail. The matrix condition number and the effective numerical
|
||||
/// rank can be computed from this decomposition.
|
||||
/// </summary>
|
||||
|
||||
[Serializable]
|
||||
public class SingularValueDecomposition : System.Runtime.Serialization.ISerializable
|
||||
{
|
||||
#region Class variables
|
||||
|
||||
/// <summary>Arrays for internal storage of U and V.
|
||||
/// @serial internal storage of U.
|
||||
/// @serial internal storage of V.
|
||||
/// </summary>
|
||||
private decimal[][] U, V;
|
||||
|
||||
/// <summary>Array for internal storage of singular values.
|
||||
/// @serial internal storage of singular values.
|
||||
/// </summary>
|
||||
private decimal[] s;
|
||||
|
||||
/// <summary>Row and column dimensions.
|
||||
/// @serial row dimension.
|
||||
/// @serial column dimension.
|
||||
/// </summary>
|
||||
private int m, n;
|
||||
|
||||
#endregion //Class variables
|
||||
|
||||
#region Constructor
|
||||
|
||||
/// <summary>Construct the singular value decomposition</summary>
|
||||
/// <param name="Arg"> Rectangular matrix
|
||||
/// </param>
|
||||
/// <returns> Structure to access U, S and V.
|
||||
/// </returns>
|
||||
|
||||
public SingularValueDecomposition(GeneralMatrix Arg)
|
||||
{
|
||||
// Derived from LINPACK code.
|
||||
// Initialize.
|
||||
decimal[][] A = Arg.ArrayCopy;
|
||||
m = Arg.RowDimension;
|
||||
n = Arg.ColumnDimension;
|
||||
int nu = System.Math.Min(m, n);
|
||||
s = new decimal[System.Math.Min(m + 1, n)];
|
||||
U = new decimal[m][];
|
||||
for (int i = 0; i < m; i++)
|
||||
{
|
||||
U[i] = new decimal[nu];
|
||||
}
|
||||
V = new decimal[n][];
|
||||
for (int i2 = 0; i2 < n; i2++)
|
||||
{
|
||||
V[i2] = new decimal[n];
|
||||
}
|
||||
decimal[] e = new decimal[n];
|
||||
decimal[] work = new decimal[m];
|
||||
bool wantu = true;
|
||||
bool wantv = true;
|
||||
|
||||
// Reduce A to bidiagonal form, storing the diagonal elements
|
||||
// in s and the super-diagonal elements in e.
|
||||
|
||||
int nct = System.Math.Min(m - 1, n);
|
||||
int nrt = System.Math.Max(0, System.Math.Min(n - 2, m));
|
||||
for (int k = 0; k < System.Math.Max(nct, nrt); k++)
|
||||
{
|
||||
if (k < nct)
|
||||
{
|
||||
|
||||
// Compute the transformation for the k-th column and
|
||||
// place the k-th diagonal in s[k].
|
||||
// Compute 2-norm of k-th column without under/overflow.
|
||||
s[k] = 0;
|
||||
for (int i = k; i < m; i++)
|
||||
{
|
||||
s[k] = Maths.Hypot(s[k], A[i][k]);
|
||||
}
|
||||
if (s[k] != 0.0m)
|
||||
{
|
||||
if (A[k][k] < 0.0m)
|
||||
{
|
||||
s[k] = - s[k];
|
||||
}
|
||||
for (int i = k; i < m; i++)
|
||||
{
|
||||
A[i][k] /= s[k];
|
||||
}
|
||||
A[k][k] += 1.0m;
|
||||
}
|
||||
s[k] = - s[k];
|
||||
}
|
||||
for (int j = k + 1; j < n; j++)
|
||||
{
|
||||
if ((k < nct) & (s[k] != 0.0m))
|
||||
{
|
||||
|
||||
// Apply the transformation.
|
||||
|
||||
decimal t = 0;
|
||||
for (int i = k; i < m; i++)
|
||||
{
|
||||
t += A[i][k] * A[i][j];
|
||||
}
|
||||
t = (- t) / A[k][k];
|
||||
for (int i = k; i < m; i++)
|
||||
{
|
||||
A[i][j] += t * A[i][k];
|
||||
}
|
||||
}
|
||||
|
||||
// Place the k-th row of A into e for the
|
||||
// subsequent calculation of the row transformation.
|
||||
|
||||
e[j] = A[k][j];
|
||||
}
|
||||
if (wantu & (k < nct))
|
||||
{
|
||||
|
||||
// Place the transformation in U for subsequent back
|
||||
// multiplication.
|
||||
|
||||
for (int i = k; i < m; i++)
|
||||
{
|
||||
U[i][k] = A[i][k];
|
||||
}
|
||||
}
|
||||
if (k < nrt)
|
||||
{
|
||||
|
||||
// Compute the k-th row transformation and place the
|
||||
// k-th super-diagonal in e[k].
|
||||
// Compute 2-norm without under/overflow.
|
||||
e[k] = 0;
|
||||
for (int i = k + 1; i < n; i++)
|
||||
{
|
||||
e[k] = Maths.Hypot(e[k], e[i]);
|
||||
}
|
||||
if (e[k] != 0.0m)
|
||||
{
|
||||
if (e[k + 1] < 0.0m)
|
||||
{
|
||||
e[k] = - e[k];
|
||||
}
|
||||
for (int i = k + 1; i < n; i++)
|
||||
{
|
||||
e[i] /= e[k];
|
||||
}
|
||||
e[k + 1] += 1.0m;
|
||||
}
|
||||
e[k] = - e[k];
|
||||
if ((k + 1 < m) & (e[k] != 0.0m))
|
||||
{
|
||||
|
||||
// Apply the transformation.
|
||||
|
||||
for (int i = k + 1; i < m; i++)
|
||||
{
|
||||
work[i] = 0.0m;
|
||||
}
|
||||
for (int j = k + 1; j < n; j++)
|
||||
{
|
||||
for (int i = k + 1; i < m; i++)
|
||||
{
|
||||
work[i] += e[j] * A[i][j];
|
||||
}
|
||||
}
|
||||
for (int j = k + 1; j < n; j++)
|
||||
{
|
||||
decimal t = (- e[j]) / e[k + 1];
|
||||
for (int i = k + 1; i < m; i++)
|
||||
{
|
||||
A[i][j] += t * work[i];
|
||||
}
|
||||
}
|
||||
}
|
||||
if (wantv)
|
||||
{
|
||||
|
||||
// Place the transformation in V for subsequent
|
||||
// back multiplication.
|
||||
|
||||
for (int i = k + 1; i < n; i++)
|
||||
{
|
||||
V[i][k] = e[i];
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Set up the final bidiagonal matrix or order p.
|
||||
|
||||
int p = System.Math.Min(n, m + 1);
|
||||
if (nct < n)
|
||||
{
|
||||
s[nct] = A[nct][nct];
|
||||
}
|
||||
if (m < p)
|
||||
{
|
||||
s[p - 1] = 0.0m;
|
||||
}
|
||||
if (nrt + 1 < p)
|
||||
{
|
||||
e[nrt] = A[nrt][p - 1];
|
||||
}
|
||||
e[p - 1] = 0.0m;
|
||||
|
||||
// If required, generate U.
|
||||
|
||||
if (wantu)
|
||||
{
|
||||
for (int j = nct; j < nu; j++)
|
||||
{
|
||||
for (int i = 0; i < m; i++)
|
||||
{
|
||||
U[i][j] = 0.0m;
|
||||
}
|
||||
U[j][j] = 1.0m;
|
||||
}
|
||||
for (int k = nct - 1; k >= 0; k--)
|
||||
{
|
||||
if (s[k] != 0.0m)
|
||||
{
|
||||
for (int j = k + 1; j < nu; j++)
|
||||
{
|
||||
decimal t = 0;
|
||||
for (int i = k; i < m; i++)
|
||||
{
|
||||
t += U[i][k] * U[i][j];
|
||||
}
|
||||
t = (- t) / U[k][k];
|
||||
for (int i = k; i < m; i++)
|
||||
{
|
||||
U[i][j] += t * U[i][k];
|
||||
}
|
||||
}
|
||||
for (int i = k; i < m; i++)
|
||||
{
|
||||
U[i][k] = - U[i][k];
|
||||
}
|
||||
U[k][k] = 1.0m + U[k][k];
|
||||
for (int i = 0; i < k - 1; i++)
|
||||
{
|
||||
U[i][k] = 0.0m;
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
for (int i = 0; i < m; i++)
|
||||
{
|
||||
U[i][k] = 0.0m;
|
||||
}
|
||||
U[k][k] = 1.0m;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// If required, generate V.
|
||||
|
||||
if (wantv)
|
||||
{
|
||||
for (int k = n - 1; k >= 0; k--)
|
||||
{
|
||||
if ((k < nrt) & (e[k] != 0.0m))
|
||||
{
|
||||
for (int j = k + 1; j < nu; j++)
|
||||
{
|
||||
decimal t = 0;
|
||||
for (int i = k + 1; i < n; i++)
|
||||
{
|
||||
t += V[i][k] * V[i][j];
|
||||
}
|
||||
t = (- t) / V[k + 1][k];
|
||||
for (int i = k + 1; i < n; i++)
|
||||
{
|
||||
V[i][j] += t * V[i][k];
|
||||
}
|
||||
}
|
||||
}
|
||||
for (int i = 0; i < n; i++)
|
||||
{
|
||||
V[i][k] = 0.0m;
|
||||
}
|
||||
V[k][k] = 1.0m;
|
||||
}
|
||||
}
|
||||
|
||||
// Main iteration loop for the singular values.
|
||||
|
||||
int pp = p - 1;
|
||||
int iter = 0;
|
||||
decimal eps = (decimal)Math.Pow(2.0f, - 52.0f);
|
||||
while (p > 0)
|
||||
{
|
||||
int k, kase;
|
||||
|
||||
// Here is where a test for too many iterations would go.
|
||||
|
||||
// This section of the program inspects for
|
||||
// negligible elements in the s and e arrays. On
|
||||
// completion the variables kase and k are set as follows.
|
||||
|
||||
// kase = 1 if s(p) and e[k-1] are negligible and k<p
|
||||
// kase = 2 if s(k) is negligible and k<p
|
||||
// kase = 3 if e[k-1] is negligible, k<p, and
|
||||
// s(k), ..., s(p) are not negligible (qr step).
|
||||
// kase = 4 if e(p-1) is negligible (convergence).
|
||||
|
||||
for (k = p - 2; k >= - 1; k--)
|
||||
{
|
||||
if (k == - 1)
|
||||
{
|
||||
break;
|
||||
}
|
||||
if (System.Math.Abs(e[k]) <= eps * (System.Math.Abs(s[k]) + System.Math.Abs(s[k + 1])))
|
||||
{
|
||||
e[k] = 0.0m;
|
||||
break;
|
||||
}
|
||||
}
|
||||
if (k == p - 2)
|
||||
{
|
||||
kase = 4;
|
||||
}
|
||||
else
|
||||
{
|
||||
int ks;
|
||||
for (ks = p - 1; ks >= k; ks--)
|
||||
{
|
||||
if (ks == k)
|
||||
{
|
||||
break;
|
||||
}
|
||||
decimal t = (ks != p?System.Math.Abs(e[ks]):0.0m) + (ks != k + 1?System.Math.Abs(e[ks - 1]):0.0m);
|
||||
if (System.Math.Abs(s[ks]) <= eps * t)
|
||||
{
|
||||
s[ks] = 0.0m;
|
||||
break;
|
||||
}
|
||||
}
|
||||
if (ks == k)
|
||||
{
|
||||
kase = 3;
|
||||
}
|
||||
else if (ks == p - 1)
|
||||
{
|
||||
kase = 1;
|
||||
}
|
||||
else
|
||||
{
|
||||
kase = 2;
|
||||
k = ks;
|
||||
}
|
||||
}
|
||||
k++;
|
||||
|
||||
// Perform the task indicated by kase.
|
||||
|
||||
switch (kase)
|
||||
{
|
||||
|
||||
|
||||
// Deflate negligible s(p).
|
||||
case 1:
|
||||
{
|
||||
decimal f = e[p - 2];
|
||||
e[p - 2] = 0.0m;
|
||||
for (int j = p - 2; j >= k; j--)
|
||||
{
|
||||
decimal t = Maths.Hypot(s[j], f);
|
||||
decimal cs = s[j] / t;
|
||||
decimal sn = f / t;
|
||||
s[j] = t;
|
||||
if (j != k)
|
||||
{
|
||||
f = (- sn) * e[j - 1];
|
||||
e[j - 1] = cs * e[j - 1];
|
||||
}
|
||||
if (wantv)
|
||||
{
|
||||
for (int i = 0; i < n; i++)
|
||||
{
|
||||
t = cs * V[i][j] + sn * V[i][p - 1];
|
||||
V[i][p - 1] = (- sn) * V[i][j] + cs * V[i][p - 1];
|
||||
V[i][j] = t;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
break;
|
||||
|
||||
// Split at negligible s(k).
|
||||
|
||||
|
||||
case 2:
|
||||
{
|
||||
decimal f = e[k - 1];
|
||||
e[k - 1] = 0.0m;
|
||||
for (int j = k; j < p; j++)
|
||||
{
|
||||
decimal t = Maths.Hypot(s[j], f);
|
||||
decimal cs = s[j] / t;
|
||||
decimal sn = f / t;
|
||||
s[j] = t;
|
||||
f = (- sn) * e[j];
|
||||
e[j] = cs * e[j];
|
||||
if (wantu)
|
||||
{
|
||||
for (int i = 0; i < m; i++)
|
||||
{
|
||||
t = cs * U[i][j] + sn * U[i][k - 1];
|
||||
U[i][k - 1] = (- sn) * U[i][j] + cs * U[i][k - 1];
|
||||
U[i][j] = t;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
break;
|
||||
|
||||
// Perform one qr step.
|
||||
|
||||
|
||||
case 3:
|
||||
{
|
||||
// Calculate the shift.
|
||||
|
||||
decimal scale = System.Math.Max(System.Math.Max(System.Math.Max(System.Math.Max(System.Math.Abs(s[p - 1]), System.Math.Abs(s[p - 2])), System.Math.Abs(e[p - 2])), System.Math.Abs(s[k])), System.Math.Abs(e[k]));
|
||||
decimal sp = s[p - 1] / scale;
|
||||
decimal spm1 = s[p - 2] / scale;
|
||||
decimal epm1 = e[p - 2] / scale;
|
||||
decimal sk = s[k] / scale;
|
||||
decimal ek = e[k] / scale;
|
||||
decimal b = ((spm1 + sp) * (spm1 - sp) + epm1 * epm1) / 2.0m;
|
||||
decimal c = (sp * epm1) * (sp * epm1);
|
||||
decimal shift = 0.0m;
|
||||
if ((b != 0.0m) | (c != 0.0m))
|
||||
{
|
||||
shift = (decimal)DecimalMath.Sqrt(b * b + c);
|
||||
if (b < 0.0m)
|
||||
{
|
||||
shift = - shift;
|
||||
}
|
||||
shift = c / (b + shift);
|
||||
}
|
||||
decimal f = (sk + sp) * (sk - sp) + shift;
|
||||
decimal g = sk * ek;
|
||||
|
||||
// Chase zeros.
|
||||
|
||||
for (int j = k; j < p - 1; j++)
|
||||
{
|
||||
decimal t = Maths.Hypot(f, g);
|
||||
decimal cs = f / t;
|
||||
decimal sn = g / t;
|
||||
if (j != k)
|
||||
{
|
||||
e[j - 1] = t;
|
||||
}
|
||||
f = cs * s[j] + sn * e[j];
|
||||
e[j] = cs * e[j] - sn * s[j];
|
||||
g = sn * s[j + 1];
|
||||
s[j + 1] = cs * s[j + 1];
|
||||
if (wantv)
|
||||
{
|
||||
for (int i = 0; i < n; i++)
|
||||
{
|
||||
t = cs * V[i][j] + sn * V[i][j + 1];
|
||||
V[i][j + 1] = (- sn) * V[i][j] + cs * V[i][j + 1];
|
||||
V[i][j] = t;
|
||||
}
|
||||
}
|
||||
t = Maths.Hypot(f, g);
|
||||
cs = f / t;
|
||||
sn = g / t;
|
||||
s[j] = t;
|
||||
f = cs * e[j] + sn * s[j + 1];
|
||||
s[j + 1] = (- sn) * e[j] + cs * s[j + 1];
|
||||
g = sn * e[j + 1];
|
||||
e[j + 1] = cs * e[j + 1];
|
||||
if (wantu && (j < m - 1))
|
||||
{
|
||||
for (int i = 0; i < m; i++)
|
||||
{
|
||||
t = cs * U[i][j] + sn * U[i][j + 1];
|
||||
U[i][j + 1] = (- sn) * U[i][j] + cs * U[i][j + 1];
|
||||
U[i][j] = t;
|
||||
}
|
||||
}
|
||||
}
|
||||
e[p - 2] = f;
|
||||
iter = iter + 1;
|
||||
}
|
||||
break;
|
||||
|
||||
// Convergence.
|
||||
|
||||
|
||||
case 4:
|
||||
{
|
||||
// Make the singular values positive.
|
||||
|
||||
if (s[k] <= 0.0m)
|
||||
{
|
||||
s[k] = (s[k] < 0.0m?- s[k]:0.0m);
|
||||
if (wantv)
|
||||
{
|
||||
for (int i = 0; i <= pp; i++)
|
||||
{
|
||||
V[i][k] = - V[i][k];
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Order the singular values.
|
||||
|
||||
while (k < pp)
|
||||
{
|
||||
if (s[k] >= s[k + 1])
|
||||
{
|
||||
break;
|
||||
}
|
||||
decimal t = s[k];
|
||||
s[k] = s[k + 1];
|
||||
s[k + 1] = t;
|
||||
if (wantv && (k < n - 1))
|
||||
{
|
||||
for (int i = 0; i < n; i++)
|
||||
{
|
||||
t = V[i][k + 1]; V[i][k + 1] = V[i][k]; V[i][k] = t;
|
||||
}
|
||||
}
|
||||
if (wantu && (k < m - 1))
|
||||
{
|
||||
for (int i = 0; i < m; i++)
|
||||
{
|
||||
t = U[i][k + 1]; U[i][k + 1] = U[i][k]; U[i][k] = t;
|
||||
}
|
||||
}
|
||||
k++;
|
||||
}
|
||||
iter = 0;
|
||||
p--;
|
||||
}
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
#endregion //Constructor
|
||||
|
||||
#region Public Properties
|
||||
/// <summary>Return the one-dimensional array of singular values</summary>
|
||||
/// <returns> diagonal of S.
|
||||
/// </returns>
|
||||
virtual public decimal[] SingularValues
|
||||
{
|
||||
get
|
||||
{
|
||||
return s;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>Return the diagonal matrix of singular values</summary>
|
||||
/// <returns> S
|
||||
/// </returns>
|
||||
virtual public GeneralMatrix S
|
||||
{
|
||||
get
|
||||
{
|
||||
GeneralMatrix X = new GeneralMatrix(n, n);
|
||||
decimal[][] S = X.Array;
|
||||
for (int i = 0; i < n; i++)
|
||||
{
|
||||
for (int j = 0; j < n; j++)
|
||||
{
|
||||
S[i][j] = 0.0m;
|
||||
}
|
||||
S[i][i] = this.s[i];
|
||||
}
|
||||
return X;
|
||||
}
|
||||
}
|
||||
#endregion // Public Properties
|
||||
|
||||
#region Public Methods
|
||||
|
||||
/// <summary>Return the left singular vectors</summary>
|
||||
/// <returns> U
|
||||
/// </returns>
|
||||
|
||||
public virtual GeneralMatrix GetU()
|
||||
{
|
||||
return new GeneralMatrix(U, m, System.Math.Min(m + 1, n));
|
||||
}
|
||||
|
||||
/// <summary>Return the right singular vectors</summary>
|
||||
/// <returns> V
|
||||
/// </returns>
|
||||
|
||||
public virtual GeneralMatrix GetV()
|
||||
{
|
||||
return new GeneralMatrix(V, n, n);
|
||||
}
|
||||
|
||||
/// <summary>Two norm</summary>
|
||||
/// <returns> max(S)
|
||||
/// </returns>
|
||||
|
||||
public virtual decimal Norm2()
|
||||
{
|
||||
return s[0];
|
||||
}
|
||||
|
||||
/// <summary>Two norm condition number</summary>
|
||||
/// <returns> max(S)/min(S)
|
||||
/// </returns>
|
||||
|
||||
public virtual decimal Condition()
|
||||
{
|
||||
return s[0] / s[System.Math.Min(m, n) - 1];
|
||||
}
|
||||
|
||||
/// <summary>Effective numerical matrix rank</summary>
|
||||
/// <returns> Number of nonnegligible singular values.
|
||||
/// </returns>
|
||||
|
||||
public virtual int Rank()
|
||||
{
|
||||
decimal eps = (decimal)System.Math.Pow(2.0f, - 52.0f);
|
||||
decimal tol = System.Math.Max(m, n) * s[0] * eps;
|
||||
int r = 0;
|
||||
for (int i = 0; i < s.Length; i++)
|
||||
{
|
||||
if (s[i] > tol)
|
||||
{
|
||||
r++;
|
||||
}
|
||||
}
|
||||
return r;
|
||||
}
|
||||
#endregion //Public Methods
|
||||
|
||||
// A method called when serializing this class.
|
||||
void ISerializable.GetObjectData(SerializationInfo info, StreamingContext context)
|
||||
{
|
||||
}
|
||||
}
|
||||
}
|
||||
11
Assets/Scripts/Matrix/SingularValueDecomposition.cs.meta
Normal file
11
Assets/Scripts/Matrix/SingularValueDecomposition.cs.meta
Normal file
|
|
@ -0,0 +1,11 @@
|
|||
fileFormatVersion: 2
|
||||
guid: 834d344a2e4c6e0468fcd799e751ff9b
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
621
Assets/Scripts/NumericalMethods.cs
Normal file
621
Assets/Scripts/NumericalMethods.cs
Normal file
|
|
@ -0,0 +1,621 @@
|
|||
using DotNetMatrix;
|
||||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
using System.Globalization;
|
||||
using System.Text;
|
||||
using UnityEngine;
|
||||
|
||||
public class NumericalMethods
|
||||
{
|
||||
#region GaussSeidal Yap Burayı
|
||||
/*public static List<decimal> Method_GaussSeidal(List<string> _eq, Dictionary<string, decimal> initialValues, int iterationCount)
|
||||
{
|
||||
//yakınsama şartı (3x3 matris için) -> |a11|>|a12|+|a13| && |a22|>|a21|+|a23| && |a33|>|a31|+|a32|
|
||||
|
||||
bool convergence = true;
|
||||
List<int> dominantTerms = new List<int>();
|
||||
for (int i = 0; i < _eq.Count; i++)
|
||||
{
|
||||
int currentDominant = -1;
|
||||
bool dominantFound = false;
|
||||
for (int j = 0; j < _eq.Count; j++)
|
||||
{
|
||||
decimal sum0 = 0;
|
||||
|
||||
for (int k = 0; k < _eq.Count; k++)
|
||||
{
|
||||
if (k != j)
|
||||
{
|
||||
sum0 += Mathf.Abs(_eq[i].Coefficients[k]);
|
||||
}
|
||||
}
|
||||
|
||||
if (Mathf.Abs(_eq[i].Coefficients[j]) > sum0)
|
||||
{
|
||||
currentDominant = j;
|
||||
dominantFound = true;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
if (dominantFound)
|
||||
{
|
||||
dominantTerms.Add(currentDominant);
|
||||
}
|
||||
else
|
||||
{
|
||||
dominantTerms.Add(-1);
|
||||
}
|
||||
}
|
||||
|
||||
for (int i = 0; i < dominantTerms.Count && convergence; i++)
|
||||
{
|
||||
for (int j = 0; j < dominantTerms.Count; j++)
|
||||
{
|
||||
if (i != j)
|
||||
{
|
||||
if (dominantTerms[i] == dominantTerms[j])
|
||||
{
|
||||
convergence = false;
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (convergence == false)
|
||||
{
|
||||
Debug.LogError("This matris cannot be solved with Gauss-Seidal Method!");
|
||||
return new List<decimal>();
|
||||
}
|
||||
else
|
||||
{
|
||||
List<decimal> answers = initialValues;
|
||||
for (int i = 0; i < iterationCount; i++)
|
||||
{
|
||||
for (int j = 0; j < _eq.Count; j++)
|
||||
{
|
||||
decimal sum = _eq[j].Constant + _eq[j].Coefficients[j] * answers[j];
|
||||
for (int k = 0; k < _eq.Count; k++)
|
||||
{
|
||||
sum += -_eq[j].Coefficients[k] * answers[k];
|
||||
}
|
||||
answers[j] = sum / _eq[j].Coefficients[j];
|
||||
}
|
||||
}
|
||||
string ansdeb = "";
|
||||
for (int i = 0; i < answers.Count; i++)
|
||||
{
|
||||
ansdeb += answers[i] + ((i == answers.Count - 1) ? "" : ", ");
|
||||
}
|
||||
Debug.Log(ansdeb);
|
||||
|
||||
return answers;
|
||||
}
|
||||
}*/
|
||||
#endregion
|
||||
|
||||
|
||||
public static decimal Method_EvaluateFunction(string eq, string variable, decimal point)
|
||||
{
|
||||
string newEq = eq;
|
||||
|
||||
newEq = newEq.Replace(variable, point.ToString(/*new CultureInfo("en-US")*/));
|
||||
|
||||
Function f = new Function();
|
||||
f.Parse(newEq);
|
||||
f.Infix2Postfix();
|
||||
f.EvaluatePostfix();
|
||||
return f.m_result;
|
||||
}
|
||||
public static decimal MethodMultiVar_EvaluateFunction(string eq, Dictionary<string, decimal> points)
|
||||
{
|
||||
string newEq = eq;
|
||||
foreach (KeyValuePair<string, decimal> pt in points)
|
||||
{
|
||||
newEq = newEq.Replace(pt.Key, pt.Value.ToString(/*new CultureInfo("en-US")*/));
|
||||
}
|
||||
//Debug.Log(newEq);
|
||||
|
||||
Function f = new Function();
|
||||
f.Parse(newEq);
|
||||
f.Infix2Postfix();
|
||||
f.EvaluatePostfix();
|
||||
//Debug.Log(f.ErrorDescription);
|
||||
//Debug.Log(Math.Round(f.m_result, 5));
|
||||
return f.m_result;
|
||||
|
||||
}
|
||||
|
||||
|
||||
public static Dictionary<string, decimal> MethodMultiVar_SolveEquationSystem(List<string> eqs, Dictionary<string, decimal> initPoints, int iterationCount = 5, decimal h = 0.001m)
|
||||
{
|
||||
Dictionary<string, decimal> dynamicPoints = initPoints;
|
||||
for (int itC = 0; itC < iterationCount; itC++)
|
||||
{
|
||||
List<string> linearizedEqs = new List<string>(), variablesStr = new List<string>();
|
||||
List<decimal> leftVector = new List<decimal>();
|
||||
for (int i = 0; i < eqs.Count; i++)
|
||||
{
|
||||
string le = MethodMultiVar_TaylorSeries(eqs[i], 1, dynamicPoints, h);
|
||||
|
||||
int charCount = 0;
|
||||
for (int j = 0; j < le.Length; j++)
|
||||
{
|
||||
if ((le[j] == '+' || le[j] == '-') && j != 0) break;
|
||||
if (le[j] == '*')
|
||||
{
|
||||
charCount = 0;
|
||||
break;
|
||||
}
|
||||
charCount++;
|
||||
if (j == le.Length - 1 && charCount != 0) charCount = 0;
|
||||
}
|
||||
|
||||
//Debug.Log(le + " | " + charCount);
|
||||
|
||||
decimal leftV = charCount != 0 ? decimal.Parse(le.Substring(0, charCount)) * -1 : 0;
|
||||
|
||||
|
||||
string sum = "";
|
||||
foreach (KeyValuePair<string, decimal> p in dynamicPoints)
|
||||
{
|
||||
sum += p.Key + ": " + p.Value.ToString("0." + new string('#', 339)) + ", ";
|
||||
}
|
||||
|
||||
//Debug.Log(le + " | " + charCount + " | " + sum + " | " + leftV);
|
||||
|
||||
le = le.Remove(0, le[charCount] == '-' ? charCount : charCount + 1);
|
||||
|
||||
|
||||
foreach (KeyValuePair<string, decimal> p in dynamicPoints)
|
||||
{
|
||||
le = le.Replace("(" + p.Key + (p.Value < 0 ? "+" + DecimalMath.Abs(p.Value).ToString("0." + new string('#', 339)) : "-" + p.Value.ToString("0." + new string('#', 339))) + ")", p.Key);
|
||||
}
|
||||
//Debug.Log(le);
|
||||
|
||||
|
||||
linearizedEqs.Add(le);
|
||||
leftVector.Add(leftV);
|
||||
}
|
||||
|
||||
foreach (KeyValuePair<string, decimal> p in dynamicPoints)
|
||||
{
|
||||
variablesStr.Add(p.Key);
|
||||
}
|
||||
|
||||
decimal[][] jacobianMatrix = Matrix.ConvertLinearEquationsToMatrix(linearizedEqs, variablesStr);
|
||||
decimal[] leftHandVector = leftVector.ToArray();
|
||||
|
||||
decimal[] newPoints = Matrix.SolveEquation(jacobianMatrix, leftHandVector);
|
||||
|
||||
|
||||
List<decimal> newPointsList = new List<decimal>();
|
||||
|
||||
for (int i = 0; i < newPoints.Length; i++)
|
||||
{
|
||||
newPointsList.Add(newPoints[i]);
|
||||
}
|
||||
|
||||
Dictionary<string, decimal> tempDynamicPoints = new Dictionary<string, decimal>();
|
||||
|
||||
foreach (KeyValuePair<string, decimal> p in dynamicPoints)
|
||||
{
|
||||
tempDynamicPoints.Add(p.Key, p.Value);
|
||||
}
|
||||
|
||||
foreach (KeyValuePair<string, decimal> p in dynamicPoints)
|
||||
{
|
||||
tempDynamicPoints[p.Key] = newPointsList[0] + p.Value;
|
||||
newPointsList.RemoveAt(0);
|
||||
}
|
||||
dynamicPoints = tempDynamicPoints;
|
||||
}
|
||||
|
||||
return dynamicPoints;
|
||||
}
|
||||
|
||||
|
||||
public static string Method_TaylorSeries(string eq, int degree, string variable, decimal point, decimal h = 0.001m)
|
||||
{
|
||||
decimal constant = Method_EvaluateFunction(eq, variable, point);
|
||||
constant = DecimalMath.Abs(constant) <= 0.001m ? 0 : constant;
|
||||
string newEq = constant != 0 ? constant.ToString("0." + new string('#', 339)) : "";
|
||||
for (int i = 1; i < degree + 1; i++)
|
||||
{
|
||||
//decimal der = Method_Derivative(eq, i, variable, point);
|
||||
decimal der = Method_NumericalDifferentiation(eq, i, variable, point, 4, h);
|
||||
decimal fact = Factorial(i);
|
||||
decimal coeff = (der / fact);
|
||||
string varSTR = "";
|
||||
if (coeff > 0) varSTR = "+";
|
||||
if (coeff == 0) continue;
|
||||
|
||||
string addingSTR = "";
|
||||
if (point < 0)
|
||||
{
|
||||
addingSTR = "+" + DecimalMath.Abs(point).ToString("0." + new string('#', 339));
|
||||
}
|
||||
else if (point > 0)
|
||||
{
|
||||
addingSTR = "-" + point.ToString("0." + new string('#', 339));
|
||||
}
|
||||
|
||||
varSTR += coeff + "*(" + variable + addingSTR + (i == 1 ? ")" : ")^" + i.ToString("0." + new string('#', 339)) + "");
|
||||
newEq += varSTR;
|
||||
}
|
||||
return newEq;
|
||||
}
|
||||
/*public static string Method_TaylorSeries2(string eq, int degree, string variable, decimal point)
|
||||
{
|
||||
decimal constant = Method_EvaluateFunction(eq, variable, point);
|
||||
constant = DecimalMath.Abs(constant) <= 0.0001m ? 0 : constant;
|
||||
|
||||
string newEq = constant.ToString("0." + new string('#', 339));
|
||||
for (int i = 1; i < degree + 1; i++)
|
||||
{
|
||||
decimal der = Method_Derivative(eq, i, variable, point);
|
||||
//Debug.Log("derivative at " + i + ": " + der.ToString());
|
||||
if (DecimalMath.Abs(der) <= 0.0001m)
|
||||
{
|
||||
der = 0;
|
||||
Debug.Log(der + ": " + 0);
|
||||
}
|
||||
decimal fact = Factorial(i);
|
||||
decimal coeff = (der / fact);
|
||||
|
||||
string varSTR = "";
|
||||
if (coeff > 0) varSTR = "+";
|
||||
if (coeff == 0) continue;
|
||||
|
||||
string addingSTR = "";
|
||||
if (point < 0)
|
||||
{
|
||||
addingSTR = "+" + DecimalMath.Abs(point).ToString("0." + new string('#', 339));
|
||||
}
|
||||
else if (point > 0)
|
||||
{
|
||||
addingSTR = "-" + point.ToString("0." + new string('#', 339));
|
||||
}
|
||||
|
||||
varSTR += coeff + "*(" + variable + addingSTR + (i == 1 ? ")" : ")^" + i.ToString("0." + new string('#', 339)));
|
||||
newEq += varSTR;
|
||||
}
|
||||
return newEq;
|
||||
}*/
|
||||
public static string MethodMultiVar_TaylorSeries(string eq, int degree, Dictionary<string, decimal> points, decimal h = 0.001m)
|
||||
{
|
||||
decimal constant = MethodMultiVar_EvaluateFunction(eq, points);
|
||||
constant = DecimalMath.Abs(constant) <= 0.0001m ? 0 : constant;
|
||||
string newEq = constant != 0 ? constant.ToString("0." + new string('#', 339)) : "";
|
||||
for (int i = 1; i < degree + 1; i++)
|
||||
{
|
||||
foreach (KeyValuePair<string, decimal> pt in points)
|
||||
{
|
||||
decimal partDer = Method_NumericalPartialDifferentiation(eq, i, pt.Key, points, 4, h);
|
||||
//decimal partDer = Method_PartialDerivative(eq, i, pt.Key, points, h);
|
||||
decimal fact = Factorial(i);
|
||||
decimal coeff = partDer / fact;
|
||||
string varSTR = "";
|
||||
if (coeff > 0) varSTR = "+";
|
||||
if (coeff == 0) continue;
|
||||
|
||||
string addingSTR = "";
|
||||
if (pt.Value < 0)
|
||||
{
|
||||
addingSTR = "+" + DecimalMath.Abs(pt.Value).ToString("0." + new string('#', 339));
|
||||
}
|
||||
else if (pt.Value > 0)
|
||||
{
|
||||
addingSTR = "-" + pt.Value.ToString("0." + new string('#', 339));
|
||||
}
|
||||
varSTR += coeff + "*(" + pt.Key + addingSTR + (i == 1 ? ")" : ")^" + i.ToString("0." + new string('#', 339)));
|
||||
newEq += varSTR;
|
||||
}
|
||||
}
|
||||
//Debug.Log(newEq + " + " + constant + " | " + eq);
|
||||
if (constant == 0) newEq.Substring(0, 1);
|
||||
return newEq;
|
||||
}
|
||||
|
||||
|
||||
public static decimal Method_NewtonRaphson(string eq, string variable, decimal initialValue, int iterationCount, decimal h = 0.001m)
|
||||
{
|
||||
decimal Lipschitz = Method_EvaluateFunction(eq, variable, initialValue) * Method_NumericalDifferentiation(eq, 2, variable, initialValue, 6, h) /
|
||||
Method_NumericalDifferentiation(eq, 1, variable, initialValue, 6, h) / Method_NumericalDifferentiation(eq, 1, variable, initialValue, 6, h);
|
||||
|
||||
if (Lipschitz < 1) { }
|
||||
else
|
||||
{
|
||||
Debug.LogError("Wrong initial value!");
|
||||
return 0;
|
||||
}
|
||||
|
||||
decimal init = initialValue;
|
||||
for (int i = 0; i < iterationCount; i++)
|
||||
{
|
||||
//decimal next = init - Method_EvaluateFunction(eq, variable, init) / Method_Derivative(eq, 1, variable, init, h);
|
||||
decimal next = init - Method_EvaluateFunction(eq, variable, init) / Method_NumericalDifferentiation(eq, 1, variable, init, 6, h);
|
||||
init = next;
|
||||
}
|
||||
return init;
|
||||
}
|
||||
public static decimal Method_BisectionMethod(string eq, string variable, decimal lowerValue, decimal higherValue, int iterationCount)
|
||||
{
|
||||
if (lowerValue >= higherValue)
|
||||
return 0;
|
||||
|
||||
decimal mp = (lowerValue + higherValue) / 2;
|
||||
|
||||
for (int i = 0; i < iterationCount; i++)
|
||||
{
|
||||
mp = (lowerValue + higherValue) / 2;
|
||||
|
||||
decimal evLower = Method_EvaluateFunction(eq, variable, lowerValue);
|
||||
decimal evHigher = Method_EvaluateFunction(eq, variable, higherValue);
|
||||
decimal evMiddle = Method_EvaluateFunction(eq, variable, mp);
|
||||
|
||||
if (evLower * evMiddle < 0)
|
||||
{
|
||||
higherValue = mp;
|
||||
}
|
||||
else
|
||||
{
|
||||
lowerValue = mp;
|
||||
}
|
||||
}
|
||||
|
||||
return mp;
|
||||
}
|
||||
|
||||
|
||||
public static decimal Method_Derivative(string eq, int degree, string variable, decimal point, decimal h = 0.001m)
|
||||
{
|
||||
decimal sum = 0;
|
||||
for (int i = 0; i <= degree; i++)
|
||||
{
|
||||
sum += DecimalMath.Pow(-1, i) * Combination(degree, i) * Method_EvaluateFunction(eq, variable, point + (degree - i) * h);
|
||||
//Debug.Log(sum);
|
||||
}
|
||||
|
||||
decimal res = sum / DecimalMath.Pow(h, degree);
|
||||
|
||||
//Debug.Log(sum.ToString("0." + new string('#', 339)) + " || " + Math.Pow((double)h, degree).ToString() + " | " + Math.Pow((double)h, degree).ToString("0." + new string('#', 339)));
|
||||
//Debug.Log(res + " || " + sum.ToString("0." + new string('#', 339)) + " || " + DecimalMath.Pow(h, degree).ToString() + " | " + DecimalMath.Pow(h, degree).ToString("0." + new string('#', 339)));
|
||||
|
||||
return res;
|
||||
}
|
||||
public static decimal Method_PartialDerivative(string eq, int degree, string toVar, Dictionary<string, decimal> points, decimal h = 0.001m)
|
||||
{
|
||||
decimal sum = 0;
|
||||
Dictionary<string, decimal> testPS = new Dictionary<string, decimal>();
|
||||
foreach (KeyValuePair<string, decimal> item in points)
|
||||
{
|
||||
testPS.Add(item.Key, item.Value);
|
||||
}
|
||||
decimal firstToVar = testPS[toVar];
|
||||
for (int i = 0; i <= degree; i++)
|
||||
{
|
||||
testPS[toVar] = firstToVar + (degree - i) * h;
|
||||
sum += DecimalMath.Pow(-1, i) * Combination(degree, i) * MethodMultiVar_EvaluateFunction(eq, testPS);
|
||||
}
|
||||
decimal res = sum / DecimalMath.Pow(h, degree);
|
||||
return res;
|
||||
}
|
||||
|
||||
|
||||
public static decimal Method_NumericalDifferentiation(string eq, int degree, string variable, decimal point, int accuracy = 2, decimal h = 0.001m)
|
||||
{
|
||||
decimal result = 0;
|
||||
|
||||
int m = degree, n = accuracy;
|
||||
int p = (2 * Mathf.FloorToInt((m + 1) / 2) - 2 + n) / 2;
|
||||
|
||||
decimal[][] _matrix = new decimal[2 * p + 1][];
|
||||
decimal[] _rhsVector = new decimal[2 * p + 1], functionPointsX = new decimal[2 * p + 1],
|
||||
functionPointsY = new decimal[2 * p + 1];
|
||||
|
||||
|
||||
for (int i = 0; i < _matrix.Length; i++)
|
||||
{
|
||||
_matrix[i] = new decimal[2 * p + 1];
|
||||
for (int j = 0; j < _matrix[i].Length; j++)
|
||||
{
|
||||
_matrix[i][j] = (decimal)Mathf.Pow(-p + j, i);
|
||||
}
|
||||
|
||||
|
||||
if (i != m)
|
||||
{
|
||||
_rhsVector[i] = 0;
|
||||
}
|
||||
else if (i == m)
|
||||
{
|
||||
_rhsVector[i] = Factorial(m);
|
||||
}
|
||||
|
||||
|
||||
functionPointsX[i] = point - (p - i) * h;
|
||||
functionPointsY[i] = Method_EvaluateFunction(eq, variable, functionPointsX[i]);
|
||||
}
|
||||
|
||||
GeneralMatrix _A = new GeneralMatrix(_matrix);
|
||||
GeneralMatrix _b = new GeneralMatrix(_rhsVector.Length, 1);
|
||||
|
||||
for (int i = 0; i < _rhsVector.Length; i++)
|
||||
{
|
||||
_b.Array[i][0] = _rhsVector[i];
|
||||
}
|
||||
|
||||
//decimal[] coeffs = Matrix.SolveEquation(_matrix, _rhsVector);
|
||||
GeneralMatrix coeffs = _A.Solve(_b);
|
||||
|
||||
decimal sum = 0;
|
||||
|
||||
for (int i = 0; i < coeffs.Array.Length; i++)
|
||||
{
|
||||
sum += coeffs.Array[i][0] * functionPointsY[i];
|
||||
}
|
||||
|
||||
result = sum / DecimalMath.Pow(h, degree);
|
||||
|
||||
return result;
|
||||
}
|
||||
public static decimal Method_NumericalPartialDifferentiation(string eq, int degree, string toVar, Dictionary<string, decimal> points, int accuracy = 2, decimal h = 0.001m)
|
||||
{
|
||||
decimal result = 0;
|
||||
|
||||
int m = degree, n = accuracy;
|
||||
int p = (2 * Mathf.FloorToInt((m + 1) / 2) - 2 + n) / 2;
|
||||
|
||||
decimal[][] _matrix = new decimal[2 * p + 1][];
|
||||
Dictionary<string, decimal[]> functionPointsDomain = new Dictionary<string, decimal[]>();
|
||||
decimal[] _rhsVector = new decimal[2 * p + 1], functionPointsRange = new decimal[2 * p + 1];
|
||||
|
||||
|
||||
for (int i = 0; i < _matrix.Length; i++)
|
||||
{
|
||||
_matrix[i] = new decimal[2 * p + 1];
|
||||
for (int j = 0; j < _matrix[i].Length; j++)
|
||||
{
|
||||
_matrix[i][j] = (decimal)Mathf.Pow(-p + j, i);
|
||||
}
|
||||
|
||||
if (i != m)
|
||||
{
|
||||
_rhsVector[i] = 0;
|
||||
}
|
||||
else if (i == m)
|
||||
{
|
||||
_rhsVector[i] = Factorial(m);
|
||||
}
|
||||
}
|
||||
|
||||
foreach (KeyValuePair<string, decimal> item in points)
|
||||
{
|
||||
decimal[] ps = new decimal[2 * p + 1];
|
||||
|
||||
if (item.Key == toVar)
|
||||
{
|
||||
for (int i = 0; i < ps.Length; i++)
|
||||
{
|
||||
ps[i] = points[item.Key] - (p - i) * h;
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
for (int i = 0; i < ps.Length; i++)
|
||||
{
|
||||
ps[i] = points[item.Key];
|
||||
}
|
||||
}
|
||||
|
||||
functionPointsDomain.Add(item.Key, ps);
|
||||
}
|
||||
|
||||
|
||||
for (int i = 0; i < 2 * p + 1; i++)
|
||||
{
|
||||
Dictionary<string, decimal> pair = new Dictionary<string, decimal>();
|
||||
|
||||
foreach (KeyValuePair<string, decimal[]> item in functionPointsDomain)
|
||||
{
|
||||
pair.Add(item.Key, item.Value[i]);
|
||||
}
|
||||
|
||||
functionPointsRange[i] = MethodMultiVar_EvaluateFunction(eq, pair);
|
||||
}
|
||||
|
||||
|
||||
GeneralMatrix _A = new GeneralMatrix(_matrix);
|
||||
GeneralMatrix _b = new GeneralMatrix(_rhsVector.Length, 1);
|
||||
|
||||
for (int i = 0; i < _rhsVector.Length; i++)
|
||||
{
|
||||
_b.Array[i][0] = _rhsVector[i];
|
||||
}
|
||||
|
||||
//decimal[] coeffs = Matrix.SolveEquation(_matrix, _rhsVector);
|
||||
GeneralMatrix coeffs = _A.Solve(_b);
|
||||
|
||||
decimal sum = 0;
|
||||
|
||||
for (int i = 0; i < coeffs.Array.Length; i++)
|
||||
{
|
||||
sum += coeffs.Array[i][0] * functionPointsRange[i];
|
||||
}
|
||||
|
||||
result = sum / DecimalMath.Pow(h, degree);
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
#region testPow
|
||||
/*public static decimal Pow(decimal x, uint y)
|
||||
{
|
||||
decimal A = 1m;
|
||||
BitArray e = new BitArray(BitConverter.GetBytes(y));
|
||||
int t = e.Count;
|
||||
|
||||
for (int i = t - 1; i >= 0; --i)
|
||||
{
|
||||
A *= A;
|
||||
if (e[i] == true)
|
||||
{
|
||||
A *= x;
|
||||
}
|
||||
}
|
||||
return A;
|
||||
}*/
|
||||
#endregion
|
||||
|
||||
public static int Factorial(int num)
|
||||
{
|
||||
if (num >= 2)
|
||||
{
|
||||
return num * Factorial(num -1);
|
||||
}
|
||||
else
|
||||
{
|
||||
return 1;
|
||||
}
|
||||
}
|
||||
public static int Combination(int n, int k)
|
||||
{
|
||||
int top = Factorial(n);
|
||||
int bot1 = Factorial(n - k);
|
||||
int bot2 = Factorial(k);
|
||||
|
||||
return top / bot1 / bot2;
|
||||
}
|
||||
public static int Permutation(int n, int k)
|
||||
{
|
||||
int top = Factorial(n);
|
||||
int bot = Factorial(n - k);
|
||||
|
||||
return top / bot;
|
||||
}
|
||||
|
||||
public static string GenerateCombinations(string chars, string prefix, int length)
|
||||
{
|
||||
StringBuilder sb = new StringBuilder();
|
||||
|
||||
GenerateCombinationsRecursive(prefix, chars, length, sb);
|
||||
|
||||
return sb.ToString();
|
||||
}
|
||||
static void GenerateCombinationsRecursive(string prefix, string chars, int length, StringBuilder sb)
|
||||
{
|
||||
if (length == 0)
|
||||
{
|
||||
sb.AppendLine(prefix);
|
||||
return;
|
||||
}
|
||||
|
||||
foreach (char c in chars)
|
||||
{
|
||||
GenerateCombinationsRecursive(prefix + c, chars, length - 1, sb);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
11
Assets/Scripts/NumericalMethods.cs.meta
Normal file
11
Assets/Scripts/NumericalMethods.cs.meta
Normal file
|
|
@ -0,0 +1,11 @@
|
|||
fileFormatVersion: 2
|
||||
guid: eabb80de3b87ba64497fe5d6b0644c47
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
8
Assets/Scripts/Old.meta
Normal file
8
Assets/Scripts/Old.meta
Normal file
|
|
@ -0,0 +1,8 @@
|
|||
fileFormatVersion: 2
|
||||
guid: 82b9a4758390a7b44822597c7c1b24a4
|
||||
folderAsset: yes
|
||||
DefaultImporter:
|
||||
externalObjects: {}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
158
Assets/Scripts/Old/EquationSystem.cs
Normal file
158
Assets/Scripts/Old/EquationSystem.cs
Normal file
|
|
@ -0,0 +1,158 @@
|
|||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
using UnityEngine;
|
||||
|
||||
public class EquationSystem : MonoBehaviour
|
||||
{
|
||||
public static List<Equation> Equations = new List<Equation>();
|
||||
public static int VarCount = 3;
|
||||
}
|
||||
|
||||
public class Equation
|
||||
{
|
||||
public string EquationString = "";
|
||||
public bool MultiVariable = false;
|
||||
|
||||
public List<float> Coefficients = new List<float>();
|
||||
public List<string> Variables = new List<string>();
|
||||
public float Constant;
|
||||
public List<Operation> Operations = new List<Operation>();
|
||||
|
||||
public Equation(List<float> coeffs, float cons, List<string> vars = null, bool multiVar = false)
|
||||
{
|
||||
Coefficients = coeffs;
|
||||
Variables = vars;
|
||||
Constant = cons;
|
||||
MultiVariable = multiVar;
|
||||
EquationString = ConvertEquationToString();
|
||||
}
|
||||
public Equation(string eq)
|
||||
{
|
||||
List<float> coeffs = new List<float>();
|
||||
float cons = 0;
|
||||
string testSTR = "";
|
||||
for (int i = 0; i < eq.Length; i++)
|
||||
{
|
||||
if (eq[i] == '+' || eq[i] == '-')
|
||||
{
|
||||
testSTR = "";
|
||||
}
|
||||
if (eq[i] == '*')
|
||||
{
|
||||
coeffs.Add(float.Parse(testSTR));
|
||||
testSTR = "";
|
||||
}
|
||||
testSTR += eq[i];
|
||||
}
|
||||
|
||||
char op = 'a';
|
||||
testSTR = "";
|
||||
for (int i = eq.Length - 1; i >= 0; i--)
|
||||
{
|
||||
if (eq[i] == '+' || eq[i] == '-')
|
||||
{
|
||||
op = eq[i];
|
||||
string newstr = "";
|
||||
for (int j = testSTR.Length - 1; j >= 0; j--)
|
||||
{
|
||||
newstr += testSTR[j];
|
||||
}
|
||||
testSTR = op + newstr;
|
||||
cons = (testSTR.Length > 0) ? float.Parse(testSTR) : 0f;
|
||||
break;
|
||||
}
|
||||
testSTR += eq[i];
|
||||
}
|
||||
|
||||
Coefficients = coeffs;
|
||||
Constant = cons;
|
||||
Variables = null;
|
||||
MultiVariable = false;
|
||||
}
|
||||
|
||||
public string ConvertEquationToString()
|
||||
{
|
||||
string newEq = "";
|
||||
for(int i = 0; i < Coefficients.Count; i++)
|
||||
{
|
||||
if (i == Coefficients.Count - 1)
|
||||
{
|
||||
if (Coefficients[i] > 0)
|
||||
{
|
||||
newEq += "+" + Coefficients[i].ToString() + "*x";
|
||||
}
|
||||
else if (Coefficients[i] < 0)
|
||||
{
|
||||
newEq += Coefficients[i].ToString() + "*x";
|
||||
}
|
||||
else if (Coefficients[i] == 0)
|
||||
{
|
||||
continue;
|
||||
}
|
||||
}
|
||||
else if (i == 0)
|
||||
{
|
||||
if (Coefficients[i] != 0)
|
||||
{
|
||||
newEq += Coefficients[i].ToString() + "*x^" + (Coefficients.Count - i).ToString();
|
||||
}
|
||||
else
|
||||
{
|
||||
continue;
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
if (Coefficients[i] > 0)
|
||||
{
|
||||
newEq += "+" + Coefficients[i].ToString() + "*x^" + (Coefficients.Count - i).ToString();
|
||||
}
|
||||
else if (Coefficients[i] < 0)
|
||||
{
|
||||
newEq += Coefficients[i].ToString() + "*x^" + (Coefficients.Count - i).ToString();
|
||||
}
|
||||
else if (Coefficients[i] == 0)
|
||||
{
|
||||
continue;
|
||||
}
|
||||
}
|
||||
}
|
||||
if (Constant > 0)
|
||||
{
|
||||
newEq += "+" + Constant;
|
||||
}
|
||||
else if (Constant < 0)
|
||||
{
|
||||
newEq += Constant;
|
||||
}
|
||||
|
||||
string newSTR = "";
|
||||
for (int i = 0; i < newEq.Length; i++)
|
||||
{
|
||||
if (newEq[i] == ',')
|
||||
{
|
||||
newSTR += '.';
|
||||
}
|
||||
else
|
||||
{
|
||||
newSTR += newEq[i];
|
||||
}
|
||||
}
|
||||
newEq = newSTR;
|
||||
|
||||
return newEq;
|
||||
}
|
||||
|
||||
public void SubstituteEquation(Equation eq)
|
||||
{
|
||||
if (!MultiVariable)
|
||||
{
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public enum Operation
|
||||
{
|
||||
ADDITION, SUBTRACTION, MULTIPLICATION, DIVISION, EXPONENTIATION
|
||||
}
|
||||
11
Assets/Scripts/Old/EquationSystem.cs.meta
Normal file
11
Assets/Scripts/Old/EquationSystem.cs.meta
Normal file
|
|
@ -0,0 +1,11 @@
|
|||
fileFormatVersion: 2
|
||||
guid: c57eddf0ac8df46468098df8caebdc48
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
36
Assets/Scripts/Old/EquationSystem2.cs
Normal file
36
Assets/Scripts/Old/EquationSystem2.cs
Normal file
|
|
@ -0,0 +1,36 @@
|
|||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
using UnityEngine;
|
||||
|
||||
public class EquationSystem2 : MonoBehaviour
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
||||
public class Equation2
|
||||
{
|
||||
public static char[] AllVariables = new char[] { 'x', 'y', 'z', 't', 'w', 'r' };
|
||||
|
||||
public string EquationString = "";
|
||||
|
||||
public enum Type { Variable, Value, UnaryOperator, Operator, Function, Result, Bracket, Comma, Error }
|
||||
public struct Symbol
|
||||
{
|
||||
string SYM_Name;
|
||||
double SYM_Value;
|
||||
Type SYM_Type;
|
||||
public override string ToString()
|
||||
{
|
||||
return SYM_Name;
|
||||
}
|
||||
}
|
||||
|
||||
public ArrayList m_equation = new ArrayList();
|
||||
|
||||
public Equation2(string eq)
|
||||
{
|
||||
EquationString = eq;
|
||||
}
|
||||
}
|
||||
11
Assets/Scripts/Old/EquationSystem2.cs.meta
Normal file
11
Assets/Scripts/Old/EquationSystem2.cs.meta
Normal file
|
|
@ -0,0 +1,11 @@
|
|||
fileFormatVersion: 2
|
||||
guid: 86ef5945fb00cd74db15c2c37e803dce
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
306
Assets/Scripts/Old/FunctionEvaluator2.cs
Normal file
306
Assets/Scripts/Old/FunctionEvaluator2.cs
Normal file
|
|
@ -0,0 +1,306 @@
|
|||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
using UnityEngine;
|
||||
|
||||
public class FunctionEvaluator2 : MonoBehaviour
|
||||
{
|
||||
public List<string> Chars = new List<string>();
|
||||
List<char> Operators = new List<char> { '+', '-', '*', '/', '^' };
|
||||
|
||||
string Evaluate(string eq)
|
||||
{
|
||||
int deepestP = FindDeepestParenthesis(eq);
|
||||
int closeDeepestP = (deepestP == -1) ? -1 : FindClosedParenthesis(eq, deepestP);
|
||||
|
||||
if (closeDeepestP != -1)
|
||||
{
|
||||
string newEq = eq.Substring(deepestP + 1, closeDeepestP - deepestP - 1);
|
||||
return Evaluate(newEq);
|
||||
}
|
||||
else
|
||||
{
|
||||
Chars.Clear();
|
||||
string newChar = "";
|
||||
bool containsOperator = true;
|
||||
|
||||
while (containsOperator)
|
||||
{
|
||||
for (int i = 0; i < eq.Length; i++)
|
||||
{
|
||||
if (Operators.Contains(eq[i]))
|
||||
{
|
||||
Chars.Add(newChar);
|
||||
newChar = eq[i].ToString();
|
||||
Chars.Add(newChar);
|
||||
newChar = "";
|
||||
}
|
||||
else
|
||||
{
|
||||
if (eq[i] != '(' && eq[i] != ')')
|
||||
{
|
||||
newChar += eq[i];
|
||||
}
|
||||
if (i == eq.Length - 1)
|
||||
{
|
||||
Chars.Add(newChar);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (Chars.Contains("^"))
|
||||
{
|
||||
int pow = (int)Mathf.Pow(GetLeftNumber(eq, FindClosestPower(eq)), GetRightNumber(eq, FindClosestPower(eq)));
|
||||
|
||||
eq = ReplaceWith(eq, pow.ToString(), GetLeftNumberIndex(eq, FindClosestPower(eq)), GetRightNumberIndex(eq, FindClosestPower(eq)));
|
||||
}
|
||||
else if (Chars.Contains("*"))
|
||||
{
|
||||
int mult = GetLeftNumber(eq, FindClosestMultiplication(eq)) * GetRightNumber(eq, FindClosestMultiplication(eq));
|
||||
|
||||
eq = ReplaceWith(eq, mult.ToString(), GetLeftNumberIndex(eq, FindClosestMultiplication(eq)), GetRightNumberIndex(eq, FindClosestMultiplication(eq)));
|
||||
}
|
||||
else if (Chars.Contains("/"))
|
||||
{
|
||||
int div = (int)(GetLeftNumber(eq, FindClosestDivision(eq)) / GetRightNumber(eq, FindClosestDivision(eq)));
|
||||
|
||||
eq = ReplaceWith(eq, div.ToString(), GetLeftNumberIndex(eq, FindClosestDivision(eq)), GetRightNumberIndex(eq, FindClosestDivision(eq)));
|
||||
}
|
||||
else if (Chars.Contains("+"))
|
||||
{
|
||||
int sum = GetLeftNumber(eq, FindClosestAddition(eq)) + GetRightNumber(eq, FindClosestAddition(eq));
|
||||
|
||||
eq = ReplaceWith(eq, sum.ToString(), GetLeftNumberIndex(eq, FindClosestAddition(eq)), GetRightNumberIndex(eq, FindClosestAddition(eq)));
|
||||
}
|
||||
else if (Chars.Contains("-"))
|
||||
{
|
||||
int sub = GetLeftNumber(eq, FindClosestSubtraction(eq)) - GetRightNumber(eq, FindClosestSubtraction(eq));
|
||||
|
||||
eq = ReplaceWith(eq, sub.ToString(), GetLeftNumberIndex(eq, FindClosestSubtraction(eq)), GetRightNumberIndex(eq, FindClosestSubtraction(eq)));
|
||||
}
|
||||
|
||||
|
||||
bool contains = false;
|
||||
for (int i = 0; i < eq.Length; i++)
|
||||
{
|
||||
if (Operators.Contains(eq[i]))
|
||||
{
|
||||
contains = true;
|
||||
}
|
||||
}
|
||||
|
||||
containsOperator = contains;
|
||||
}
|
||||
|
||||
|
||||
return eq;
|
||||
}
|
||||
}
|
||||
|
||||
string ReplaceWith(string replacedStr, string replacingStr, int start, int end)
|
||||
{
|
||||
string ans = replacedStr;
|
||||
ans = ans.Remove(start, end - start);
|
||||
|
||||
string newStr = "";
|
||||
for (int i = start; i < ans.Length; i++)
|
||||
{
|
||||
newStr += ans[i];
|
||||
}
|
||||
ans = ans.Remove(start);
|
||||
|
||||
ans += replacingStr;
|
||||
ans += newStr;
|
||||
|
||||
return ans;
|
||||
}
|
||||
|
||||
int GetLeftNumber(string eq, int index)
|
||||
{
|
||||
string ans = "";
|
||||
for (int i = 0; i < index; i++)
|
||||
{
|
||||
ans += eq[i];
|
||||
if (Operators.Contains(eq[i]))
|
||||
{
|
||||
ans = "";
|
||||
}
|
||||
Debug.Log(ans + " / " + index + " / " + i);
|
||||
}
|
||||
Debug.Log(ans + " / " + index);
|
||||
|
||||
return int.Parse(ans);
|
||||
}
|
||||
int GetLeftNumberIndex(string eq, int index)
|
||||
{
|
||||
int ans = -1;
|
||||
for (int i = index - 1; i >= 0; i--)
|
||||
{
|
||||
if (Operators.Contains(eq[i]))
|
||||
{
|
||||
ans = i + 1;
|
||||
break;
|
||||
}
|
||||
if (i == 0)
|
||||
ans = 0;
|
||||
}
|
||||
return ans;
|
||||
}
|
||||
int GetRightNumber(string eq, int index)
|
||||
{
|
||||
string ans = "";
|
||||
for (int i = index + 1; i < eq.Length; i++)
|
||||
{
|
||||
if (Operators.Contains(eq[i]))
|
||||
{
|
||||
break;
|
||||
}
|
||||
else
|
||||
{
|
||||
ans += eq[i];
|
||||
}
|
||||
}
|
||||
return int.Parse(ans);
|
||||
}
|
||||
int GetRightNumberIndex(string eq, int index)
|
||||
{
|
||||
int ans = -1;
|
||||
for (int i = index + 1; i < eq.Length; i++)
|
||||
{
|
||||
if (Operators.Contains(eq[i]))
|
||||
{
|
||||
ans = i - 1;
|
||||
break;
|
||||
}
|
||||
}
|
||||
return ans;
|
||||
}
|
||||
|
||||
int FindDeepestParenthesis(string eq)
|
||||
{
|
||||
int openedCount = 0, deepestIndex = -1, maxOpenedCount = 0;
|
||||
if (eq.Contains("("))
|
||||
{
|
||||
for (int i = 0; i < eq.Length; i++)
|
||||
{
|
||||
if (eq[i] == '(')
|
||||
{
|
||||
openedCount++;
|
||||
}
|
||||
else if (eq[i] == ')')
|
||||
{
|
||||
openedCount--;
|
||||
}
|
||||
|
||||
if (openedCount > maxOpenedCount)
|
||||
{
|
||||
maxOpenedCount = openedCount;
|
||||
deepestIndex = i;
|
||||
}
|
||||
}
|
||||
}
|
||||
return deepestIndex;
|
||||
}
|
||||
|
||||
int FindClosedParenthesis(string eq, int index)
|
||||
{
|
||||
if (eq[index] == '(')
|
||||
{
|
||||
int openedCount = 1, result = -1;
|
||||
bool counted = false;
|
||||
for (int i = index + 1; i < eq.Length; i++)
|
||||
{
|
||||
if (eq[i] == '(')
|
||||
{
|
||||
if (!counted)
|
||||
counted = true;
|
||||
openedCount++;
|
||||
}
|
||||
|
||||
else if (eq[i] == ')')
|
||||
{
|
||||
if (!counted)
|
||||
counted = true;
|
||||
openedCount--;
|
||||
}
|
||||
|
||||
if (counted && openedCount == 0)
|
||||
{
|
||||
result = i;
|
||||
break;
|
||||
}
|
||||
}
|
||||
return result;
|
||||
}
|
||||
else
|
||||
{
|
||||
return -1;
|
||||
}
|
||||
}
|
||||
|
||||
int FindClosestPower(string eq)
|
||||
{
|
||||
int ans = -1;
|
||||
for (int i = 0; i < eq.Length; i++)
|
||||
{
|
||||
if (eq[i] == '^')
|
||||
{
|
||||
ans = i;
|
||||
break;
|
||||
}
|
||||
}
|
||||
return ans;
|
||||
}
|
||||
|
||||
int FindClosestMultiplication(string eq)
|
||||
{
|
||||
int ans = -1;
|
||||
for (int i = 0; i < eq.Length; i++)
|
||||
{
|
||||
if (eq[i] == '*')
|
||||
{
|
||||
ans = i;
|
||||
break;
|
||||
}
|
||||
}
|
||||
return ans;
|
||||
}
|
||||
int FindClosestDivision(string eq)
|
||||
{
|
||||
int ans = -1;
|
||||
for (int i = 0; i < eq.Length; i++)
|
||||
{
|
||||
if (eq[i] == '/')
|
||||
{
|
||||
ans = i;
|
||||
break;
|
||||
}
|
||||
}
|
||||
return ans;
|
||||
}
|
||||
int FindClosestAddition(string eq)
|
||||
{
|
||||
int ans = -1;
|
||||
for (int i = 0; i < eq.Length; i++)
|
||||
{
|
||||
if (eq[i] == '+')
|
||||
{
|
||||
ans = i;
|
||||
break;
|
||||
}
|
||||
}
|
||||
return ans;
|
||||
}
|
||||
int FindClosestSubtraction(string eq)
|
||||
{
|
||||
int ans = -1;
|
||||
for (int i = 0; i < eq.Length; i++)
|
||||
{
|
||||
if (eq[i] == '-')
|
||||
{
|
||||
ans = i;
|
||||
break;
|
||||
}
|
||||
}
|
||||
return ans;
|
||||
}
|
||||
}
|
||||
11
Assets/Scripts/Old/FunctionEvaluator2.cs.meta
Normal file
11
Assets/Scripts/Old/FunctionEvaluator2.cs.meta
Normal file
|
|
@ -0,0 +1,11 @@
|
|||
fileFormatVersion: 2
|
||||
guid: 271c85a86fed81142b038a88741dda40
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
246
Assets/Scripts/Old/NumericalMethods2.cs
Normal file
246
Assets/Scripts/Old/NumericalMethods2.cs
Normal file
|
|
@ -0,0 +1,246 @@
|
|||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
using UnityEngine;
|
||||
|
||||
public class NumericalMethods2
|
||||
{
|
||||
public List<Equation> Equations = new List<Equation>();
|
||||
|
||||
#region Create
|
||||
/*public void Create()
|
||||
{
|
||||
Equation eq1 = new Equation(new List<float> { -6, 4, -1 }, -4);
|
||||
Equation eq2 = new Equation(new List<float> { 1, 4, -1 }, 2);
|
||||
Equation eq3 = new Equation(new List<float> { -2, 4, -7 }, 18);
|
||||
Equations.Add(eq1);
|
||||
Equations.Add(eq2);
|
||||
Equations.Add(eq3);
|
||||
|
||||
|
||||
Equations.Clear();
|
||||
|
||||
for (int i = 0; i < EquationSystem.VarCount; i++)
|
||||
{
|
||||
List<float> coeffs = new List<float>();
|
||||
for (int j = 0; j < EquationSystem.VarCount; j++)
|
||||
{
|
||||
coeffs.Add(Random.Range(-10f, 10f));
|
||||
}
|
||||
Equation eq = new Equation(coeffs, Random.Range(-10f, 10f));
|
||||
Equations.Add(eq);
|
||||
}
|
||||
|
||||
string log = "";
|
||||
for (int i = 0; i < EquationSystem.VarCount; i++)
|
||||
{
|
||||
for (int j = 0; j < EquationSystem.VarCount; j++)
|
||||
{
|
||||
log += Equations[i].Coefficients[j] + ((j == EquationSystem.VarCount - 1) ? " " + Equations[i].Constant + "\n" : " ");
|
||||
}
|
||||
}
|
||||
Debug.Log(log);
|
||||
|
||||
MethodLinear_GaussSeidal(Equations, new List<float> { 2f, 2f, 2f }, 3);
|
||||
}*/
|
||||
#endregion
|
||||
|
||||
public static List<float> Method_GaussSeidal(List<Equation> _eq, List<float> initialValues, int iterationCount)
|
||||
{
|
||||
//yakınsama şartı (3x3 matris için) -> |a11|>|a12|+|a13| && |a22|>|a21|+|a23| && |a33|>|a31|+|a32|
|
||||
|
||||
bool convergence = true;
|
||||
List<int> dominantTerms = new List<int>();
|
||||
for (int i = 0; i < _eq.Count; i++)
|
||||
{
|
||||
int currentDominant = -1;
|
||||
bool dominantFound = false;
|
||||
for (int j = 0; j < _eq.Count; j++)
|
||||
{
|
||||
float sum0 = 0;
|
||||
|
||||
for (int k = 0; k < _eq.Count; k++)
|
||||
{
|
||||
if (k != j)
|
||||
{
|
||||
sum0 += Mathf.Abs(_eq[i].Coefficients[k]);
|
||||
}
|
||||
}
|
||||
|
||||
if (Mathf.Abs(_eq[i].Coefficients[j]) > sum0)
|
||||
{
|
||||
currentDominant = j;
|
||||
dominantFound = true;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
if (dominantFound)
|
||||
{
|
||||
dominantTerms.Add(currentDominant);
|
||||
}
|
||||
else
|
||||
{
|
||||
dominantTerms.Add(-1);
|
||||
}
|
||||
}
|
||||
|
||||
for (int i = 0; i < dominantTerms.Count && convergence; i++)
|
||||
{
|
||||
for (int j = 0; j < dominantTerms.Count; j++)
|
||||
{
|
||||
if (i != j)
|
||||
{
|
||||
if (dominantTerms[i] == dominantTerms[j])
|
||||
{
|
||||
convergence = false;
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (convergence == false)
|
||||
{
|
||||
Debug.LogError("This matris cannot be solved with Gauss-Seidal Method!");
|
||||
return new List<float>();
|
||||
}
|
||||
else
|
||||
{
|
||||
List<float> answers = initialValues;
|
||||
for (int i = 0; i < iterationCount; i++)
|
||||
{
|
||||
for (int j = 0; j < _eq.Count; j++)
|
||||
{
|
||||
float sum = _eq[j].Constant + _eq[j].Coefficients[j] * answers[j];
|
||||
for (int k = 0; k < _eq.Count; k++)
|
||||
{
|
||||
sum += -_eq[j].Coefficients[k] * answers[k];
|
||||
}
|
||||
answers[j] = sum / _eq[j].Coefficients[j];
|
||||
}
|
||||
}
|
||||
string ansdeb = "";
|
||||
for (int i = 0; i < answers.Count; i++)
|
||||
{
|
||||
ansdeb += answers[i] + ((i == answers.Count - 1) ? "" : ", ");
|
||||
}
|
||||
Debug.Log(ansdeb);
|
||||
|
||||
return answers;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
public static float Method_EvaluateFunction(Equation eq, float point)
|
||||
{
|
||||
float result = 0;
|
||||
if (!eq.MultiVariable)
|
||||
{
|
||||
float sum = 0;
|
||||
for (int i = 0; i < eq.Coefficients.Count; i++)
|
||||
{
|
||||
sum += eq.Coefficients[i] * Mathf.Pow(point, eq.Coefficients.Count - i);
|
||||
}
|
||||
result = sum + eq.Constant;
|
||||
}
|
||||
else
|
||||
{
|
||||
Debug.LogError("Method_EvaluateFunction: The equation is multivariable!");
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
|
||||
public static Equation Method_TaylorSeries(Equation eq, int degree)
|
||||
{
|
||||
float constant = Method_EvaluateFunction(eq, 0);
|
||||
List<float> coeffs = new List<float>();
|
||||
for (int i = 1; i < degree + 1; i++)
|
||||
{
|
||||
float c = Method_Derivative(eq, i, 0, 0.1f) / Factorial(i);
|
||||
coeffs.Add(c);
|
||||
}
|
||||
|
||||
coeffs.Reverse();
|
||||
|
||||
Equation taylorEq = new Equation(coeffs, constant);
|
||||
return taylorEq;
|
||||
}
|
||||
|
||||
public static float Method_NewtonRaphson(Equation eq, float initialValue, int iterationCount)
|
||||
{
|
||||
float Lipschitz = Method_EvaluateFunction(eq, initialValue) * Method_SecondDerivative(eq, initialValue, 0.1f) /
|
||||
Method_FirstDerivative(eq, initialValue, 0.1f) / Method_FirstDerivative(eq, initialValue, 0.1f);
|
||||
|
||||
if (Lipschitz < 1) { }
|
||||
else
|
||||
{
|
||||
Debug.LogError("Wrong initial value!");
|
||||
return 0;
|
||||
}
|
||||
|
||||
float init = initialValue;
|
||||
for (int i = 0; i < iterationCount; i++)
|
||||
{
|
||||
float next = init - Method_EvaluateFunction(eq, init) / Method_FirstDerivative(eq, init, 0.1f);
|
||||
init = next;
|
||||
}
|
||||
return init;
|
||||
}
|
||||
|
||||
public static float Method_Derivative(Equation eq, int degree, float point, float h)
|
||||
{
|
||||
Equation mainEq = eq, resEq = eq;
|
||||
float sum = 0;
|
||||
for (int i = 0; i <= degree; i++)
|
||||
{
|
||||
sum += Mathf.Pow(-1, i) * Combination(degree, i) * Method_EvaluateFunction(eq, point + (degree - i) * h);
|
||||
}
|
||||
float res = sum / Mathf.Pow(h, degree);
|
||||
return res;
|
||||
}
|
||||
|
||||
public static float Method_FirstDerivative(Equation eq, float point, float h)
|
||||
{
|
||||
float fxi = Method_EvaluateFunction(eq, point), fxi1 = Method_EvaluateFunction(eq, point - h), fxi2 = Method_EvaluateFunction(eq, point - 2 * h);
|
||||
return (3 * fxi + fxi2 - 4 * fxi1) / 2 / h;
|
||||
}
|
||||
public static float Method_SecondDerivative(Equation eq, float point, float h)
|
||||
{
|
||||
float fxi = Method_EvaluateFunction(eq, point), fxi1 = Method_EvaluateFunction(eq, point - h), fxi2 = Method_EvaluateFunction(eq, point - 2 * h);
|
||||
return (fxi + fxi2 - 2 * fxi1) / h / h;
|
||||
}
|
||||
public static float Method_ThirdDerivative(Equation eq, float point, float h)
|
||||
{
|
||||
float fxi = Method_EvaluateFunction(eq, point), fxi1 = Method_EvaluateFunction(eq, point - h), fxi2 = Method_EvaluateFunction(eq, point - 2 * h),
|
||||
fxi3 = Method_EvaluateFunction(eq, point - 3 * h), fxi4 = Method_EvaluateFunction(eq, point - 4 * h);
|
||||
return (3 * fxi4 - 14 * fxi3 + 24 * fxi2 - 18 * fxi1 + 5 * fxi) / 2 / h / h / h;
|
||||
|
||||
}
|
||||
public static float Method_FourthDerivative(Equation eq, float point, float h)
|
||||
{
|
||||
float fxi = Method_EvaluateFunction(eq, point), fxi1 = Method_EvaluateFunction(eq, point - h), fxi2 = Method_EvaluateFunction(eq, point - 2 * h),
|
||||
fxi3 = Method_EvaluateFunction(eq, point - 3 * h), fxi4 = Method_EvaluateFunction(eq, point - 4 * h), fxi5 = Method_EvaluateFunction(eq, point - 5 * h);
|
||||
return (-2 * fxi5 + 11 * fxi4 - 24 * fxi3 + 26 * fxi2 - 14 * fxi1 + 3 * fxi) / h / h / h / h;
|
||||
}
|
||||
|
||||
public static int Factorial(int num)
|
||||
{
|
||||
if (num >= 1)
|
||||
{
|
||||
return num * Factorial(num - 1);
|
||||
}
|
||||
else
|
||||
{
|
||||
return 1;
|
||||
}
|
||||
}
|
||||
public static int Combination(int n, int k)
|
||||
{
|
||||
int top = Factorial(n);
|
||||
int bot1 = Factorial(n - k);
|
||||
int bot2 = Factorial(k);
|
||||
|
||||
return top / bot1 / bot2;
|
||||
}
|
||||
}
|
||||
11
Assets/Scripts/Old/NumericalMethods2.cs.meta
Normal file
11
Assets/Scripts/Old/NumericalMethods2.cs.meta
Normal file
|
|
@ -0,0 +1,11 @@
|
|||
fileFormatVersion: 2
|
||||
guid: ba64140c4b91bfe479878f521a339495
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
184
Assets/Scripts/UIController.cs
Normal file
184
Assets/Scripts/UIController.cs
Normal file
|
|
@ -0,0 +1,184 @@
|
|||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
using UnityEngine;
|
||||
using UnityEngine.UI;
|
||||
|
||||
public class UIController : MonoBehaviour
|
||||
{
|
||||
public GameObject PNLMainMenu, PNLEquationRootFinder, PNLEquationSystemSolver, INPTEquation, INPTMinX, INPTMaxX,
|
||||
INPTRoot, INPTRoots, TGLNewtonRaphson, INPTNRInitialPoint, INPTEqVariable, INPTIterationCount, INPTEquationCount,
|
||||
INPTEquationPrefab, INPTVariablePrefab, INPTInitialPointPrefab, ESEquationParent, ESVariableParent, ESInitialPointParent,
|
||||
INPTESIterationCount;
|
||||
public string INPTRootText, INPTRootsText;
|
||||
public bool isNewtonRaphson;
|
||||
|
||||
public void BTNEquationRootFinder()
|
||||
{
|
||||
PNLMainMenu.SetActive(false);
|
||||
PNLEquationRootFinder.SetActive(true);
|
||||
PNLEquationSystemSolver.SetActive(false);
|
||||
}
|
||||
public void BTNEquationSystemSolver()
|
||||
{
|
||||
PNLMainMenu.SetActive(false);
|
||||
PNLEquationRootFinder.SetActive(false);
|
||||
PNLEquationSystemSolver.SetActive(true);
|
||||
}
|
||||
|
||||
public void BTNMainMenuERF()
|
||||
{
|
||||
PNLMainMenu.SetActive(true);
|
||||
PNLEquationRootFinder.SetActive(false);
|
||||
PNLEquationSystemSolver.SetActive(false);
|
||||
|
||||
INPTNRInitialPoint.GetComponent<InputField>().text = "";
|
||||
INPTMaxX.GetComponent<InputField>().text = "";
|
||||
INPTMinX.GetComponent<InputField>().text = "";
|
||||
INPTEqVariable.GetComponent<InputField>().text = "";
|
||||
INPTIterationCount.GetComponent<InputField>().text = "";
|
||||
INPTEquation.GetComponent<InputField>().text = "";
|
||||
}
|
||||
public void BTNMainMenuESS()
|
||||
{
|
||||
PNLMainMenu.SetActive(true);
|
||||
PNLEquationRootFinder.SetActive(false);
|
||||
PNLEquationSystemSolver.SetActive(false);
|
||||
|
||||
for (int i = 0; i < Equations.Count; i++)
|
||||
{
|
||||
Destroy(Equations[i]);
|
||||
Destroy(Variables[i]);
|
||||
Destroy(InitialPoints[i]);
|
||||
}
|
||||
|
||||
Equations.Clear();
|
||||
Variables.Clear();
|
||||
InitialPoints.Clear();
|
||||
|
||||
INPTEquationCount.GetComponent<InputField>().text = "0";
|
||||
}
|
||||
|
||||
public void BTNFindEquationRoot()
|
||||
{
|
||||
string eq = INPTEquation.GetComponent<InputField>().text;
|
||||
string variable = INPTEqVariable.GetComponent<InputField>().text;
|
||||
int iterCount = int.Parse(INPTIterationCount.GetComponent<InputField>().text);
|
||||
|
||||
decimal res = 0;
|
||||
|
||||
if (isNewtonRaphson)
|
||||
{
|
||||
decimal initialP = decimal.Parse(INPTNRInitialPoint.GetComponent<InputField>().text);
|
||||
res = NumericalMethods.Method_NewtonRaphson(eq, variable, initialP, iterCount);
|
||||
}
|
||||
else
|
||||
{
|
||||
decimal minX = decimal.Parse(INPTMinX.GetComponent<InputField>().text);
|
||||
decimal maxX = decimal.Parse(INPTMaxX.GetComponent<InputField>().text);
|
||||
res = NumericalMethods.Method_BisectionMethod(eq, variable, minX, maxX, iterCount);
|
||||
}
|
||||
INPTRootText = res.ToString();
|
||||
INPTRoot.GetComponent<InputField>().text = res.ToString();
|
||||
}
|
||||
public void TGLNewtonRaphsonBisection()
|
||||
{
|
||||
if (TGLNewtonRaphson.GetComponent<Toggle>().isOn)
|
||||
{
|
||||
isNewtonRaphson = true;
|
||||
INPTNRInitialPoint.SetActive(true);
|
||||
INPTMaxX.SetActive(false);
|
||||
INPTMinX.SetActive(false);
|
||||
|
||||
INPTNRInitialPoint.GetComponent<InputField>().text = "";
|
||||
INPTMaxX.GetComponent<InputField>().text = "";
|
||||
INPTMinX.GetComponent<InputField>().text = "";
|
||||
}
|
||||
else
|
||||
{
|
||||
isNewtonRaphson = false;
|
||||
INPTNRInitialPoint.SetActive(false);
|
||||
INPTMaxX.SetActive(true);
|
||||
INPTMinX.SetActive(true);
|
||||
|
||||
INPTNRInitialPoint.GetComponent<InputField>().text = "";
|
||||
INPTMaxX.GetComponent<InputField>().text = "";
|
||||
INPTMinX.GetComponent<InputField>().text = "";
|
||||
}
|
||||
}
|
||||
|
||||
public void INPTRootChanged()
|
||||
{
|
||||
INPTRoot.GetComponent<InputField>().text = INPTRootText;
|
||||
}
|
||||
public void INPTRootsChanged()
|
||||
{
|
||||
INPTRoots.GetComponent<InputField>().text = INPTRootsText;
|
||||
}
|
||||
|
||||
List<GameObject> Equations = new List<GameObject>(), Variables = new List<GameObject>(), InitialPoints = new List<GameObject>();
|
||||
public void INPTEquationCountChanged()
|
||||
{
|
||||
int eqCount = INPTEquationCount.GetComponent<InputField>().text != "" ? int.Parse(INPTEquationCount.GetComponent<InputField>().text) : 0;
|
||||
|
||||
for (int i = 0; i < Equations.Count; i++)
|
||||
{
|
||||
Destroy(Equations[i]);
|
||||
Destroy(Variables[i]);
|
||||
Destroy(InitialPoints[i]);
|
||||
}
|
||||
|
||||
Equations.Clear();
|
||||
Variables.Clear();
|
||||
InitialPoints.Clear();
|
||||
|
||||
for (int i = 0; i < eqCount; i++)
|
||||
{
|
||||
GameObject GO1 = Instantiate(INPTEquationPrefab);
|
||||
GO1.transform.SetParent(ESEquationParent.transform);
|
||||
GO1.transform.localScale = Vector3.one;
|
||||
Equations.Add(GO1);
|
||||
|
||||
GameObject GO2 = Instantiate(INPTVariablePrefab);
|
||||
GO2.transform.SetParent(ESVariableParent.transform);
|
||||
GO2.transform.localScale = Vector3.one;
|
||||
Variables.Add(GO2);
|
||||
|
||||
GameObject GO3 = Instantiate(INPTInitialPointPrefab);
|
||||
GO3.transform.SetParent(ESInitialPointParent.transform);
|
||||
GO3.transform.localScale = Vector3.one;
|
||||
InitialPoints.Add(GO3);
|
||||
}
|
||||
}
|
||||
|
||||
Dictionary<string, decimal> dict = new Dictionary<string, decimal>();
|
||||
public void BTNFindEquationSystemRoots()
|
||||
{
|
||||
List<string> Eqs = new List<string>();
|
||||
|
||||
dict.Clear();
|
||||
for (int i = 0; i < Equations.Count; i++)
|
||||
{
|
||||
dict.Add(Variables[i].GetComponent<InputField>().text, decimal.Parse(InitialPoints[i].GetComponent<InputField>().text));
|
||||
Eqs.Add(Equations[i].GetComponent<InputField>().text);
|
||||
}
|
||||
|
||||
int iterCount = int.Parse(INPTESIterationCount.GetComponent<InputField>().text);
|
||||
|
||||
Dictionary<string, decimal> results = NumericalMethods.MethodMultiVar_SolveEquationSystem(Eqs, dict, iterCount);
|
||||
|
||||
string sum = "";
|
||||
|
||||
foreach (KeyValuePair<string, decimal> p in results)
|
||||
{
|
||||
sum += p.Key + ": " + p.Value + ", ";
|
||||
}
|
||||
|
||||
INPTRootsText = sum;
|
||||
INPTRoots.GetComponent<InputField>().text = INPTRootsText;
|
||||
}
|
||||
|
||||
public void ExitTheProgram()
|
||||
{
|
||||
Application.Quit();
|
||||
}
|
||||
}
|
||||
11
Assets/Scripts/UIController.cs.meta
Normal file
11
Assets/Scripts/UIController.cs.meta
Normal file
|
|
@ -0,0 +1,11 @@
|
|||
fileFormatVersion: 2
|
||||
guid: a1b44d2e6ad183849baa8b9136dac8ab
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
174
MathApp/MathApp/Controls/GraphPlotterControl.Designer.cs
generated
Normal file
174
MathApp/MathApp/Controls/GraphPlotterControl.Designer.cs
generated
Normal file
|
|
@ -0,0 +1,174 @@
|
|||
|
||||
namespace MathApp.Controls
|
||||
{
|
||||
partial class GraphPlotterControl
|
||||
{
|
||||
private System.ComponentModel.IContainer components = null;
|
||||
|
||||
protected override void Dispose(bool disposing)
|
||||
{
|
||||
if (disposing && (components != null))
|
||||
{
|
||||
components.Dispose();
|
||||
}
|
||||
base.Dispose(disposing);
|
||||
}
|
||||
|
||||
private void InitializeComponent()
|
||||
{
|
||||
this.lblTitle = new System.Windows.Forms.Label();
|
||||
this.pnlControls = new System.Windows.Forms.Panel();
|
||||
this.pnlGraphConfig = new System.Windows.Forms.Panel();
|
||||
this.pnlCanvas = new System.Windows.Forms.Panel();
|
||||
|
||||
this.lblEquation = new System.Windows.Forms.Label();
|
||||
this.txtEquation = new System.Windows.Forms.TextBox();
|
||||
this.lblMinX = new System.Windows.Forms.Label();
|
||||
this.txtMinX = new System.Windows.Forms.TextBox();
|
||||
this.lblMaxX = new System.Windows.Forms.Label();
|
||||
this.txtMaxX = new System.Windows.Forms.TextBox();
|
||||
this.btnPlot = new System.Windows.Forms.Button();
|
||||
|
||||
this.pnlControls.SuspendLayout();
|
||||
this.pnlGraphConfig.SuspendLayout();
|
||||
this.SuspendLayout();
|
||||
|
||||
//
|
||||
// lblTitle
|
||||
//
|
||||
this.lblTitle.Dock = System.Windows.Forms.DockStyle.Top;
|
||||
this.lblTitle.Font = new System.Drawing.Font("Segoe UI Light", 24F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point);
|
||||
this.lblTitle.ForeColor = System.Drawing.Color.White;
|
||||
this.lblTitle.Location = new System.Drawing.Point(0, 0);
|
||||
this.lblTitle.Name = "lblTitle";
|
||||
this.lblTitle.Size = new System.Drawing.Size(800, 60);
|
||||
this.lblTitle.TabIndex = 0;
|
||||
this.lblTitle.Text = "Graph Plotter";
|
||||
this.lblTitle.TextAlign = System.Drawing.ContentAlignment.MiddleLeft;
|
||||
|
||||
//
|
||||
// pnlControls
|
||||
//
|
||||
this.pnlControls.Controls.Add(this.pnlGraphConfig);
|
||||
this.pnlControls.Dock = System.Windows.Forms.DockStyle.Top;
|
||||
this.pnlControls.Location = new System.Drawing.Point(0, 60);
|
||||
this.pnlControls.Name = "pnlControls";
|
||||
this.pnlControls.Size = new System.Drawing.Size(800, 80);
|
||||
this.pnlControls.TabIndex = 1;
|
||||
|
||||
//
|
||||
// pnlGraphConfig
|
||||
//
|
||||
this.pnlGraphConfig.Controls.Add(this.btnPlot);
|
||||
this.pnlGraphConfig.Controls.Add(this.txtMaxX);
|
||||
this.pnlGraphConfig.Controls.Add(this.lblMaxX);
|
||||
this.pnlGraphConfig.Controls.Add(this.txtMinX);
|
||||
this.pnlGraphConfig.Controls.Add(this.lblMinX);
|
||||
this.pnlGraphConfig.Controls.Add(this.txtEquation);
|
||||
this.pnlGraphConfig.Controls.Add(this.lblEquation);
|
||||
this.pnlGraphConfig.Dock = System.Windows.Forms.DockStyle.Fill;
|
||||
this.pnlGraphConfig.Location = new System.Drawing.Point(0, 0);
|
||||
this.pnlGraphConfig.Name = "pnlGraphConfig";
|
||||
this.pnlGraphConfig.Size = new System.Drawing.Size(800, 80);
|
||||
this.pnlGraphConfig.TabIndex = 0;
|
||||
|
||||
int xPos = 20; int yPos = 25;
|
||||
|
||||
// Equation
|
||||
this.lblEquation.AutoSize = true;
|
||||
this.lblEquation.ForeColor = System.Drawing.Color.White;
|
||||
this.lblEquation.Text = "y = ";
|
||||
this.lblEquation.Location = new System.Drawing.Point(xPos, yPos);
|
||||
xPos += 30;
|
||||
|
||||
this.txtEquation.Location = new System.Drawing.Point(xPos, yPos-3);
|
||||
this.txtEquation.Size = new System.Drawing.Size(200, 23);
|
||||
this.txtEquation.BackColor = System.Drawing.Color.FromArgb(40,40,40);
|
||||
this.txtEquation.ForeColor = System.Drawing.Color.White;
|
||||
this.txtEquation.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle;
|
||||
xPos += 220;
|
||||
|
||||
// Min X
|
||||
this.lblMinX.AutoSize = true;
|
||||
this.lblMinX.ForeColor = System.Drawing.Color.White;
|
||||
this.lblMinX.Text = "Min X:";
|
||||
this.lblMinX.Location = new System.Drawing.Point(xPos, yPos);
|
||||
xPos += 50;
|
||||
|
||||
this.txtMinX.Location = new System.Drawing.Point(xPos, yPos-3);
|
||||
this.txtMinX.Size = new System.Drawing.Size(50, 23);
|
||||
this.txtMinX.Text = "-10";
|
||||
this.txtMinX.BackColor = System.Drawing.Color.FromArgb(40,40,40);
|
||||
this.txtMinX.ForeColor = System.Drawing.Color.White;
|
||||
this.txtMinX.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle;
|
||||
xPos += 70;
|
||||
|
||||
// Max X
|
||||
this.lblMaxX.AutoSize = true;
|
||||
this.lblMaxX.ForeColor = System.Drawing.Color.White;
|
||||
this.lblMaxX.Text = "Max X:";
|
||||
this.lblMaxX.Location = new System.Drawing.Point(xPos, yPos);
|
||||
xPos += 50;
|
||||
|
||||
this.txtMaxX.Location = new System.Drawing.Point(xPos, yPos-3);
|
||||
this.txtMaxX.Size = new System.Drawing.Size(50, 23);
|
||||
this.txtMaxX.Text = "10";
|
||||
this.txtMaxX.BackColor = System.Drawing.Color.FromArgb(40,40,40);
|
||||
this.txtMaxX.ForeColor = System.Drawing.Color.White;
|
||||
this.txtMaxX.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle;
|
||||
xPos += 70;
|
||||
|
||||
// Plot Button
|
||||
this.btnPlot.Location = new System.Drawing.Point(xPos, yPos-5);
|
||||
this.btnPlot.Size = new System.Drawing.Size(100, 30);
|
||||
this.btnPlot.Text = "Plot";
|
||||
this.btnPlot.FlatStyle = System.Windows.Forms.FlatStyle.Flat;
|
||||
this.btnPlot.BackColor = System.Drawing.Color.FromArgb(0, 120, 215);
|
||||
this.btnPlot.ForeColor = System.Drawing.Color.White;
|
||||
this.btnPlot.FlatAppearance.BorderSize = 0;
|
||||
|
||||
|
||||
//
|
||||
// pnlCanvas
|
||||
//
|
||||
this.pnlCanvas.Dock = System.Windows.Forms.DockStyle.Fill;
|
||||
this.pnlCanvas.Location = new System.Drawing.Point(0, 140);
|
||||
this.pnlCanvas.Name = "pnlCanvas";
|
||||
this.pnlCanvas.Size = new System.Drawing.Size(800, 460);
|
||||
this.pnlCanvas.TabIndex = 2;
|
||||
this.pnlCanvas.BackColor = System.Drawing.Color.FromArgb(30, 30, 30);
|
||||
this.pnlCanvas.Paint += new System.Windows.Forms.PaintEventHandler(this.pnlCanvas_Paint);
|
||||
this.pnlCanvas.Resize += new System.EventHandler(this.pnlCanvas_Resize);
|
||||
|
||||
//
|
||||
// GraphPlotterControl
|
||||
//
|
||||
this.AutoScaleDimensions = new System.Drawing.SizeF(7F, 15F);
|
||||
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
|
||||
this.BackColor = System.Drawing.Color.FromArgb(((int)(((byte)(20)))), ((int)(((byte)(20)))), ((int)(((byte)(20)))));
|
||||
this.Controls.Add(this.pnlCanvas);
|
||||
this.Controls.Add(this.pnlControls);
|
||||
this.Controls.Add(this.lblTitle);
|
||||
this.Name = "GraphPlotterControl";
|
||||
this.Size = new System.Drawing.Size(800, 600);
|
||||
this.pnlControls.ResumeLayout(false);
|
||||
this.pnlGraphConfig.ResumeLayout(false);
|
||||
this.pnlGraphConfig.PerformLayout();
|
||||
this.ResumeLayout(false);
|
||||
|
||||
}
|
||||
|
||||
private System.Windows.Forms.Label lblTitle;
|
||||
private System.Windows.Forms.Panel pnlControls;
|
||||
private System.Windows.Forms.Panel pnlGraphConfig;
|
||||
private System.Windows.Forms.Panel pnlCanvas;
|
||||
|
||||
private System.Windows.Forms.Label lblEquation;
|
||||
private System.Windows.Forms.TextBox txtEquation;
|
||||
private System.Windows.Forms.Label lblMinX;
|
||||
private System.Windows.Forms.TextBox txtMinX;
|
||||
private System.Windows.Forms.Label lblMaxX;
|
||||
private System.Windows.Forms.TextBox txtMaxX;
|
||||
private System.Windows.Forms.Button btnPlot;
|
||||
}
|
||||
}
|
||||
339
MathApp/MathApp/Controls/GraphPlotterControl.cs
Normal file
339
MathApp/MathApp/Controls/GraphPlotterControl.cs
Normal file
|
|
@ -0,0 +1,339 @@
|
|||
using System;
|
||||
using System.Drawing;
|
||||
using System.Windows.Forms;
|
||||
using System.Collections.Generic;
|
||||
|
||||
namespace MathApp.Controls
|
||||
{
|
||||
public partial class GraphPlotterControl : UserControl
|
||||
{
|
||||
private List<PointF> points = new List<PointF>();
|
||||
private List<PointF> markedPoints = new List<PointF>(); // Points marked by user
|
||||
|
||||
// Logical ranges
|
||||
private float rangeMinX = -10;
|
||||
private float rangeMaxX = 10;
|
||||
private float rangeMinY = -10;
|
||||
private float rangeMaxY = 10;
|
||||
|
||||
private string currentEq = "";
|
||||
|
||||
// Mouse interaction state
|
||||
private bool isDragging = false;
|
||||
private Point lastMousePos;
|
||||
|
||||
public GraphPlotterControl()
|
||||
{
|
||||
InitializeComponent();
|
||||
btnPlot.Click += BtnPlot_Click;
|
||||
|
||||
// Enable double buffering
|
||||
SetStyle(ControlStyles.DoubleBuffer | ControlStyles.UserPaint | ControlStyles.AllPaintingInWmPaint, true);
|
||||
UpdateStyles();
|
||||
|
||||
// Attach mouse events to the canvas panel
|
||||
pnlCanvas.MouseDown += PnlCanvas_MouseDown;
|
||||
pnlCanvas.MouseMove += PnlCanvas_MouseMove;
|
||||
pnlCanvas.MouseUp += PnlCanvas_MouseUp;
|
||||
pnlCanvas.MouseClick += PnlCanvas_MouseClick;
|
||||
pnlCanvas.Resize += pnlCanvas_Resize;
|
||||
pnlCanvas.Paint += pnlCanvas_Paint;
|
||||
}
|
||||
|
||||
private void BtnPlot_Click(object sender, EventArgs e)
|
||||
{
|
||||
currentEq = txtEquation.Text;
|
||||
markedPoints.Clear(); // Clear marks on new plot? Or keep them? Let's clear for fresh start.
|
||||
if (string.IsNullOrWhiteSpace(currentEq)) return;
|
||||
|
||||
rangeMinX = parse(txtMinX.Text, -10);
|
||||
rangeMaxX = parse(txtMaxX.Text, 10);
|
||||
|
||||
GeneratePoints();
|
||||
pnlCanvas.Invalidate();
|
||||
}
|
||||
|
||||
private float parse(string s, float def)
|
||||
{
|
||||
if (float.TryParse(s, out float v)) return v;
|
||||
return def;
|
||||
}
|
||||
|
||||
private void GeneratePoints()
|
||||
{
|
||||
points.Clear();
|
||||
float step = (rangeMaxX - rangeMinX) / 2000f; // Higher resolution
|
||||
|
||||
float tempMinY = float.MaxValue;
|
||||
float tempMaxY = float.MinValue;
|
||||
|
||||
for (float x = rangeMinX; x <= rangeMaxX; x += step)
|
||||
{
|
||||
try
|
||||
{
|
||||
decimal val = NumericalMethods.Method_EvaluateFunction(currentEq, "x", (decimal)x);
|
||||
float y = (float)val;
|
||||
|
||||
// Clamp excessively large values to avoid float overflow issues in GDI+
|
||||
if (y > 10000) y = 10000;
|
||||
if (y < -10000) y = -10000;
|
||||
|
||||
points.Add(new PointF(x, y));
|
||||
|
||||
if (y < tempMinY) tempMinY = y;
|
||||
if (y > tempMaxY) tempMaxY = y;
|
||||
}
|
||||
catch
|
||||
{
|
||||
// Ignore calculation errors (e.g. division by zero, out of domain)
|
||||
}
|
||||
}
|
||||
|
||||
// Auto-scale Y if it's the first plot or if we want to snap to range
|
||||
// But if we are panning, we probably shouldn't auto-reset Y every time unless explicit.
|
||||
// For now, let's set Y range based on data with some padding
|
||||
if (tempMinY < tempMaxY)
|
||||
{
|
||||
float h = tempMaxY - tempMinY;
|
||||
if (h == 0) h = 10;
|
||||
rangeMinY = tempMinY - h * 0.1f;
|
||||
rangeMaxY = tempMaxY + h * 0.1f;
|
||||
}
|
||||
else
|
||||
{
|
||||
rangeMinY = -10;
|
||||
rangeMaxY = 10;
|
||||
}
|
||||
}
|
||||
|
||||
// --- Mouse Interaction ---
|
||||
|
||||
protected override void OnMouseWheel(MouseEventArgs e)
|
||||
{
|
||||
// Zoom Factor
|
||||
float zoom = (e.Delta > 0) ? 0.9f : 1.1f;
|
||||
|
||||
// Calculate center
|
||||
float rangeW = rangeMaxX - rangeMinX;
|
||||
float rangeH = rangeMaxY - rangeMinY;
|
||||
|
||||
float midX = (rangeMaxX + rangeMinX) / 2;
|
||||
float midY = (rangeMaxY + rangeMinY) / 2;
|
||||
|
||||
// Apply zoom centered
|
||||
float halfW = (rangeW * zoom) / 2;
|
||||
float halfH = (rangeH * zoom) / 2;
|
||||
|
||||
rangeMinX = midX - halfW;
|
||||
rangeMaxX = midX + halfW;
|
||||
rangeMinY = midY - halfH;
|
||||
rangeMaxY = midY + halfH;
|
||||
|
||||
GeneratePoints();
|
||||
pnlCanvas.Invalidate();
|
||||
|
||||
base.OnMouseWheel(e); // Let it bubble if needed
|
||||
}
|
||||
|
||||
private void PnlCanvas_MouseDown(object sender, MouseEventArgs e)
|
||||
{
|
||||
if (e.Button == MouseButtons.Left)
|
||||
{
|
||||
isDragging = true;
|
||||
lastMousePos = e.Location;
|
||||
Cursor = Cursors.SizeAll;
|
||||
}
|
||||
}
|
||||
|
||||
private void PnlCanvas_MouseMove(object sender, MouseEventArgs e)
|
||||
{
|
||||
if (isDragging)
|
||||
{
|
||||
float dx = e.X - lastMousePos.X;
|
||||
float dy = e.Y - lastMousePos.Y;
|
||||
|
||||
float rangeW = rangeMaxX - rangeMinX;
|
||||
float rangeH = rangeMaxY - rangeMinY;
|
||||
|
||||
float w = pnlCanvas.Width;
|
||||
float h = pnlCanvas.Height;
|
||||
|
||||
// Scale pixel delta to logical delta
|
||||
// Note: dy is inverted because screen Y grows down, but math Y grows up
|
||||
float moveX = -dx * (rangeW / w);
|
||||
float moveY = dy * (rangeH / h);
|
||||
|
||||
rangeMinX += moveX;
|
||||
rangeMaxX += moveX;
|
||||
rangeMinY += moveY;
|
||||
rangeMaxY += moveY;
|
||||
|
||||
lastMousePos = e.Location;
|
||||
|
||||
// Re-generate points if we moved significantly to ensure we cover the new X range?
|
||||
// Or just shift the view?
|
||||
// If we shift X, we need new data points for the new X area.
|
||||
// So we should re-generate.
|
||||
GeneratePoints();
|
||||
|
||||
pnlCanvas.Invalidate();
|
||||
}
|
||||
}
|
||||
|
||||
private void PnlCanvas_MouseUp(object sender, MouseEventArgs e)
|
||||
{
|
||||
if (e.Button == MouseButtons.Left)
|
||||
{
|
||||
isDragging = false;
|
||||
Cursor = Cursors.Default;
|
||||
}
|
||||
}
|
||||
|
||||
private void PnlCanvas_MouseClick(object sender, MouseEventArgs e)
|
||||
{
|
||||
// Right click or simple click (if not dragging) to mark point
|
||||
if (!isDragging && e.Button == MouseButtons.Left)
|
||||
{
|
||||
// Unmap screen to logical
|
||||
float w = pnlCanvas.Width;
|
||||
float h = pnlCanvas.Height;
|
||||
|
||||
float xPct = e.X / w;
|
||||
float yPct = (h - e.Y) / h; // Invert Y
|
||||
|
||||
float logX = rangeMinX + xPct * (rangeMaxX - rangeMinX);
|
||||
float logY = rangeMinY + yPct * (rangeMaxY - rangeMinY);
|
||||
|
||||
markedPoints.Add(new PointF(logX, logY));
|
||||
pnlCanvas.Invalidate();
|
||||
}
|
||||
}
|
||||
|
||||
// --- Rendering ---
|
||||
|
||||
private void pnlCanvas_Paint(object sender, PaintEventArgs e)
|
||||
{
|
||||
Graphics g = e.Graphics;
|
||||
g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;
|
||||
g.TextRenderingHint = System.Drawing.Text.TextRenderingHint.ClearTypeGridFit;
|
||||
|
||||
float w = pnlCanvas.Width;
|
||||
float h = pnlCanvas.Height;
|
||||
|
||||
// Map functions
|
||||
float MapX(float x) => (x - rangeMinX) / (rangeMaxX - rangeMinX) * w;
|
||||
float MapY(float y) => h - ((y - rangeMinY) / (rangeMaxY - rangeMinY) * h);
|
||||
|
||||
// 1. Draw Grid
|
||||
DrawGrid(g, w, h, MapX, MapY);
|
||||
|
||||
// 2. Draw Axes
|
||||
using (Pen axisPen = new Pen(Color.White, 2))
|
||||
{
|
||||
float y0 = MapY(0);
|
||||
float x0 = MapX(0);
|
||||
|
||||
if (y0 >= 0 && y0 <= h) g.DrawLine(axisPen, 0, y0, w, y0);
|
||||
if (x0 >= 0 && x0 <= w) g.DrawLine(axisPen, x0, 0, x0, h);
|
||||
}
|
||||
|
||||
// 3. Draw Function
|
||||
if (points.Count > 1)
|
||||
{
|
||||
using (Pen graphPen = new Pen(Color.Cyan, 2))
|
||||
{
|
||||
List<PointF> screenPoints = new List<PointF>();
|
||||
foreach (var p in points)
|
||||
{
|
||||
screenPoints.Add(new PointF(MapX(p.X), MapY(p.Y)));
|
||||
}
|
||||
|
||||
// Draw valid segments
|
||||
// We shouldn't connect jumps between valid/invalid or huge leaps
|
||||
// For simplicity, we draw lines.
|
||||
try
|
||||
{
|
||||
// Clipping helps performance but GDI+ handles basic out-of-bounds well
|
||||
// unless coordinates are floats.NaN or Infinity.
|
||||
g.DrawLines(graphPen, screenPoints.ToArray());
|
||||
}
|
||||
catch { }
|
||||
}
|
||||
}
|
||||
|
||||
// 4. Draw Marked Points
|
||||
using(Brush markBrush = new SolidBrush(Color.Yellow))
|
||||
using(Pen markPen = new Pen(Color.Red, 2))
|
||||
using(Font markFont = new Font("Segoe UI", 9))
|
||||
{
|
||||
foreach(var p in markedPoints)
|
||||
{
|
||||
float sx = MapX(p.X);
|
||||
float sy = MapY(p.Y);
|
||||
|
||||
// Draw X mark
|
||||
float r = 5;
|
||||
g.DrawLine(markPen, sx - r, sy - r, sx + r, sy + r);
|
||||
g.DrawLine(markPen, sx + r, sy - r, sx - r, sy + r);
|
||||
|
||||
// Draw coordinates text
|
||||
string coord = $"({p.X:F2}, {p.Y:F2})";
|
||||
g.DrawString(coord, markFont, markBrush, sx + 5, sy - 20);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private void DrawGrid(Graphics g, float w, float h, Func<float, float> mapX, Func<float, float> mapY)
|
||||
{
|
||||
using (Pen gridPen = new Pen(Color.FromArgb(50, 50, 50), 1))
|
||||
using (Font font = new Font("Segoe UI", 8))
|
||||
using (Brush brush = new SolidBrush(Color.Gray))
|
||||
{
|
||||
// Calculate roughly how many lines we want
|
||||
int targetLinesW = 10;
|
||||
int targetLinesH = 10;
|
||||
|
||||
float rangeW = rangeMaxX - rangeMinX;
|
||||
float stepW = CalculateStep(rangeW, targetLinesW);
|
||||
|
||||
float rangeH = rangeMaxY - rangeMinY;
|
||||
float stepH = CalculateStep(rangeH, targetLinesH);
|
||||
|
||||
// Vertical Grid Lines
|
||||
float startX = (float)Math.Floor(rangeMinX / stepW) * stepW;
|
||||
for (float x = startX; x <= rangeMaxX; x += stepW)
|
||||
{
|
||||
float sx = mapX(x);
|
||||
g.DrawLine(gridPen, sx, 0, sx, h);
|
||||
g.DrawString(x.ToString("0.##"), font, brush, sx + 2, h - 20);
|
||||
}
|
||||
|
||||
// Horizontal Grid Lines
|
||||
float startY = (float)Math.Floor(rangeMinY / stepH) * stepH;
|
||||
for (float y = startY; y <= rangeMaxY; y += stepH)
|
||||
{
|
||||
float sy = mapY(y);
|
||||
g.DrawLine(gridPen, 0, sy, w, sy);
|
||||
g.DrawString(y.ToString("0.##"), font, brush, 5, sy - 15);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private float CalculateStep(float range, int targetSteps)
|
||||
{
|
||||
float rawStep = range / targetSteps;
|
||||
float mag = (float)Math.Pow(10, Math.Floor(Math.Log10(rawStep)));
|
||||
float normStep = rawStep / mag;
|
||||
|
||||
if (normStep < 1.5) return 1 * mag;
|
||||
if (normStep < 3.5) return 2 * mag;
|
||||
if (normStep < 7.5) return 5 * mag;
|
||||
return 10 * mag;
|
||||
}
|
||||
|
||||
private void pnlCanvas_Resize(object sender, EventArgs e)
|
||||
{
|
||||
pnlCanvas.Invalidate();
|
||||
}
|
||||
}
|
||||
}
|
||||
204
MathApp/MathApp/Controls/MultivariableTaylorControl.Designer.cs
generated
Normal file
204
MathApp/MathApp/Controls/MultivariableTaylorControl.Designer.cs
generated
Normal file
|
|
@ -0,0 +1,204 @@
|
|||
|
||||
namespace MathApp.Controls
|
||||
{
|
||||
partial class MultivariableTaylorControl
|
||||
{
|
||||
private System.ComponentModel.IContainer components = null;
|
||||
|
||||
protected override void Dispose(bool disposing)
|
||||
{
|
||||
if (disposing && (components != null))
|
||||
{
|
||||
components.Dispose();
|
||||
}
|
||||
base.Dispose(disposing);
|
||||
}
|
||||
|
||||
private void InitializeComponent()
|
||||
{
|
||||
this.lblTitle = new System.Windows.Forms.Label();
|
||||
this.lblEquation = new System.Windows.Forms.Label();
|
||||
this.txtEquation = new System.Windows.Forms.TextBox();
|
||||
this.lblOrder = new System.Windows.Forms.Label();
|
||||
this.numOrder = new System.Windows.Forms.NumericUpDown();
|
||||
this.grpVariables = new System.Windows.Forms.GroupBox();
|
||||
this.pnlVariables = new System.Windows.Forms.FlowLayoutPanel();
|
||||
this.btnAddVar = new System.Windows.Forms.Button();
|
||||
this.btnCalculate = new System.Windows.Forms.Button();
|
||||
this.lblResult = new System.Windows.Forms.Label();
|
||||
this.txtResult = new System.Windows.Forms.TextBox();
|
||||
((System.ComponentModel.ISupportInitialize)(this.numOrder)).BeginInit();
|
||||
this.grpVariables.SuspendLayout();
|
||||
this.SuspendLayout();
|
||||
//
|
||||
// lblTitle
|
||||
//
|
||||
this.lblTitle.AutoSize = true;
|
||||
this.lblTitle.Font = new System.Drawing.Font("Segoe UI Light", 18F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point);
|
||||
this.lblTitle.ForeColor = System.Drawing.Color.White;
|
||||
this.lblTitle.Location = new System.Drawing.Point(20, 20);
|
||||
this.lblTitle.Name = "lblTitle";
|
||||
this.lblTitle.Size = new System.Drawing.Size(280, 32);
|
||||
this.lblTitle.TabIndex = 0;
|
||||
this.lblTitle.Text = "Multivariable Taylor Series";
|
||||
//
|
||||
// lblEquation
|
||||
//
|
||||
this.lblEquation.AutoSize = true;
|
||||
this.lblEquation.ForeColor = System.Drawing.Color.White;
|
||||
this.lblEquation.Location = new System.Drawing.Point(23, 70);
|
||||
this.lblEquation.Name = "lblEquation";
|
||||
this.lblEquation.Size = new System.Drawing.Size(89, 15);
|
||||
this.lblEquation.TabIndex = 1;
|
||||
this.lblEquation.Text = "Function f(...) =";
|
||||
//
|
||||
// txtEquation
|
||||
//
|
||||
this.txtEquation.BackColor = System.Drawing.Color.FromArgb(((int)(((byte)(40)))), ((int)(((byte)(40)))), ((int)(((byte)(40)))));
|
||||
this.txtEquation.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle;
|
||||
this.txtEquation.ForeColor = System.Drawing.Color.White;
|
||||
this.txtEquation.Location = new System.Drawing.Point(118, 68);
|
||||
this.txtEquation.Name = "txtEquation";
|
||||
this.txtEquation.Size = new System.Drawing.Size(300, 23);
|
||||
this.txtEquation.TabIndex = 2;
|
||||
this.txtEquation.Text = "sin(x)*cos(y)";
|
||||
//
|
||||
// lblOrder
|
||||
//
|
||||
this.lblOrder.AutoSize = true;
|
||||
this.lblOrder.ForeColor = System.Drawing.Color.White;
|
||||
this.lblOrder.Location = new System.Drawing.Point(440, 70);
|
||||
this.lblOrder.Name = "lblOrder";
|
||||
this.lblOrder.Size = new System.Drawing.Size(74, 15);
|
||||
this.lblOrder.TabIndex = 3;
|
||||
this.lblOrder.Text = "Series Order:";
|
||||
//
|
||||
// numOrder
|
||||
//
|
||||
this.numOrder.BackColor = System.Drawing.Color.FromArgb(((int)(((byte)(40)))), ((int)(((byte)(40)))), ((int)(((byte)(40)))));
|
||||
this.numOrder.ForeColor = System.Drawing.Color.White;
|
||||
this.numOrder.Location = new System.Drawing.Point(520, 68);
|
||||
this.numOrder.Maximum = new decimal(new int[] {
|
||||
10,
|
||||
0,
|
||||
0,
|
||||
0});
|
||||
this.numOrder.Minimum = new decimal(new int[] {
|
||||
1,
|
||||
0,
|
||||
0,
|
||||
0});
|
||||
this.numOrder.Name = "numOrder";
|
||||
this.numOrder.Size = new System.Drawing.Size(60, 23);
|
||||
this.numOrder.TabIndex = 4;
|
||||
this.numOrder.Value = new decimal(new int[] {
|
||||
2,
|
||||
0,
|
||||
0,
|
||||
0});
|
||||
//
|
||||
// grpVariables
|
||||
//
|
||||
this.grpVariables.Controls.Add(this.pnlVariables);
|
||||
this.grpVariables.Controls.Add(this.btnAddVar);
|
||||
this.grpVariables.ForeColor = System.Drawing.Color.White;
|
||||
this.grpVariables.Location = new System.Drawing.Point(23, 110);
|
||||
this.grpVariables.Name = "grpVariables";
|
||||
this.grpVariables.Size = new System.Drawing.Size(557, 200);
|
||||
this.grpVariables.TabIndex = 5;
|
||||
this.grpVariables.TabStop = false;
|
||||
this.grpVariables.Text = "Variables & Expansion Points";
|
||||
//
|
||||
// pnlVariables
|
||||
//
|
||||
this.pnlVariables.AutoScroll = true;
|
||||
this.pnlVariables.Location = new System.Drawing.Point(10, 25);
|
||||
this.pnlVariables.Name = "pnlVariables";
|
||||
this.pnlVariables.Size = new System.Drawing.Size(530, 130);
|
||||
this.pnlVariables.TabIndex = 1;
|
||||
//
|
||||
// btnAddVar
|
||||
//
|
||||
this.btnAddVar.BackColor = System.Drawing.Color.FromArgb(((int)(((byte)(50)))), ((int)(((byte)(50)))), ((int)(((byte)(50)))));
|
||||
this.btnAddVar.FlatAppearance.BorderSize = 0;
|
||||
this.btnAddVar.FlatStyle = System.Windows.Forms.FlatStyle.Flat;
|
||||
this.btnAddVar.Location = new System.Drawing.Point(10, 165);
|
||||
this.btnAddVar.Name = "btnAddVar";
|
||||
this.btnAddVar.Size = new System.Drawing.Size(120, 25);
|
||||
this.btnAddVar.TabIndex = 0;
|
||||
this.btnAddVar.Text = "+ Add Variable";
|
||||
this.btnAddVar.UseVisualStyleBackColor = false;
|
||||
//
|
||||
// btnCalculate
|
||||
//
|
||||
this.btnCalculate.BackColor = System.Drawing.Color.FromArgb(((int)(((byte)(0)))), ((int)(((byte)(120)))), ((int)(((byte)(215)))));
|
||||
this.btnCalculate.FlatStyle = System.Windows.Forms.FlatStyle.Flat;
|
||||
this.btnCalculate.Font = new System.Drawing.Font("Segoe UI", 11F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point);
|
||||
this.btnCalculate.ForeColor = System.Drawing.Color.White;
|
||||
this.btnCalculate.Location = new System.Drawing.Point(23, 330);
|
||||
this.btnCalculate.Name = "btnCalculate";
|
||||
this.btnCalculate.Size = new System.Drawing.Size(150, 40);
|
||||
this.btnCalculate.TabIndex = 6;
|
||||
this.btnCalculate.Text = "Calculate Expansion";
|
||||
this.btnCalculate.UseVisualStyleBackColor = false;
|
||||
//
|
||||
// lblResult
|
||||
//
|
||||
this.lblResult.AutoSize = true;
|
||||
this.lblResult.ForeColor = System.Drawing.Color.White;
|
||||
this.lblResult.Location = new System.Drawing.Point(23, 390);
|
||||
this.lblResult.Name = "lblResult";
|
||||
this.lblResult.Size = new System.Drawing.Size(42, 15);
|
||||
this.lblResult.TabIndex = 7;
|
||||
this.lblResult.Text = "Result:";
|
||||
//
|
||||
// txtResult
|
||||
//
|
||||
this.txtResult.BackColor = System.Drawing.Color.FromArgb(((int)(((byte)(30)))), ((int)(((byte)(30)))), ((int)(((byte)(30)))));
|
||||
this.txtResult.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle;
|
||||
this.txtResult.Font = new System.Drawing.Font("Consolas", 10F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point);
|
||||
this.txtResult.ForeColor = System.Drawing.Color.Cyan;
|
||||
this.txtResult.Location = new System.Drawing.Point(23, 410);
|
||||
this.txtResult.Multiline = true;
|
||||
this.txtResult.Name = "txtResult";
|
||||
this.txtResult.ReadOnly = true;
|
||||
this.txtResult.ScrollBars = System.Windows.Forms.ScrollBars.Vertical;
|
||||
this.txtResult.Size = new System.Drawing.Size(557, 150);
|
||||
this.txtResult.TabIndex = 8;
|
||||
//
|
||||
// MultivariableTaylorControl
|
||||
//
|
||||
this.AutoScaleDimensions = new System.Drawing.SizeF(7F, 15F);
|
||||
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
|
||||
this.BackColor = System.Drawing.Color.FromArgb(((int)(((byte)(20)))), ((int)(((byte)(20)))), ((int)(((byte)(20)))));
|
||||
this.Controls.Add(this.txtResult);
|
||||
this.Controls.Add(this.lblResult);
|
||||
this.Controls.Add(this.btnCalculate);
|
||||
this.Controls.Add(this.grpVariables);
|
||||
this.Controls.Add(this.numOrder);
|
||||
this.Controls.Add(this.lblOrder);
|
||||
this.Controls.Add(this.txtEquation);
|
||||
this.Controls.Add(this.lblEquation);
|
||||
this.Controls.Add(this.lblTitle);
|
||||
this.Name = "MultivariableTaylorControl";
|
||||
this.Size = new System.Drawing.Size(600, 600);
|
||||
((System.ComponentModel.ISupportInitialize)(this.numOrder)).EndInit();
|
||||
this.grpVariables.ResumeLayout(false);
|
||||
this.ResumeLayout(false);
|
||||
this.PerformLayout();
|
||||
|
||||
}
|
||||
|
||||
private System.Windows.Forms.Label lblTitle;
|
||||
private System.Windows.Forms.Label lblEquation;
|
||||
private System.Windows.Forms.TextBox txtEquation;
|
||||
private System.Windows.Forms.Label lblOrder;
|
||||
private System.Windows.Forms.NumericUpDown numOrder;
|
||||
private System.Windows.Forms.GroupBox grpVariables;
|
||||
private System.Windows.Forms.FlowLayoutPanel pnlVariables;
|
||||
private System.Windows.Forms.Button btnAddVar;
|
||||
private System.Windows.Forms.Button btnCalculate;
|
||||
private System.Windows.Forms.Label lblResult;
|
||||
private System.Windows.Forms.TextBox txtResult;
|
||||
}
|
||||
}
|
||||
123
MathApp/MathApp/Controls/MultivariableTaylorControl.cs
Normal file
123
MathApp/MathApp/Controls/MultivariableTaylorControl.cs
Normal file
|
|
@ -0,0 +1,123 @@
|
|||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Drawing;
|
||||
using System.Windows.Forms;
|
||||
using System.Globalization;
|
||||
|
||||
namespace MathApp.Controls
|
||||
{
|
||||
public partial class MultivariableTaylorControl : UserControl
|
||||
{
|
||||
private List<VariableInputRow> variableRows = new List<VariableInputRow>();
|
||||
|
||||
public MultivariableTaylorControl()
|
||||
{
|
||||
InitializeComponent();
|
||||
btnAddVar.Click += BtnAddVar_Click;
|
||||
btnCalculate.Click += BtnCalculate_Click;
|
||||
|
||||
// Add initial variables inputs (x and y default)
|
||||
AddVariableRow("x", "0");
|
||||
AddVariableRow("y", "0");
|
||||
}
|
||||
|
||||
private void BtnAddVar_Click(object sender, EventArgs e)
|
||||
{
|
||||
AddVariableRow("", "0");
|
||||
}
|
||||
|
||||
private void AddVariableRow(string varName, string val)
|
||||
{
|
||||
VariableInputRow row = new VariableInputRow(varName, val);
|
||||
row.OnRemove += (s, e) =>
|
||||
{
|
||||
pnlVariables.Controls.Remove(row.Panel);
|
||||
variableRows.Remove(row);
|
||||
};
|
||||
|
||||
pnlVariables.Controls.Add(row.Panel);
|
||||
variableRows.Add(row);
|
||||
}
|
||||
|
||||
private void BtnCalculate_Click(object sender, EventArgs e)
|
||||
{
|
||||
try
|
||||
{
|
||||
string eq = txtEquation.Text;
|
||||
int degree = (int)numOrder.Value;
|
||||
Dictionary<string, decimal> points = new Dictionary<string, decimal>();
|
||||
|
||||
foreach(var row in variableRows)
|
||||
{
|
||||
string v = row.Variable;
|
||||
if(string.IsNullOrWhiteSpace(v)) continue;
|
||||
|
||||
decimal p = 0;
|
||||
if(!decimal.TryParse(row.Value, NumberStyles.Any, CultureInfo.InvariantCulture, out p))
|
||||
{
|
||||
txtResult.Text = "Invalid number format for variable: " + v;
|
||||
return;
|
||||
}
|
||||
|
||||
if(points.ContainsKey(v))
|
||||
{
|
||||
txtResult.Text = "Duplicate variable: " + v;
|
||||
return;
|
||||
}
|
||||
points.Add(v, p);
|
||||
}
|
||||
|
||||
if(points.Count == 0)
|
||||
{
|
||||
txtResult.Text = "Please add at least one variable.";
|
||||
return;
|
||||
}
|
||||
|
||||
string result = NumericalMethods.MethodMultiVar_TaylorSeries(eq, degree, points);
|
||||
|
||||
// Formatting clean up
|
||||
result = result.Replace("+ -", "- ").Replace("+-", "- ").Replace("*", " * ");
|
||||
|
||||
txtResult.Text = result;
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
txtResult.Text = "Error: " + ex.Message;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Helper class for dynamic rows
|
||||
public class VariableInputRow
|
||||
{
|
||||
public Panel Panel { get; private set; }
|
||||
public TextBox TxtVariable { get; private set; }
|
||||
public TextBox TxtValue { get; private set; }
|
||||
public EventHandler OnRemove;
|
||||
|
||||
public string Variable => TxtVariable.Text.Trim();
|
||||
public string Value => TxtValue.Text.Trim();
|
||||
|
||||
public VariableInputRow(string varName, string val)
|
||||
{
|
||||
Panel = new Panel();
|
||||
Panel.Size = new Size(500, 30);
|
||||
|
||||
Label lblVar = new Label { Text = "Var:", Location = new Point(0, 5), AutoSize = true, ForeColor = Color.White };
|
||||
TxtVariable = new TextBox { Text = varName, Location = new Point(40, 2), Size = new Size(100, 23), BackColor = Color.FromArgb(40,40,40), ForeColor = Color.White, BorderStyle = BorderStyle.FixedSingle };
|
||||
|
||||
Label lblVal = new Label { Text = "Point:", Location = new Point(160, 5), AutoSize = true, ForeColor = Color.White };
|
||||
TxtValue = new TextBox { Text = val, Location = new Point(210, 2), Size = new Size(100, 23), BackColor = Color.FromArgb(40,40,40), ForeColor = Color.White, BorderStyle = BorderStyle.FixedSingle };
|
||||
|
||||
Button btnRemove = new Button { Text = "X", Location = new Point(330, 2), Size = new Size(30, 23), BackColor = Color.Red, ForeColor = Color.White, FlatStyle = FlatStyle.Flat };
|
||||
btnRemove.FlatAppearance.BorderSize = 0;
|
||||
btnRemove.Click += (s, e) => OnRemove?.Invoke(this, EventArgs.Empty);
|
||||
|
||||
Panel.Controls.Add(lblVar);
|
||||
Panel.Controls.Add(TxtVariable);
|
||||
Panel.Controls.Add(lblVal);
|
||||
Panel.Controls.Add(TxtValue);
|
||||
Panel.Controls.Add(btnRemove);
|
||||
}
|
||||
}
|
||||
}
|
||||
222
MathApp/MathApp/Controls/RootFinderControl.Designer.cs
generated
Normal file
222
MathApp/MathApp/Controls/RootFinderControl.Designer.cs
generated
Normal file
|
|
@ -0,0 +1,222 @@
|
|||
|
||||
namespace MathApp.Controls
|
||||
{
|
||||
partial class RootFinderControl
|
||||
{
|
||||
private System.ComponentModel.IContainer components = null;
|
||||
|
||||
protected override void Dispose(bool disposing)
|
||||
{
|
||||
if (disposing && (components != null))
|
||||
{
|
||||
components.Dispose();
|
||||
}
|
||||
base.Dispose(disposing);
|
||||
}
|
||||
|
||||
private void InitializeComponent()
|
||||
{
|
||||
this.lblTitle = new System.Windows.Forms.Label();
|
||||
this.pnlContent = new System.Windows.Forms.Panel();
|
||||
|
||||
this.lblMethod = new System.Windows.Forms.Label();
|
||||
this.cmbMethod = new System.Windows.Forms.ComboBox();
|
||||
this.lblEquation = new System.Windows.Forms.Label();
|
||||
this.txtEquation = new System.Windows.Forms.TextBox();
|
||||
this.lblVariable = new System.Windows.Forms.Label();
|
||||
this.txtVariable = new System.Windows.Forms.TextBox();
|
||||
this.lblInitialPoint = new System.Windows.Forms.Label();
|
||||
this.txtInitialPoint = new System.Windows.Forms.TextBox();
|
||||
this.lblMinX = new System.Windows.Forms.Label();
|
||||
this.txtMinX = new System.Windows.Forms.TextBox();
|
||||
this.lblMaxX = new System.Windows.Forms.Label();
|
||||
this.txtMaxX = new System.Windows.Forms.TextBox();
|
||||
this.lblIterations = new System.Windows.Forms.Label();
|
||||
this.txtIterations = new System.Windows.Forms.TextBox();
|
||||
this.btnFindRoot = new System.Windows.Forms.Button();
|
||||
this.lblResult = new System.Windows.Forms.Label();
|
||||
this.txtResult = new System.Windows.Forms.TextBox();
|
||||
|
||||
this.pnlContent.SuspendLayout();
|
||||
this.SuspendLayout();
|
||||
|
||||
//
|
||||
// lblTitle
|
||||
//
|
||||
this.lblTitle.Dock = System.Windows.Forms.DockStyle.Top;
|
||||
this.lblTitle.Font = new System.Drawing.Font("Segoe UI Light", 24F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point);
|
||||
this.lblTitle.ForeColor = System.Drawing.Color.White;
|
||||
this.lblTitle.Location = new System.Drawing.Point(0, 0);
|
||||
this.lblTitle.Name = "lblTitle";
|
||||
this.lblTitle.Size = new System.Drawing.Size(800, 60);
|
||||
this.lblTitle.TabIndex = 0;
|
||||
this.lblTitle.Text = "Equation Root Finder";
|
||||
this.lblTitle.TextAlign = System.Drawing.ContentAlignment.MiddleLeft;
|
||||
|
||||
//
|
||||
// pnlContent
|
||||
//
|
||||
this.pnlContent.Controls.Add(this.txtResult);
|
||||
this.pnlContent.Controls.Add(this.lblResult);
|
||||
this.pnlContent.Controls.Add(this.btnFindRoot);
|
||||
this.pnlContent.Controls.Add(this.txtIterations);
|
||||
this.pnlContent.Controls.Add(this.lblIterations);
|
||||
this.pnlContent.Controls.Add(this.txtMaxX);
|
||||
this.pnlContent.Controls.Add(this.lblMaxX);
|
||||
this.pnlContent.Controls.Add(this.txtMinX);
|
||||
this.pnlContent.Controls.Add(this.lblMinX);
|
||||
this.pnlContent.Controls.Add(this.txtInitialPoint);
|
||||
this.pnlContent.Controls.Add(this.lblInitialPoint);
|
||||
this.pnlContent.Controls.Add(this.txtVariable);
|
||||
this.pnlContent.Controls.Add(this.lblVariable);
|
||||
this.pnlContent.Controls.Add(this.txtEquation);
|
||||
this.pnlContent.Controls.Add(this.lblEquation);
|
||||
this.pnlContent.Controls.Add(this.cmbMethod);
|
||||
this.pnlContent.Controls.Add(this.lblMethod);
|
||||
this.pnlContent.Dock = System.Windows.Forms.DockStyle.Fill;
|
||||
this.pnlContent.Location = new System.Drawing.Point(0, 60);
|
||||
this.pnlContent.Name = "pnlContent";
|
||||
this.pnlContent.Size = new System.Drawing.Size(800, 540);
|
||||
this.pnlContent.TabIndex = 1;
|
||||
|
||||
// Simple layout similar to previous, but styling updated in logic or here
|
||||
int yObject = 20;
|
||||
|
||||
// Helper to style labels
|
||||
void StyleLabel(System.Windows.Forms.Label l) {
|
||||
l.AutoSize = true;
|
||||
l.Font = new System.Drawing.Font("Segoe UI", 10F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point);
|
||||
l.ForeColor = System.Drawing.Color.White;
|
||||
}
|
||||
|
||||
StyleLabel(this.lblMethod);
|
||||
this.lblMethod.Location = new System.Drawing.Point(20, yObject);
|
||||
this.lblMethod.Text = "Method:";
|
||||
|
||||
this.cmbMethod.FormattingEnabled = true;
|
||||
this.cmbMethod.Items.AddRange(new object[] { "Newton-Raphson", "Bisection", "Secant", "Regula Falsi" });
|
||||
this.cmbMethod.Location = new System.Drawing.Point(150, yObject);
|
||||
this.cmbMethod.Name = "cmbMethod";
|
||||
this.cmbMethod.Size = new System.Drawing.Size(200, 25);
|
||||
this.cmbMethod.DropDownStyle = System.Windows.Forms.ComboBoxStyle.DropDownList;
|
||||
yObject += 50;
|
||||
|
||||
StyleLabel(this.lblEquation);
|
||||
this.lblEquation.Location = new System.Drawing.Point(20, yObject);
|
||||
this.lblEquation.Text = "Equation:";
|
||||
this.txtEquation.BackColor = System.Drawing.Color.FromArgb(((int)(((byte)(40)))), ((int)(((byte)(40)))), ((int)(((byte)(40)))));
|
||||
this.txtEquation.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle;
|
||||
this.txtEquation.ForeColor = System.Drawing.Color.White;
|
||||
this.txtEquation.Location = new System.Drawing.Point(150, yObject);
|
||||
this.txtEquation.Size = new System.Drawing.Size(400, 25);
|
||||
yObject += 50;
|
||||
|
||||
StyleLabel(this.lblVariable);
|
||||
this.lblVariable.Location = new System.Drawing.Point(20, yObject);
|
||||
this.lblVariable.Text = "Variable (e.g. x):";
|
||||
this.txtVariable.BackColor = System.Drawing.Color.FromArgb(((int)(((byte)(40)))), ((int)(((byte)(40)))), ((int)(((byte)(40)))));
|
||||
this.txtVariable.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle;
|
||||
this.txtVariable.ForeColor = System.Drawing.Color.White;
|
||||
this.txtVariable.Location = new System.Drawing.Point(150, yObject);
|
||||
this.txtVariable.Size = new System.Drawing.Size(100, 25);
|
||||
yObject += 50;
|
||||
|
||||
StyleLabel(this.lblInitialPoint);
|
||||
this.lblInitialPoint.Location = new System.Drawing.Point(20, yObject);
|
||||
this.lblInitialPoint.Text = "Initial Point:";
|
||||
this.txtInitialPoint.BackColor = System.Drawing.Color.FromArgb(((int)(((byte)(40)))), ((int)(((byte)(40)))), ((int)(((byte)(40)))));
|
||||
this.txtInitialPoint.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle;
|
||||
this.txtInitialPoint.ForeColor = System.Drawing.Color.White;
|
||||
this.txtInitialPoint.Location = new System.Drawing.Point(150, yObject);
|
||||
this.txtInitialPoint.Size = new System.Drawing.Size(100, 25);
|
||||
|
||||
StyleLabel(this.lblMinX);
|
||||
this.lblMinX.Location = new System.Drawing.Point(20, yObject);
|
||||
this.lblMinX.Text = "Min X:";
|
||||
this.txtMinX.BackColor = System.Drawing.Color.FromArgb(((int)(((byte)(40)))), ((int)(((byte)(40)))), ((int)(((byte)(40)))));
|
||||
this.txtMinX.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle;
|
||||
this.txtMinX.ForeColor = System.Drawing.Color.White;
|
||||
this.txtMinX.Location = new System.Drawing.Point(150, yObject);
|
||||
this.txtMinX.Size = new System.Drawing.Size(100, 25);
|
||||
|
||||
StyleLabel(this.lblMaxX);
|
||||
this.lblMaxX.Location = new System.Drawing.Point(280, yObject);
|
||||
this.lblMaxX.Text = "Max X:";
|
||||
this.txtMaxX.BackColor = System.Drawing.Color.FromArgb(((int)(((byte)(40)))), ((int)(((byte)(40)))), ((int)(((byte)(40)))));
|
||||
this.txtMaxX.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle;
|
||||
this.txtMaxX.ForeColor = System.Drawing.Color.White;
|
||||
this.txtMaxX.Location = new System.Drawing.Point(340, yObject);
|
||||
this.txtMaxX.Size = new System.Drawing.Size(100, 25);
|
||||
yObject += 50;
|
||||
|
||||
StyleLabel(this.lblIterations);
|
||||
this.lblIterations.Location = new System.Drawing.Point(20, yObject);
|
||||
this.lblIterations.Text = "Iterations:";
|
||||
this.txtIterations.BackColor = System.Drawing.Color.FromArgb(((int)(((byte)(40)))), ((int)(((byte)(40)))), ((int)(((byte)(40)))));
|
||||
this.txtIterations.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle;
|
||||
this.txtIterations.ForeColor = System.Drawing.Color.White;
|
||||
this.txtIterations.Location = new System.Drawing.Point(150, yObject);
|
||||
this.txtIterations.Size = new System.Drawing.Size(100, 25);
|
||||
this.txtIterations.Text = "100";
|
||||
yObject += 60;
|
||||
|
||||
this.btnFindRoot.Location = new System.Drawing.Point(150, yObject);
|
||||
this.btnFindRoot.Name = "btnFindRoot";
|
||||
this.btnFindRoot.Size = new System.Drawing.Size(150, 40);
|
||||
this.btnFindRoot.Text = "Find Root";
|
||||
this.btnFindRoot.FlatStyle = System.Windows.Forms.FlatStyle.Flat;
|
||||
this.btnFindRoot.BackColor = System.Drawing.Color.FromArgb(0, 120, 215);
|
||||
this.btnFindRoot.ForeColor = System.Drawing.Color.White;
|
||||
this.btnFindRoot.Font = new System.Drawing.Font("Segoe UI Semibold", 10F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point);
|
||||
this.btnFindRoot.FlatAppearance.BorderSize = 0;
|
||||
yObject += 60;
|
||||
|
||||
StyleLabel(this.lblResult);
|
||||
this.lblResult.Location = new System.Drawing.Point(20, yObject);
|
||||
this.lblResult.Text = "Result:";
|
||||
this.txtResult.BackColor = System.Drawing.Color.FromArgb(((int)(((byte)(30)))), ((int)(((byte)(30)))), ((int)(((byte)(30)))));
|
||||
this.txtResult.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle;
|
||||
this.txtResult.ForeColor = System.Drawing.Color.White;
|
||||
this.txtResult.Location = new System.Drawing.Point(150, yObject);
|
||||
this.txtResult.Size = new System.Drawing.Size(600, 150);
|
||||
this.txtResult.Multiline = true;
|
||||
this.txtResult.ScrollBars = System.Windows.Forms.ScrollBars.Vertical;
|
||||
this.txtResult.Font = new System.Drawing.Font("Consolas", 10F);
|
||||
|
||||
//
|
||||
// RootFinderControl
|
||||
//
|
||||
this.AutoScaleDimensions = new System.Drawing.SizeF(7F, 15F);
|
||||
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
|
||||
this.BackColor = System.Drawing.Color.FromArgb(((int)(((byte)(20)))), ((int)(((byte)(20)))), ((int)(((byte)(20)))));
|
||||
this.Controls.Add(this.pnlContent);
|
||||
this.Controls.Add(this.lblTitle);
|
||||
this.Name = "RootFinderControl";
|
||||
this.Size = new System.Drawing.Size(800, 600);
|
||||
this.pnlContent.ResumeLayout(false);
|
||||
this.pnlContent.PerformLayout();
|
||||
this.ResumeLayout(false);
|
||||
|
||||
}
|
||||
|
||||
private System.Windows.Forms.Label lblTitle;
|
||||
private System.Windows.Forms.Panel pnlContent;
|
||||
private System.Windows.Forms.Label lblMethod;
|
||||
public System.Windows.Forms.ComboBox cmbMethod;
|
||||
private System.Windows.Forms.Label lblEquation;
|
||||
public System.Windows.Forms.TextBox txtEquation;
|
||||
private System.Windows.Forms.Label lblVariable;
|
||||
public System.Windows.Forms.TextBox txtVariable;
|
||||
private System.Windows.Forms.Label lblInitialPoint;
|
||||
public System.Windows.Forms.TextBox txtInitialPoint;
|
||||
private System.Windows.Forms.Label lblMinX;
|
||||
public System.Windows.Forms.TextBox txtMinX;
|
||||
private System.Windows.Forms.Label lblMaxX;
|
||||
public System.Windows.Forms.TextBox txtMaxX;
|
||||
private System.Windows.Forms.Label lblIterations;
|
||||
public System.Windows.Forms.TextBox txtIterations;
|
||||
public System.Windows.Forms.Button btnFindRoot;
|
||||
private System.Windows.Forms.Label lblResult;
|
||||
public System.Windows.Forms.TextBox txtResult;
|
||||
}
|
||||
}
|
||||
80
MathApp/MathApp/Controls/RootFinderControl.cs
Normal file
80
MathApp/MathApp/Controls/RootFinderControl.cs
Normal file
|
|
@ -0,0 +1,80 @@
|
|||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Windows.Forms;
|
||||
|
||||
namespace MathApp.Controls
|
||||
{
|
||||
public partial class RootFinderControl : UserControl
|
||||
{
|
||||
public RootFinderControl()
|
||||
{
|
||||
InitializeComponent();
|
||||
cmbMethod.SelectedIndex = 0;
|
||||
UpdateVisibility();
|
||||
cmbMethod.SelectedIndexChanged += (s, e) => UpdateVisibility();
|
||||
btnFindRoot.Click += BtnFindRoot_Click;
|
||||
}
|
||||
|
||||
private void UpdateVisibility()
|
||||
{
|
||||
string method = cmbMethod.SelectedItem?.ToString() ?? "";
|
||||
bool isBracket = method == "Bisection" || method == "Regula Falsi";
|
||||
|
||||
lblInitialPoint.Visible = !isBracket;
|
||||
txtInitialPoint.Visible = !isBracket;
|
||||
|
||||
lblMinX.Visible = isBracket;
|
||||
txtMinX.Visible = isBracket;
|
||||
lblMaxX.Visible = isBracket;
|
||||
txtMaxX.Visible = isBracket;
|
||||
}
|
||||
|
||||
private void BtnFindRoot_Click(object sender, EventArgs e)
|
||||
{
|
||||
try
|
||||
{
|
||||
string eq = txtEquation.Text;
|
||||
string variable = txtVariable.Text;
|
||||
int iterCount = 100;
|
||||
int.TryParse(txtIterations.Text, out iterCount);
|
||||
string method = cmbMethod.SelectedItem.ToString();
|
||||
|
||||
List<string> logs = new List<string>();
|
||||
|
||||
if (method == "Newton-Raphson")
|
||||
{
|
||||
decimal initialP = decimal.Parse(txtInitialPoint.Text);
|
||||
logs = NumericalMethods.Method_NewtonRaphson(eq, initialP, iterCount);
|
||||
}
|
||||
else if (method == "Bisection")
|
||||
{
|
||||
decimal minX = decimal.Parse(txtMinX.Text);
|
||||
decimal maxX = decimal.Parse(txtMaxX.Text);
|
||||
logs = NumericalMethods.Method_Bisection(eq, minX, maxX, iterCount);
|
||||
}
|
||||
else if (method == "Secant")
|
||||
{
|
||||
decimal minX = decimal.Parse(txtMinX.Text);
|
||||
decimal maxX = decimal.Parse(txtMaxX.Text);
|
||||
logs = NumericalMethods.Method_Secant(eq, minX, maxX, iterCount);
|
||||
}
|
||||
else if (method == "Regula Falsi")
|
||||
{
|
||||
decimal minX = decimal.Parse(txtMinX.Text);
|
||||
decimal maxX = decimal.Parse(txtMaxX.Text);
|
||||
logs = NumericalMethods.Method_RegulaFalsi(eq, minX, maxX, iterCount);
|
||||
}
|
||||
else
|
||||
{
|
||||
logs = new List<string> { method + " method not yet active." };
|
||||
}
|
||||
|
||||
txtResult.Text = string.Join(Environment.NewLine, logs);
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
txtResult.Text = "Error: " + ex.Message;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
76
MathApp/MathApp/Controls/SimpleCalculatorControl.Designer.cs
generated
Normal file
76
MathApp/MathApp/Controls/SimpleCalculatorControl.Designer.cs
generated
Normal file
|
|
@ -0,0 +1,76 @@
|
|||
|
||||
namespace MathApp.Controls
|
||||
{
|
||||
partial class SimpleCalculatorControl
|
||||
{
|
||||
private System.ComponentModel.IContainer components = null;
|
||||
|
||||
protected override void Dispose(bool disposing)
|
||||
{
|
||||
if (disposing && (components != null))
|
||||
{
|
||||
components.Dispose();
|
||||
}
|
||||
base.Dispose(disposing);
|
||||
}
|
||||
|
||||
private void InitializeComponent()
|
||||
{
|
||||
this.txtDisplay = new System.Windows.Forms.TextBox();
|
||||
this.pnlButtons = new System.Windows.Forms.TableLayoutPanel();
|
||||
this.SuspendLayout();
|
||||
|
||||
//
|
||||
// txtDisplay
|
||||
//
|
||||
this.txtDisplay.BackColor = System.Drawing.Color.FromArgb(((int)(((byte)(32)))), ((int)(((byte)(32)))), ((int)(((byte)(32)))));
|
||||
this.txtDisplay.BorderStyle = System.Windows.Forms.BorderStyle.None;
|
||||
this.txtDisplay.Dock = System.Windows.Forms.DockStyle.Top;
|
||||
this.txtDisplay.Font = new System.Drawing.Font("Segoe UI", 36F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point);
|
||||
this.txtDisplay.ForeColor = System.Drawing.Color.White;
|
||||
this.txtDisplay.Location = new System.Drawing.Point(0, 0);
|
||||
this.txtDisplay.Name = "txtDisplay";
|
||||
this.txtDisplay.ReadOnly = true;
|
||||
this.txtDisplay.Size = new System.Drawing.Size(600, 64);
|
||||
this.txtDisplay.TabIndex = 0;
|
||||
this.txtDisplay.Text = "0";
|
||||
this.txtDisplay.TextAlign = System.Windows.Forms.HorizontalAlignment.Right;
|
||||
|
||||
//
|
||||
// pnlButtons
|
||||
//
|
||||
this.pnlButtons.ColumnCount = 4;
|
||||
this.pnlButtons.ColumnStyles.Add(new System.Windows.Forms.ColumnStyle(System.Windows.Forms.SizeType.Percent, 25F));
|
||||
this.pnlButtons.ColumnStyles.Add(new System.Windows.Forms.ColumnStyle(System.Windows.Forms.SizeType.Percent, 25F));
|
||||
this.pnlButtons.ColumnStyles.Add(new System.Windows.Forms.ColumnStyle(System.Windows.Forms.SizeType.Percent, 25F));
|
||||
this.pnlButtons.ColumnStyles.Add(new System.Windows.Forms.ColumnStyle(System.Windows.Forms.SizeType.Percent, 25F));
|
||||
this.pnlButtons.Dock = System.Windows.Forms.DockStyle.Fill;
|
||||
this.pnlButtons.Location = new System.Drawing.Point(0, 64);
|
||||
this.pnlButtons.Name = "pnlButtons";
|
||||
this.pnlButtons.RowCount = 5;
|
||||
this.pnlButtons.RowStyles.Add(new System.Windows.Forms.RowStyle(System.Windows.Forms.SizeType.Percent, 20F));
|
||||
this.pnlButtons.RowStyles.Add(new System.Windows.Forms.RowStyle(System.Windows.Forms.SizeType.Percent, 20F));
|
||||
this.pnlButtons.RowStyles.Add(new System.Windows.Forms.RowStyle(System.Windows.Forms.SizeType.Percent, 20F));
|
||||
this.pnlButtons.RowStyles.Add(new System.Windows.Forms.RowStyle(System.Windows.Forms.SizeType.Percent, 20F));
|
||||
this.pnlButtons.RowStyles.Add(new System.Windows.Forms.RowStyle(System.Windows.Forms.SizeType.Percent, 20F));
|
||||
this.pnlButtons.Size = new System.Drawing.Size(600, 536);
|
||||
this.pnlButtons.TabIndex = 1;
|
||||
|
||||
//
|
||||
// SimpleCalculatorControl
|
||||
//
|
||||
this.AutoScaleDimensions = new System.Drawing.SizeF(7F, 15F);
|
||||
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
|
||||
this.BackColor = System.Drawing.Color.FromArgb(((int)(((byte)(20)))), ((int)(((byte)(20)))), ((int)(((byte)(20)))));
|
||||
this.Controls.Add(this.pnlButtons);
|
||||
this.Controls.Add(this.txtDisplay);
|
||||
this.Name = "SimpleCalculatorControl";
|
||||
this.Size = new System.Drawing.Size(600, 600);
|
||||
this.ResumeLayout(false);
|
||||
this.PerformLayout();
|
||||
}
|
||||
|
||||
private System.Windows.Forms.TextBox txtDisplay;
|
||||
private System.Windows.Forms.TableLayoutPanel pnlButtons;
|
||||
}
|
||||
}
|
||||
251
MathApp/MathApp/Controls/SimpleCalculatorControl.cs
Normal file
251
MathApp/MathApp/Controls/SimpleCalculatorControl.cs
Normal file
|
|
@ -0,0 +1,251 @@
|
|||
using System;
|
||||
using System.Drawing;
|
||||
using System.Windows.Forms;
|
||||
|
||||
namespace MathApp.Controls
|
||||
{
|
||||
public partial class SimpleCalculatorControl : UserControl
|
||||
{
|
||||
private string currentExpression = "";
|
||||
private bool isResultShown = false;
|
||||
|
||||
public SimpleCalculatorControl()
|
||||
{
|
||||
InitializeComponent();
|
||||
InitializeButtons();
|
||||
}
|
||||
|
||||
private void InitializeButtons()
|
||||
{
|
||||
// ... (Keeping initialization logic same, but for brevity I will rewrite it to ensure file integrity)
|
||||
// Actually, I should preserve the exact logic.
|
||||
// Re-implementing InitializeButtons to Match previous content exactly
|
||||
|
||||
this.pnlButtons.RowCount = 5;
|
||||
this.pnlButtons.RowStyles.Clear();
|
||||
for(int i=0; i<5; i++) this.pnlButtons.RowStyles.Add(new RowStyle(SizeType.Percent, 20F));
|
||||
|
||||
string[] gridLayout = {
|
||||
"C", "(", ")", "/",
|
||||
"7", "8", "9", "*",
|
||||
"4", "5", "6", "-",
|
||||
"1", "2", "3", "+",
|
||||
"0", ".", "DEL", "="
|
||||
};
|
||||
|
||||
int index = 0;
|
||||
foreach (string text in gridLayout)
|
||||
{
|
||||
Button btn = new Button();
|
||||
btn.Text = text;
|
||||
btn.Dock = DockStyle.Fill;
|
||||
btn.FlatStyle = FlatStyle.Flat;
|
||||
btn.FlatAppearance.BorderSize = 0;
|
||||
btn.Font = new Font("Segoe UI", 18F, FontStyle.Regular, GraphicsUnit.Point);
|
||||
btn.ForeColor = Color.White;
|
||||
btn.Click += Btn_Click;
|
||||
|
||||
// Tag lookup for keyboard maybe?
|
||||
btn.Tag = text;
|
||||
|
||||
if (IsOperator(text))
|
||||
{
|
||||
btn.BackColor = Color.FromArgb(40, 40, 40);
|
||||
btn.FlatAppearance.MouseOverBackColor = Color.FromArgb(60, 60, 60);
|
||||
}
|
||||
else if (text == "=")
|
||||
{
|
||||
btn.BackColor = Color.FromArgb(0, 120, 215);
|
||||
btn.FlatAppearance.MouseOverBackColor = Color.FromArgb(0, 140, 240);
|
||||
}
|
||||
else if (text == "C" || text == "DEL")
|
||||
{
|
||||
btn.BackColor = Color.FromArgb(50, 50, 50);
|
||||
btn.FlatAppearance.MouseOverBackColor = Color.FromArgb(70, 70, 70);
|
||||
}
|
||||
else
|
||||
{
|
||||
btn.BackColor = Color.FromArgb(30, 30, 30);
|
||||
btn.FlatAppearance.MouseOverBackColor = Color.FromArgb(50, 50, 50);
|
||||
}
|
||||
|
||||
pnlButtons.Controls.Add(btn, index % 4, index / 4);
|
||||
index++;
|
||||
}
|
||||
}
|
||||
|
||||
private bool IsOperator(string text)
|
||||
{
|
||||
return text == "+" || text == "-" || text == "*" || text == "/";
|
||||
}
|
||||
|
||||
// --- Keyboard Support ---
|
||||
protected override bool ProcessCmdKey(ref Message msg, Keys keyData)
|
||||
{
|
||||
// Map keys
|
||||
string input = "";
|
||||
bool handled = true;
|
||||
|
||||
switch (keyData)
|
||||
{
|
||||
case Keys.NumPad0:
|
||||
case Keys.D0: input = "0"; break;
|
||||
case Keys.NumPad1:
|
||||
case Keys.D1: input = "1"; break;
|
||||
case Keys.NumPad2:
|
||||
case Keys.D2: input = "2"; break;
|
||||
case Keys.NumPad3:
|
||||
case Keys.D3: input = "3"; break;
|
||||
case Keys.NumPad4:
|
||||
case Keys.D4: input = "4"; break;
|
||||
case Keys.NumPad5:
|
||||
case Keys.D5: input = "5"; break;
|
||||
case Keys.NumPad6:
|
||||
case Keys.D6: input = "6"; break;
|
||||
case Keys.NumPad7:
|
||||
case Keys.D7: input = "7"; break;
|
||||
case Keys.NumPad8:
|
||||
case Keys.D8: input = "8"; break;
|
||||
case Keys.NumPad9:
|
||||
case Keys.D9: input = "9"; break;
|
||||
|
||||
case Keys.Add:
|
||||
case Keys.Oemplus: // Careful with Shift+=
|
||||
input = "+"; break;
|
||||
case Keys.Subtract:
|
||||
case Keys.OemMinus:
|
||||
input = "-"; break;
|
||||
case Keys.Multiply:
|
||||
input = "*"; break;
|
||||
case Keys.Divide:
|
||||
case Keys.OemQuestion: // /
|
||||
input = "/"; break;
|
||||
|
||||
case Keys.Decimal:
|
||||
case Keys.OemPeriod:
|
||||
case Keys.Oemcomma:
|
||||
input = "."; break;
|
||||
|
||||
case Keys.Back:
|
||||
case Keys.Delete:
|
||||
input = "DEL"; break;
|
||||
|
||||
case Keys.Escape:
|
||||
input = "C"; break;
|
||||
|
||||
case Keys.Enter:
|
||||
case Keys.Return:
|
||||
input = "="; break;
|
||||
|
||||
// Shift+8 (*) etc are handled by KeyChar usually vs Keys,
|
||||
// but ProcessCmdKey sees Keys.
|
||||
// For simplicity let's stick to Numpad and basics.
|
||||
// Shift handling in ProcessCmdKey is tricky (keyData & Keys.Shift)
|
||||
default:
|
||||
// Check for shift combinations
|
||||
if (keyData == (Keys.Shift | Keys.D8)) input = "*";
|
||||
else if (keyData == (Keys.Shift | Keys.D9)) input = "(";
|
||||
else if (keyData == (Keys.Shift | Keys.D0)) input = ")";
|
||||
else if (keyData == (Keys.Shift | Keys.Oemplus)) input = "+";
|
||||
else handled = false;
|
||||
break;
|
||||
}
|
||||
|
||||
if (handled && input != "")
|
||||
{
|
||||
ProcessInput(input);
|
||||
return true;
|
||||
}
|
||||
|
||||
return base.ProcessCmdKey(ref msg, keyData);
|
||||
}
|
||||
|
||||
private void Btn_Click(object sender, EventArgs e)
|
||||
{
|
||||
Button btn = sender as Button;
|
||||
if (btn == null) return;
|
||||
ProcessInput(btn.Text);
|
||||
|
||||
// Remove focus from button so future key presses go to control/form?
|
||||
// Actually ProcessCmdKey bubbles up so it's fine.
|
||||
// But if button has focus, Enter triggers Click AND ProcessCmdKey might double trigger
|
||||
// if we are not careful.
|
||||
// ProcessCmdKey is called BEFORE key events. returning true eats it.
|
||||
// So Enter won't click the focused button if we return true.
|
||||
}
|
||||
|
||||
private void ProcessInput(string text)
|
||||
{
|
||||
if (text == "C")
|
||||
{
|
||||
currentExpression = "";
|
||||
txtDisplay.Text = "0";
|
||||
isResultShown = false;
|
||||
}
|
||||
else if (text == "DEL")
|
||||
{
|
||||
if (isResultShown)
|
||||
{
|
||||
currentExpression = "";
|
||||
txtDisplay.Text = "0";
|
||||
isResultShown = false;
|
||||
}
|
||||
else if (currentExpression.Length > 0)
|
||||
{
|
||||
currentExpression = currentExpression.Substring(0, currentExpression.Length - 1);
|
||||
txtDisplay.Text = currentExpression.Length > 0 ? currentExpression : "0";
|
||||
}
|
||||
}
|
||||
else if (text == "=")
|
||||
{
|
||||
Evaluate();
|
||||
}
|
||||
else
|
||||
{
|
||||
if (isResultShown)
|
||||
{
|
||||
if (IsOperator(text))
|
||||
{
|
||||
isResultShown = false; // Continue with result
|
||||
}
|
||||
else
|
||||
{
|
||||
currentExpression = ""; // Start new
|
||||
isResultShown = false;
|
||||
}
|
||||
}
|
||||
currentExpression += text;
|
||||
txtDisplay.Text = currentExpression;
|
||||
}
|
||||
}
|
||||
|
||||
private void Evaluate()
|
||||
{
|
||||
try
|
||||
{
|
||||
Function f = new Function();
|
||||
f.Parse(currentExpression);
|
||||
f.Infix2Postfix();
|
||||
f.EvaluatePostfix();
|
||||
|
||||
decimal result = f.m_result;
|
||||
string resStr = result.ToString("G29", System.Globalization.CultureInfo.InvariantCulture);
|
||||
|
||||
txtDisplay.Text = resStr;
|
||||
currentExpression = resStr;
|
||||
isResultShown = true;
|
||||
}
|
||||
catch (Exception)
|
||||
{
|
||||
txtDisplay.Text = "Error";
|
||||
currentExpression = "";
|
||||
isResultShown = true; // allow typing to clear
|
||||
}
|
||||
}
|
||||
|
||||
private bool IsOperator(string text) // Redundant but kept for safety if used elsewhere
|
||||
{
|
||||
return text == "+" || text == "-" || text == "*" || text == "/";
|
||||
}
|
||||
}
|
||||
}
|
||||
165
MathApp/MathApp/Controls/SystemSolverControl.Designer.cs
generated
Normal file
165
MathApp/MathApp/Controls/SystemSolverControl.Designer.cs
generated
Normal file
|
|
@ -0,0 +1,165 @@
|
|||
|
||||
namespace MathApp.Controls
|
||||
{
|
||||
partial class SystemSolverControl
|
||||
{
|
||||
private System.ComponentModel.IContainer components = null;
|
||||
|
||||
protected override void Dispose(bool disposing)
|
||||
{
|
||||
if (disposing && (components != null))
|
||||
{
|
||||
components.Dispose();
|
||||
}
|
||||
base.Dispose(disposing);
|
||||
}
|
||||
|
||||
private void InitializeComponent()
|
||||
{
|
||||
this.lblTitle = new System.Windows.Forms.Label();
|
||||
this.pnlContent = new System.Windows.Forms.Panel();
|
||||
|
||||
this.lblEqCount = new System.Windows.Forms.Label();
|
||||
this.numEqCount = new System.Windows.Forms.NumericUpDown();
|
||||
this.btnGenerate = new System.Windows.Forms.Button();
|
||||
this.pnlEquations = new System.Windows.Forms.FlowLayoutPanel();
|
||||
this.lblSystemIterations = new System.Windows.Forms.Label();
|
||||
this.txtSystemIterations = new System.Windows.Forms.TextBox();
|
||||
this.btnSolveSystem = new System.Windows.Forms.Button();
|
||||
this.lblSystemResult = new System.Windows.Forms.Label();
|
||||
this.txtSystemResult = new System.Windows.Forms.TextBox();
|
||||
|
||||
this.pnlContent.SuspendLayout();
|
||||
((System.ComponentModel.ISupportInitialize)(this.numEqCount)).BeginInit();
|
||||
this.SuspendLayout();
|
||||
|
||||
//
|
||||
// lblTitle
|
||||
//
|
||||
this.lblTitle.Dock = System.Windows.Forms.DockStyle.Top;
|
||||
this.lblTitle.Font = new System.Drawing.Font("Segoe UI Light", 24F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point);
|
||||
this.lblTitle.ForeColor = System.Drawing.Color.White;
|
||||
this.lblTitle.Location = new System.Drawing.Point(0, 0);
|
||||
this.lblTitle.Name = "lblTitle";
|
||||
this.lblTitle.Size = new System.Drawing.Size(800, 60);
|
||||
this.lblTitle.TabIndex = 0;
|
||||
this.lblTitle.Text = "Equation System Solver";
|
||||
this.lblTitle.TextAlign = System.Drawing.ContentAlignment.MiddleLeft;
|
||||
|
||||
//
|
||||
// pnlContent
|
||||
//
|
||||
this.pnlContent.Controls.Add(this.txtSystemResult);
|
||||
this.pnlContent.Controls.Add(this.lblSystemResult);
|
||||
this.pnlContent.Controls.Add(this.btnSolveSystem);
|
||||
this.pnlContent.Controls.Add(this.txtSystemIterations);
|
||||
this.pnlContent.Controls.Add(this.lblSystemIterations);
|
||||
this.pnlContent.Controls.Add(this.pnlEquations);
|
||||
this.pnlContent.Controls.Add(this.btnGenerate);
|
||||
this.pnlContent.Controls.Add(this.numEqCount);
|
||||
this.pnlContent.Controls.Add(this.lblEqCount);
|
||||
this.pnlContent.Dock = System.Windows.Forms.DockStyle.Fill;
|
||||
this.pnlContent.Location = new System.Drawing.Point(0, 60);
|
||||
this.pnlContent.Name = "pnlContent";
|
||||
this.pnlContent.Size = new System.Drawing.Size(800, 540);
|
||||
this.pnlContent.TabIndex = 1;
|
||||
|
||||
// Simple layout
|
||||
int yObject = 20;
|
||||
void StyleLabel(System.Windows.Forms.Label l) {
|
||||
l.AutoSize = true;
|
||||
l.Font = new System.Drawing.Font("Segoe UI", 10F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point);
|
||||
l.ForeColor = System.Drawing.Color.White;
|
||||
}
|
||||
|
||||
StyleLabel(this.lblEqCount);
|
||||
this.lblEqCount.Location = new System.Drawing.Point(20, yObject);
|
||||
this.lblEqCount.Text = "Number of Equations:";
|
||||
|
||||
this.numEqCount.Location = new System.Drawing.Point(180, yObject);
|
||||
this.numEqCount.Minimum = 1;
|
||||
this.numEqCount.Value = 2;
|
||||
this.numEqCount.BackColor = System.Drawing.Color.FromArgb(((int)(((byte)(40)))), ((int)(((byte)(40)))), ((int)(((byte)(40)))));
|
||||
this.numEqCount.ForeColor = System.Drawing.Color.White;
|
||||
this.numEqCount.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle;
|
||||
|
||||
this.btnGenerate.Location = new System.Drawing.Point(310, yObject - 2);
|
||||
this.btnGenerate.Text = "Generate";
|
||||
this.btnGenerate.FlatStyle = System.Windows.Forms.FlatStyle.Flat;
|
||||
this.btnGenerate.BackColor = System.Drawing.Color.FromArgb(60, 60, 60);
|
||||
this.btnGenerate.ForeColor = System.Drawing.Color.White;
|
||||
this.btnGenerate.Size = new System.Drawing.Size(100, 25);
|
||||
this.btnGenerate.FlatAppearance.BorderSize = 0;
|
||||
yObject += 50;
|
||||
|
||||
this.pnlEquations.Location = new System.Drawing.Point(20, yObject);
|
||||
this.pnlEquations.Size = new System.Drawing.Size(750, 300);
|
||||
this.pnlEquations.AutoScroll = true;
|
||||
this.pnlEquations.FlowDirection = System.Windows.Forms.FlowDirection.TopDown;
|
||||
this.pnlEquations.WrapContents = false;
|
||||
this.pnlEquations.BorderStyle = System.Windows.Forms.BorderStyle.None;
|
||||
this.pnlEquations.BackColor = System.Drawing.Color.FromArgb(((int)(((byte)(25)))), ((int)(((byte)(25)))), ((int)(((byte)(25)))));
|
||||
yObject += 320;
|
||||
|
||||
StyleLabel(this.lblSystemIterations);
|
||||
this.lblSystemIterations.Location = new System.Drawing.Point(20, yObject);
|
||||
this.lblSystemIterations.Text = "Iterations:";
|
||||
this.txtSystemIterations.Location = new System.Drawing.Point(100, yObject);
|
||||
this.txtSystemIterations.Text = "100";
|
||||
this.txtSystemIterations.BackColor = System.Drawing.Color.FromArgb(((int)(((byte)(40)))), ((int)(((byte)(40)))), ((int)(((byte)(40)))));
|
||||
this.txtSystemIterations.ForeColor = System.Drawing.Color.White;
|
||||
this.txtSystemIterations.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle;
|
||||
yObject += 50;
|
||||
|
||||
this.btnSolveSystem.Location = new System.Drawing.Point(20, yObject);
|
||||
this.btnSolveSystem.Text = "Solve System";
|
||||
this.btnSolveSystem.Size = new System.Drawing.Size(150, 40);
|
||||
this.btnSolveSystem.FlatStyle = System.Windows.Forms.FlatStyle.Flat;
|
||||
this.btnSolveSystem.BackColor = System.Drawing.Color.FromArgb(0, 120, 215);
|
||||
this.btnSolveSystem.ForeColor = System.Drawing.Color.White;
|
||||
this.btnSolveSystem.Font = new System.Drawing.Font("Segoe UI Semibold", 10F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point);
|
||||
this.btnSolveSystem.FlatAppearance.BorderSize = 0;
|
||||
yObject += 60;
|
||||
|
||||
StyleLabel(this.lblSystemResult);
|
||||
this.lblSystemResult.Location = new System.Drawing.Point(20, yObject);
|
||||
this.lblSystemResult.Text = "Result:";
|
||||
this.txtSystemResult.Location = new System.Drawing.Point(100, yObject);
|
||||
this.txtSystemResult.Size = new System.Drawing.Size(650, 100);
|
||||
this.txtSystemResult.Multiline = true;
|
||||
this.txtSystemResult.Font = new System.Drawing.Font("Consolas", 10F);
|
||||
this.txtSystemResult.BackColor = System.Drawing.Color.FromArgb(((int)(((byte)(30)))), ((int)(((byte)(30)))), ((int)(((byte)(30)))));
|
||||
this.txtSystemResult.ForeColor = System.Drawing.Color.White;
|
||||
this.txtSystemResult.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle;
|
||||
|
||||
|
||||
//
|
||||
// SystemSolverControl
|
||||
//
|
||||
this.AutoScaleDimensions = new System.Drawing.SizeF(7F, 15F);
|
||||
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
|
||||
this.BackColor = System.Drawing.Color.FromArgb(((int)(((byte)(20)))), ((int)(((byte)(20)))), ((int)(((byte)(20)))));
|
||||
this.Controls.Add(this.pnlContent);
|
||||
this.Controls.Add(this.lblTitle);
|
||||
this.Name = "SystemSolverControl";
|
||||
this.Size = new System.Drawing.Size(800, 600);
|
||||
this.pnlContent.ResumeLayout(false);
|
||||
this.pnlContent.PerformLayout();
|
||||
((System.ComponentModel.ISupportInitialize)(this.numEqCount)).EndInit();
|
||||
this.ResumeLayout(false);
|
||||
|
||||
}
|
||||
|
||||
private System.Windows.Forms.Label lblTitle;
|
||||
private System.Windows.Forms.Panel pnlContent;
|
||||
private System.Windows.Forms.Label lblEqCount;
|
||||
public System.Windows.Forms.NumericUpDown numEqCount;
|
||||
public System.Windows.Forms.Button btnGenerate;
|
||||
public System.Windows.Forms.FlowLayoutPanel pnlEquations;
|
||||
private System.Windows.Forms.Label lblSystemIterations;
|
||||
public System.Windows.Forms.TextBox txtSystemIterations;
|
||||
public System.Windows.Forms.Button btnSolveSystem;
|
||||
private System.Windows.Forms.Label lblSystemResult;
|
||||
public System.Windows.Forms.TextBox txtSystemResult;
|
||||
}
|
||||
}
|
||||
108
MathApp/MathApp/Controls/SystemSolverControl.cs
Normal file
108
MathApp/MathApp/Controls/SystemSolverControl.cs
Normal file
|
|
@ -0,0 +1,108 @@
|
|||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Drawing;
|
||||
using System.Windows.Forms;
|
||||
|
||||
namespace MathApp.Controls
|
||||
{
|
||||
public partial class SystemSolverControl : UserControl
|
||||
{
|
||||
private List<TextBox> equationsInputs = new List<TextBox>();
|
||||
private List<TextBox> variablesInputs = new List<TextBox>();
|
||||
private List<TextBox> initialPointsInputs = new List<TextBox>();
|
||||
|
||||
public SystemSolverControl()
|
||||
{
|
||||
InitializeComponent();
|
||||
btnGenerate.Click += BtnGenerate_Click;
|
||||
btnSolveSystem.Click += BtnSolveSystem_Click;
|
||||
}
|
||||
|
||||
private void BtnGenerate_Click(object sender, EventArgs e)
|
||||
{
|
||||
pnlEquations.Controls.Clear();
|
||||
equationsInputs.Clear();
|
||||
variablesInputs.Clear();
|
||||
initialPointsInputs.Clear();
|
||||
|
||||
int count = (int)numEqCount.Value;
|
||||
|
||||
for (int i = 0; i < count; i++)
|
||||
{
|
||||
Panel p = new Panel();
|
||||
p.Size = new Size(720, 30);
|
||||
p.BackColor = Color.FromArgb(30,30,30);
|
||||
|
||||
Label lblEq = new Label { Text = "Eq " + (i+1), Location = new Point(0, 5), AutoSize = true, ForeColor = Color.White };
|
||||
TextBox txtEq = new TextBox { Location = new Point(50, 3), Size = new Size(300, 23), BackColor = Color.FromArgb(40,40,40), ForeColor = Color.White, BorderStyle = BorderStyle.FixedSingle };
|
||||
|
||||
Label lblVar = new Label { Text = "Var", Location = new Point(360, 5), AutoSize = true, ForeColor = Color.White };
|
||||
TextBox txtVar = new TextBox { Location = new Point(390, 3), Size = new Size(50, 23), BackColor = Color.FromArgb(40,40,40), ForeColor = Color.White, BorderStyle = BorderStyle.FixedSingle };
|
||||
|
||||
Label lblInit = new Label { Text = "Init", Location = new Point(450, 5), AutoSize = true, ForeColor = Color.White };
|
||||
TextBox txtInit = new TextBox { Location = new Point(480, 3), Size = new Size(50, 23), Text="0", BackColor = Color.FromArgb(40,40,40), ForeColor = Color.White, BorderStyle = BorderStyle.FixedSingle };
|
||||
|
||||
p.Controls.Add(lblEq);
|
||||
p.Controls.Add(txtEq);
|
||||
p.Controls.Add(lblVar);
|
||||
p.Controls.Add(txtVar);
|
||||
p.Controls.Add(lblInit);
|
||||
p.Controls.Add(txtInit);
|
||||
|
||||
pnlEquations.Controls.Add(p);
|
||||
|
||||
equationsInputs.Add(txtEq);
|
||||
variablesInputs.Add(txtVar);
|
||||
initialPointsInputs.Add(txtInit);
|
||||
}
|
||||
}
|
||||
|
||||
private void BtnSolveSystem_Click(object sender, EventArgs e)
|
||||
{
|
||||
try
|
||||
{
|
||||
List<string> eqs = new List<string>();
|
||||
Dictionary<string, decimal> dict = new Dictionary<string, decimal>();
|
||||
|
||||
for(int i=0; i<equationsInputs.Count; i++)
|
||||
{
|
||||
eqs.Add(equationsInputs[i].Text);
|
||||
|
||||
decimal val = 0;
|
||||
if (decimal.TryParse(initialPointsInputs[i].Text, out val)) { }
|
||||
|
||||
string varName = variablesInputs[i].Text.Trim();
|
||||
if(string.IsNullOrEmpty(varName)) continue;
|
||||
|
||||
if(dict.ContainsKey(varName))
|
||||
{
|
||||
txtSystemResult.Text = "Error: Duplicate variable '" + varName + "'.";
|
||||
return;
|
||||
}
|
||||
dict.Add(varName, val);
|
||||
}
|
||||
|
||||
int iterCount = 100;
|
||||
int.TryParse(txtSystemIterations.Text, out iterCount);
|
||||
|
||||
Dictionary<string, decimal> results = NumericalMethods.MethodMultiVar_SolveEquationSystem(eqs, dict, iterCount);
|
||||
|
||||
if (results != null)
|
||||
{
|
||||
List<string> resLines = new List<string>();
|
||||
foreach(var kvp in results)
|
||||
{
|
||||
resLines.Add(kvp.Key + ": " + kvp.Value.ToString("F8"));
|
||||
}
|
||||
txtSystemResult.Text = string.Join(Environment.NewLine, resLines);
|
||||
}
|
||||
else
|
||||
txtSystemResult.Text = "Failed to solve.";
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
txtSystemResult.Text = "Error: " + ex.Message;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
177
MathApp/MathApp/Controls/TaylorSeriesControl.Designer.cs
generated
Normal file
177
MathApp/MathApp/Controls/TaylorSeriesControl.Designer.cs
generated
Normal file
|
|
@ -0,0 +1,177 @@
|
|||
|
||||
namespace MathApp.Controls
|
||||
{
|
||||
partial class TaylorSeriesControl
|
||||
{
|
||||
private System.ComponentModel.IContainer components = null;
|
||||
|
||||
protected override void Dispose(bool disposing)
|
||||
{
|
||||
if (disposing && (components != null))
|
||||
{
|
||||
components.Dispose();
|
||||
}
|
||||
base.Dispose(disposing);
|
||||
}
|
||||
|
||||
private void InitializeComponent()
|
||||
{
|
||||
this.lblTitle = new System.Windows.Forms.Label();
|
||||
this.pnlContent = new System.Windows.Forms.Panel();
|
||||
|
||||
this.lblFunction = new System.Windows.Forms.Label();
|
||||
this.txtFunction = new System.Windows.Forms.TextBox();
|
||||
this.lblVariable = new System.Windows.Forms.Label();
|
||||
this.txtVariable = new System.Windows.Forms.TextBox();
|
||||
this.lblCenterPoint = new System.Windows.Forms.Label();
|
||||
this.txtCenterPoint = new System.Windows.Forms.TextBox();
|
||||
this.lblOrder = new System.Windows.Forms.Label();
|
||||
this.numOrder = new System.Windows.Forms.NumericUpDown();
|
||||
this.btnCalculate = new System.Windows.Forms.Button();
|
||||
this.lblResult = new System.Windows.Forms.Label();
|
||||
this.txtResult = new System.Windows.Forms.TextBox();
|
||||
|
||||
this.pnlContent.SuspendLayout();
|
||||
((System.ComponentModel.ISupportInitialize)(this.numOrder)).BeginInit();
|
||||
this.SuspendLayout();
|
||||
|
||||
//
|
||||
// lblTitle
|
||||
//
|
||||
this.lblTitle.Dock = System.Windows.Forms.DockStyle.Top;
|
||||
this.lblTitle.Font = new System.Drawing.Font("Segoe UI Light", 24F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point);
|
||||
this.lblTitle.ForeColor = System.Drawing.Color.White;
|
||||
this.lblTitle.Location = new System.Drawing.Point(0, 0);
|
||||
this.lblTitle.Name = "lblTitle";
|
||||
this.lblTitle.Size = new System.Drawing.Size(800, 60);
|
||||
this.lblTitle.TabIndex = 0;
|
||||
this.lblTitle.Text = "Taylor Series Calculator";
|
||||
this.lblTitle.TextAlign = System.Drawing.ContentAlignment.MiddleLeft;
|
||||
|
||||
//
|
||||
// pnlContent
|
||||
//
|
||||
this.pnlContent.Controls.Add(this.txtResult);
|
||||
this.pnlContent.Controls.Add(this.lblResult);
|
||||
this.pnlContent.Controls.Add(this.btnCalculate);
|
||||
this.pnlContent.Controls.Add(this.numOrder);
|
||||
this.pnlContent.Controls.Add(this.lblOrder);
|
||||
this.pnlContent.Controls.Add(this.txtCenterPoint);
|
||||
this.pnlContent.Controls.Add(this.lblCenterPoint);
|
||||
this.pnlContent.Controls.Add(this.txtVariable);
|
||||
this.pnlContent.Controls.Add(this.lblVariable);
|
||||
this.pnlContent.Controls.Add(this.txtFunction);
|
||||
this.pnlContent.Controls.Add(this.lblFunction);
|
||||
this.pnlContent.Dock = System.Windows.Forms.DockStyle.Fill;
|
||||
this.pnlContent.Location = new System.Drawing.Point(0, 60);
|
||||
this.pnlContent.Name = "pnlContent";
|
||||
this.pnlContent.Size = new System.Drawing.Size(800, 540);
|
||||
this.pnlContent.TabIndex = 1;
|
||||
|
||||
int yObject = 20;
|
||||
|
||||
void StyleLabel(System.Windows.Forms.Label l) {
|
||||
l.AutoSize = true;
|
||||
l.Font = new System.Drawing.Font("Segoe UI", 10F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point);
|
||||
l.ForeColor = System.Drawing.Color.White;
|
||||
}
|
||||
|
||||
StyleLabel(this.lblFunction);
|
||||
this.lblFunction.Location = new System.Drawing.Point(20, yObject);
|
||||
this.lblFunction.Text = "Function f(x):";
|
||||
|
||||
this.txtFunction.Location = new System.Drawing.Point(150, yObject);
|
||||
this.txtFunction.Size = new System.Drawing.Size(400, 25);
|
||||
this.txtFunction.BackColor = System.Drawing.Color.FromArgb(((int)(((byte)(40)))), ((int)(((byte)(40)))), ((int)(((byte)(40)))));
|
||||
this.txtFunction.ForeColor = System.Drawing.Color.White;
|
||||
this.txtFunction.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle;
|
||||
yObject += 50;
|
||||
|
||||
StyleLabel(this.lblVariable);
|
||||
this.lblVariable.Location = new System.Drawing.Point(20, yObject);
|
||||
this.lblVariable.Text = "Variable (e.g. x):";
|
||||
this.txtVariable.Location = new System.Drawing.Point(150, yObject);
|
||||
this.txtVariable.Size = new System.Drawing.Size(100, 25);
|
||||
this.txtVariable.BackColor = System.Drawing.Color.FromArgb(((int)(((byte)(40)))), ((int)(((byte)(40)))), ((int)(((byte)(40)))));
|
||||
this.txtVariable.ForeColor = System.Drawing.Color.White;
|
||||
this.txtVariable.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle;
|
||||
this.txtVariable.Text = "x";
|
||||
yObject += 50;
|
||||
|
||||
StyleLabel(this.lblCenterPoint);
|
||||
this.lblCenterPoint.Location = new System.Drawing.Point(20, yObject);
|
||||
this.lblCenterPoint.Text = "Center Point (a):";
|
||||
this.txtCenterPoint.Location = new System.Drawing.Point(150, yObject);
|
||||
this.txtCenterPoint.Size = new System.Drawing.Size(100, 25);
|
||||
this.txtCenterPoint.BackColor = System.Drawing.Color.FromArgb(((int)(((byte)(40)))), ((int)(((byte)(40)))), ((int)(((byte)(40)))));
|
||||
this.txtCenterPoint.ForeColor = System.Drawing.Color.White;
|
||||
this.txtCenterPoint.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle;
|
||||
this.txtCenterPoint.Text = "0";
|
||||
yObject += 50;
|
||||
|
||||
StyleLabel(this.lblOrder);
|
||||
this.lblOrder.Location = new System.Drawing.Point(20, yObject);
|
||||
this.lblOrder.Text = "Order (n):";
|
||||
this.numOrder.Location = new System.Drawing.Point(150, yObject);
|
||||
this.numOrder.Minimum = 1;
|
||||
this.numOrder.Maximum = 20;
|
||||
this.numOrder.Value = 4;
|
||||
this.numOrder.BackColor = System.Drawing.Color.FromArgb(((int)(((byte)(40)))), ((int)(((byte)(40)))), ((int)(((byte)(40)))));
|
||||
this.numOrder.ForeColor = System.Drawing.Color.White;
|
||||
this.numOrder.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle;
|
||||
yObject += 60;
|
||||
|
||||
this.btnCalculate.Location = new System.Drawing.Point(150, yObject);
|
||||
this.btnCalculate.Text = "Calculate Series";
|
||||
this.btnCalculate.Size = new System.Drawing.Size(150, 40);
|
||||
this.btnCalculate.FlatStyle = System.Windows.Forms.FlatStyle.Flat;
|
||||
this.btnCalculate.BackColor = System.Drawing.Color.FromArgb(0, 120, 215);
|
||||
this.btnCalculate.ForeColor = System.Drawing.Color.White;
|
||||
this.btnCalculate.Font = new System.Drawing.Font("Segoe UI Semibold", 10F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point);
|
||||
this.btnCalculate.FlatAppearance.BorderSize = 0;
|
||||
yObject += 60;
|
||||
|
||||
StyleLabel(this.lblResult);
|
||||
this.lblResult.Location = new System.Drawing.Point(20, yObject);
|
||||
this.lblResult.Text = "Result:";
|
||||
this.txtResult.Location = new System.Drawing.Point(150, yObject);
|
||||
this.txtResult.Size = new System.Drawing.Size(600, 150);
|
||||
this.txtResult.Multiline = true;
|
||||
this.txtResult.ScrollBars = System.Windows.Forms.ScrollBars.Vertical;
|
||||
this.txtResult.Font = new System.Drawing.Font("Consolas", 10F);
|
||||
this.txtResult.BackColor = System.Drawing.Color.FromArgb(((int)(((byte)(30)))), ((int)(((byte)(30)))), ((int)(((byte)(30)))));
|
||||
this.txtResult.ForeColor = System.Drawing.Color.White;
|
||||
this.txtResult.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle;
|
||||
|
||||
//
|
||||
// TaylorSeriesControl
|
||||
//
|
||||
this.AutoScaleDimensions = new System.Drawing.SizeF(7F, 15F);
|
||||
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
|
||||
this.BackColor = System.Drawing.Color.FromArgb(((int)(((byte)(20)))), ((int)(((byte)(20)))), ((int)(((byte)(20)))));
|
||||
this.Controls.Add(this.pnlContent);
|
||||
this.Controls.Add(this.lblTitle);
|
||||
this.Name = "TaylorSeriesControl";
|
||||
this.Size = new System.Drawing.Size(800, 600);
|
||||
this.pnlContent.ResumeLayout(false);
|
||||
this.pnlContent.PerformLayout();
|
||||
((System.ComponentModel.ISupportInitialize)(this.numOrder)).EndInit();
|
||||
this.ResumeLayout(false);
|
||||
|
||||
}
|
||||
|
||||
private System.Windows.Forms.Label lblTitle;
|
||||
private System.Windows.Forms.Panel pnlContent;
|
||||
private System.Windows.Forms.Label lblFunction;
|
||||
private System.Windows.Forms.TextBox txtFunction;
|
||||
private System.Windows.Forms.Label lblVariable;
|
||||
private System.Windows.Forms.TextBox txtVariable;
|
||||
private System.Windows.Forms.Label lblCenterPoint;
|
||||
private System.Windows.Forms.TextBox txtCenterPoint;
|
||||
private System.Windows.Forms.Label lblOrder;
|
||||
private System.Windows.Forms.NumericUpDown numOrder;
|
||||
private System.Windows.Forms.Button btnCalculate;
|
||||
private System.Windows.Forms.Label lblResult;
|
||||
private System.Windows.Forms.TextBox txtResult;
|
||||
}
|
||||
}
|
||||
36
MathApp/MathApp/Controls/TaylorSeriesControl.cs
Normal file
36
MathApp/MathApp/Controls/TaylorSeriesControl.cs
Normal file
|
|
@ -0,0 +1,36 @@
|
|||
using System;
|
||||
using System.Windows.Forms;
|
||||
|
||||
namespace MathApp.Controls
|
||||
{
|
||||
public partial class TaylorSeriesControl : UserControl
|
||||
{
|
||||
public TaylorSeriesControl()
|
||||
{
|
||||
InitializeComponent();
|
||||
btnCalculate.Click += BtnCalculate_Click;
|
||||
}
|
||||
|
||||
private void BtnCalculate_Click(object sender, EventArgs e)
|
||||
{
|
||||
try
|
||||
{
|
||||
string eq = txtFunction.Text;
|
||||
string variable = txtVariable.Text;
|
||||
int degree = (int)numOrder.Value;
|
||||
decimal center = decimal.Parse(txtCenterPoint.Text);
|
||||
|
||||
string result = NumericalMethods.Method_TaylorSeries(eq, degree, variable, center);
|
||||
// Clean up result for display if needed
|
||||
result = result.Replace("+ -", "- ").Replace("+-", "- ");
|
||||
|
||||
txtResult.Text = "Taylor Series approximation for f(x) =" + Environment.NewLine +
|
||||
result;
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
txtResult.Text = "Error: " + ex.Message;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
541
MathApp/MathApp/Core/DecimalMath.cs
Normal file
541
MathApp/MathApp/Core/DecimalMath.cs
Normal file
|
|
@ -0,0 +1,541 @@
|
|||
using System;
|
||||
|
||||
/// <summary>
|
||||
/// Analogy of System.Math class for decimal types
|
||||
/// </summary>
|
||||
public static class DecimalMath
|
||||
{
|
||||
/* ... (Content from original file, confirmed no Unity deps) ... */
|
||||
|
||||
/// <summary>
|
||||
/// represents PI
|
||||
/// </summary>
|
||||
public const decimal PI = 3.14159265358979323846264338327950288419716939937510M;
|
||||
|
||||
/// <summary>
|
||||
/// represents PI
|
||||
/// </summary>
|
||||
public const decimal Epsilon = 0.0000000000000000001M;
|
||||
|
||||
/// <summary>
|
||||
/// represents 2*PI
|
||||
/// </summary>
|
||||
private const decimal PIx2 = 6.28318530717958647692528676655900576839433879875021M;
|
||||
|
||||
/// <summary>
|
||||
/// represents E
|
||||
/// </summary>
|
||||
public const decimal E = 2.7182818284590452353602874713526624977572470936999595749M;
|
||||
|
||||
/// <summary>
|
||||
/// represents PI/2
|
||||
/// </summary>
|
||||
private const decimal PIdiv2 = 1.570796326794896619231321691639751442098584699687552910487M;
|
||||
|
||||
/// <summary>
|
||||
/// represents PI/4
|
||||
/// </summary>
|
||||
private const decimal PIdiv4 = 0.785398163397448309615660845819875721049292349843776455243M;
|
||||
|
||||
/// <summary>
|
||||
/// represents 1.0/E
|
||||
/// </summary>
|
||||
private const decimal Einv = 0.3678794411714423215955237701614608674458111310317678M;
|
||||
|
||||
/// <summary>
|
||||
/// log(10,E) factor
|
||||
/// </summary>
|
||||
private const decimal Log10Inv = 0.434294481903251827651128918916605082294397005803666566114M;
|
||||
|
||||
/// <summary>
|
||||
/// Zero
|
||||
/// </summary>
|
||||
public const decimal Zero = 0.0M;
|
||||
|
||||
/// <summary>
|
||||
/// One
|
||||
/// </summary>
|
||||
public const decimal One = 1.0M;
|
||||
|
||||
/// <summary>
|
||||
/// Represents 0.5M
|
||||
/// </summary>
|
||||
private const decimal Half = 0.5M;
|
||||
|
||||
/// <summary>
|
||||
/// Max iterations count in Taylor series
|
||||
/// </summary>
|
||||
private const int MaxIteration = 100;
|
||||
|
||||
/// <summary>
|
||||
/// Analogy of Math.Exp method
|
||||
/// </summary>
|
||||
/// <param name="x"></param>
|
||||
/// <returns></returns>
|
||||
public static decimal Exp(decimal x)
|
||||
{
|
||||
var count = 0;
|
||||
|
||||
if (x > One)
|
||||
{
|
||||
count = decimal.ToInt32(decimal.Truncate(x));
|
||||
x -= decimal.Truncate(x);
|
||||
}
|
||||
|
||||
if (x < Zero)
|
||||
{
|
||||
count = decimal.ToInt32(decimal.Truncate(x) - 1);
|
||||
x = One + (x - decimal.Truncate(x));
|
||||
}
|
||||
|
||||
var iteration = 1;
|
||||
var result = One;
|
||||
var factorial = One;
|
||||
decimal cachedResult;
|
||||
do
|
||||
{
|
||||
cachedResult = result;
|
||||
factorial *= x / iteration++;
|
||||
result += factorial;
|
||||
} while (cachedResult != result);
|
||||
|
||||
if (count == 0)
|
||||
return result;
|
||||
return result * PowerN(E, count);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Analogy of Math.Pow method
|
||||
/// </summary>
|
||||
/// <param name="value"></param>
|
||||
/// <param name="pow"></param>
|
||||
/// <returns></returns>
|
||||
public static decimal Pow(decimal value, decimal pow)
|
||||
{
|
||||
if (pow == Zero) return One;
|
||||
if (pow == One) return value;
|
||||
if (value == One) return One;
|
||||
|
||||
if (value == Zero)
|
||||
{
|
||||
if (pow > Zero)
|
||||
{
|
||||
return Zero;
|
||||
}
|
||||
|
||||
throw new Exception("Invalid Operation: zero base and negative power");
|
||||
}
|
||||
|
||||
if (pow == -One) return One / value;
|
||||
|
||||
var isPowerInteger = IsInteger(pow);
|
||||
if (value < Zero && !isPowerInteger)
|
||||
{
|
||||
throw new Exception("Invalid Operation: negative base and non-integer power");
|
||||
}
|
||||
|
||||
if (isPowerInteger && value > Zero)
|
||||
{
|
||||
int powerInt = (int)pow;
|
||||
return PowerN(value, powerInt);
|
||||
}
|
||||
|
||||
if (isPowerInteger && value < Zero)
|
||||
{
|
||||
int powerInt = (int)pow;
|
||||
if (powerInt % 2 == 0)
|
||||
{
|
||||
return Exp(pow * Log(-value));
|
||||
}
|
||||
|
||||
return -Exp(pow * Log(-value));
|
||||
}
|
||||
|
||||
return Exp(pow * Log(value));
|
||||
}
|
||||
|
||||
private static bool IsInteger(decimal value)
|
||||
{
|
||||
var longValue = (long)value;
|
||||
return Abs(value - longValue) <= Epsilon;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Power to the integer value
|
||||
/// </summary>
|
||||
/// <param name="value"></param>
|
||||
/// <param name="power"></param>
|
||||
/// <returns></returns>
|
||||
public static decimal PowerN(decimal value, int power)
|
||||
{
|
||||
while (true)
|
||||
{
|
||||
if (power == Zero) return One;
|
||||
if (power < Zero)
|
||||
{
|
||||
value = One / value;
|
||||
power = -power;
|
||||
continue;
|
||||
}
|
||||
|
||||
var q = power;
|
||||
var prod = One;
|
||||
var current = value;
|
||||
while (q > 0)
|
||||
{
|
||||
if (q % 2 == 1)
|
||||
{
|
||||
// detects the 1s in the binary expression of power
|
||||
prod = current * prod; // picks up the relevant power
|
||||
q--;
|
||||
}
|
||||
|
||||
current *= current; // value^i -> value^(2*i)
|
||||
q >>= 1;
|
||||
}
|
||||
|
||||
return prod;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Analogy of Math.Log10
|
||||
/// </summary>
|
||||
/// <param name="x"></param>
|
||||
/// <returns></returns>
|
||||
public static decimal Log10(decimal x)
|
||||
{
|
||||
return Log(x) * Log10Inv;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Analogy of Math.Log
|
||||
/// </summary>
|
||||
/// <param name="x"></param>
|
||||
/// <returns></returns>
|
||||
public static decimal Log(decimal x)
|
||||
{
|
||||
if (x <= Zero)
|
||||
{
|
||||
throw new ArgumentException("x must be greater than zero");
|
||||
}
|
||||
var count = 0;
|
||||
while (x >= One)
|
||||
{
|
||||
x *= Einv;
|
||||
count++;
|
||||
}
|
||||
while (x <= Einv)
|
||||
{
|
||||
x *= E;
|
||||
count--;
|
||||
}
|
||||
x--;
|
||||
if (x == Zero) return count;
|
||||
var result = Zero;
|
||||
var iteration = 0;
|
||||
var y = One;
|
||||
var cacheResult = result - One;
|
||||
while (cacheResult != result && iteration < MaxIteration)
|
||||
{
|
||||
iteration++;
|
||||
cacheResult = result;
|
||||
y *= -x;
|
||||
result += y / iteration;
|
||||
}
|
||||
return count - result;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Analogy of Math.Cos
|
||||
/// </summary>
|
||||
/// <param name="x"></param>
|
||||
/// <returns></returns>
|
||||
public static decimal Cos(decimal x)
|
||||
{
|
||||
//truncating to [-2*PI;2*PI]
|
||||
TruncateToPeriodicInterval(ref x);
|
||||
|
||||
// now x in (-2pi,2pi)
|
||||
if (x >= PI && x <= PIx2)
|
||||
{
|
||||
return -Cos(x - PI);
|
||||
}
|
||||
if (x >= -PIx2 && x <= -PI)
|
||||
{
|
||||
return -Cos(x + PI);
|
||||
}
|
||||
|
||||
x *= x;
|
||||
//y=1-x/2!+x^2/4!-x^3/6!...
|
||||
var xx = -x * Half;
|
||||
var y = One + xx;
|
||||
var cachedY = y - One;//init cache with different value
|
||||
for (var i = 1; cachedY != y && i < MaxIteration; i++)
|
||||
{
|
||||
cachedY = y;
|
||||
decimal factor = i * ((i << 1) + 3) + 1; //2i^2+2i+i+1=2i^2+3i+1
|
||||
factor = -Half / factor;
|
||||
xx *= x * factor;
|
||||
y += xx;
|
||||
}
|
||||
return y;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Analogy of Math.Tan
|
||||
/// </summary>
|
||||
/// <param name="x"></param>
|
||||
/// <returns></returns>
|
||||
public static decimal Tan(decimal x)
|
||||
{
|
||||
var cos = Cos(x);
|
||||
if (cos == Zero) throw new ArgumentException(nameof(x));
|
||||
//calculate sin using cos
|
||||
var sin = CalculateSinFromCos(x, cos);
|
||||
return sin / cos;
|
||||
}
|
||||
/// <summary>
|
||||
/// Helper function for calculating sin(x) from cos(x)
|
||||
/// </summary>
|
||||
/// <param name="x"></param>
|
||||
/// <param name="cos"></param>
|
||||
/// <returns></returns>
|
||||
private static decimal CalculateSinFromCos(decimal x, decimal cos)
|
||||
{
|
||||
var moduleOfSin = Sqrt(One - (cos * cos));
|
||||
var sineIsPositive = IsSignOfSinePositive(x);
|
||||
if (sineIsPositive) return moduleOfSin;
|
||||
return -moduleOfSin;
|
||||
}
|
||||
/// <summary>
|
||||
/// Analogy of Math.Sin
|
||||
/// </summary>
|
||||
/// <param name="x"></param>
|
||||
/// <returns></returns>
|
||||
public static decimal Sin(decimal x)
|
||||
{
|
||||
var cos = Cos(x);
|
||||
return CalculateSinFromCos(x, cos);
|
||||
}
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// Truncates to [-2*PI;2*PI]
|
||||
/// </summary>
|
||||
/// <param name="x"></param>
|
||||
private static void TruncateToPeriodicInterval(ref decimal x)
|
||||
{
|
||||
while (x >= PIx2)
|
||||
{
|
||||
var divide = Math.Abs(decimal.ToInt32(x / PIx2));
|
||||
x -= divide * PIx2;
|
||||
}
|
||||
|
||||
while (x <= -PIx2)
|
||||
{
|
||||
var divide = Math.Abs(decimal.ToInt32(x / PIx2));
|
||||
x += divide * PIx2;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
private static bool IsSignOfSinePositive(decimal x)
|
||||
{
|
||||
//truncating to [-2*PI;2*PI]
|
||||
TruncateToPeriodicInterval(ref x);
|
||||
|
||||
//now x in [-2*PI;2*PI]
|
||||
if (x >= -PIx2 && x <= -PI) return true;
|
||||
if (x >= -PI && x <= Zero) return false;
|
||||
if (x >= Zero && x <= PI) return true;
|
||||
if (x >= PI && x <= PIx2) return false;
|
||||
|
||||
//will not be reached
|
||||
throw new ArgumentException(nameof(x));
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Analogy of Math.Sqrt
|
||||
/// </summary>
|
||||
/// <param name="x"></param>
|
||||
/// <param name="epsilon">lasts iteration while error less than this epsilon</param>
|
||||
/// <returns></returns>
|
||||
public static decimal Sqrt(decimal x, decimal epsilon = Zero)
|
||||
{
|
||||
if (x < Zero) throw new OverflowException("Cannot calculate square root from a negative number");
|
||||
//initial approximation
|
||||
decimal current = (decimal)Math.Sqrt((double)x), previous;
|
||||
do
|
||||
{
|
||||
previous = current;
|
||||
if (previous == Zero) return Zero;
|
||||
current = (previous + x / previous) * Half;
|
||||
} while (Abs(previous - current) > epsilon);
|
||||
return current;
|
||||
}
|
||||
/// <summary>
|
||||
/// Analogy of Math.Sinh
|
||||
/// </summary>
|
||||
/// <param name="x"></param>
|
||||
/// <returns></returns>
|
||||
public static decimal Sinh(decimal x)
|
||||
{
|
||||
var y = Exp(x);
|
||||
var yy = One / y;
|
||||
return (y - yy) * Half;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Analogy of Math.Cosh
|
||||
/// </summary>
|
||||
/// <param name="x"></param>
|
||||
/// <returns></returns>
|
||||
public static decimal Cosh(decimal x)
|
||||
{
|
||||
var y = Exp(x);
|
||||
var yy = One / y;
|
||||
return (y + yy) * Half;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Analogy of Math.Sign
|
||||
/// </summary>
|
||||
/// <param name="x"></param>
|
||||
/// <returns></returns>
|
||||
public static int Sign(decimal x)
|
||||
{
|
||||
return x < Zero ? -1 : (x > Zero ? 1 : 0);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Analogy of Math.Tanh
|
||||
/// </summary>
|
||||
/// <param name="x"></param>
|
||||
/// <returns></returns>
|
||||
public static decimal Tanh(decimal x)
|
||||
{
|
||||
var y = Exp(x);
|
||||
var yy = One / y;
|
||||
return (y - yy) / (y + yy);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Analogy of Math.Abs
|
||||
/// </summary>
|
||||
/// <param name="x"></param>
|
||||
/// <returns></returns>
|
||||
public static decimal Abs(decimal x)
|
||||
{
|
||||
if (x <= Zero)
|
||||
{
|
||||
return -x;
|
||||
}
|
||||
return x;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Analogy of Math.Asin
|
||||
/// </summary>
|
||||
/// <param name="x"></param>
|
||||
/// <returns></returns>
|
||||
public static decimal Asin(decimal x)
|
||||
{
|
||||
if (x > One || x < -One)
|
||||
{
|
||||
throw new ArgumentException("x must be in [-1,1]");
|
||||
}
|
||||
//known values
|
||||
if (x == Zero) return Zero;
|
||||
if (x == One) return PIdiv2;
|
||||
//asin function is odd function
|
||||
if (x < Zero) return -Asin(-x);
|
||||
|
||||
//my optimize trick here
|
||||
|
||||
// used a math formula to speed up :
|
||||
// asin(x)=0.5*(pi/2-asin(1-2*x*x))
|
||||
// if x>=0 is true
|
||||
|
||||
var newX = One - 2 * x * x;
|
||||
|
||||
//for calculating new value near to zero than current
|
||||
//because we gain more speed with values near to zero
|
||||
if (Abs(x) > Abs(newX))
|
||||
{
|
||||
var t = Asin(newX);
|
||||
return Half * (PIdiv2 - t);
|
||||
}
|
||||
var y = Zero;
|
||||
var result = x;
|
||||
decimal cachedResult;
|
||||
var i = 1;
|
||||
y += result;
|
||||
var xx = x * x;
|
||||
do
|
||||
{
|
||||
cachedResult = result;
|
||||
result *= xx * (One - Half / (i));
|
||||
y += result / ((i << 1) + 1);
|
||||
i++;
|
||||
} while (cachedResult != result);
|
||||
return y;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Analogy of Math.Atan
|
||||
/// </summary>
|
||||
/// <param name="x"></param>
|
||||
/// <returns></returns>
|
||||
public static decimal Atan(decimal x)
|
||||
{
|
||||
if (x == Zero) return Zero;
|
||||
if (x == One) return PIdiv4;
|
||||
return Asin(x / Sqrt(One + x * x));
|
||||
}
|
||||
/// <summary>
|
||||
/// Analogy of Math.Acos
|
||||
/// </summary>
|
||||
/// <param name="x"></param>
|
||||
/// <returns></returns>
|
||||
public static decimal Acos(decimal x)
|
||||
{
|
||||
if (x == Zero) return PIdiv2;
|
||||
if (x == One) return Zero;
|
||||
if (x < Zero) return PI - Acos(-x);
|
||||
return PIdiv2 - Asin(x);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Analogy of Math.Atan2
|
||||
/// for more see this
|
||||
/// <seealso cref="http://i.imgur.com/TRLjs8R.png"/>
|
||||
/// </summary>
|
||||
/// <param name="y"></param>
|
||||
/// <param name="x"></param>
|
||||
/// <returns></returns>
|
||||
public static decimal Atan2(decimal y, decimal x)
|
||||
{
|
||||
if (x > Zero)
|
||||
{
|
||||
return Atan(y / x);
|
||||
}
|
||||
if (x < Zero && y >= Zero)
|
||||
{
|
||||
return Atan(y / x) + PI;
|
||||
}
|
||||
if (x < Zero && y < Zero)
|
||||
{
|
||||
return Atan(y / x) - PI;
|
||||
}
|
||||
if (x == Zero && y > Zero)
|
||||
{
|
||||
return PIdiv2;
|
||||
}
|
||||
if (x == Zero && y < Zero)
|
||||
{
|
||||
return -PIdiv2;
|
||||
}
|
||||
throw new ArgumentException("invalid atan2 arguments");
|
||||
}
|
||||
}
|
||||
721
MathApp/MathApp/Core/Function.cs
Normal file
721
MathApp/MathApp/Core/Function.cs
Normal file
|
|
@ -0,0 +1,721 @@
|
|||
using System;
|
||||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// </summary>
|
||||
///
|
||||
public enum Type { Variable, Value, UnaryOperator, Operator, Function, Result, Bracket, Comma, Error }
|
||||
public struct Symbol
|
||||
{
|
||||
public string m_name;
|
||||
public decimal m_value;
|
||||
public Type m_type;
|
||||
public override string ToString()
|
||||
{
|
||||
return m_name;
|
||||
}
|
||||
}
|
||||
public delegate Symbol EvaluateFunctionDelegate(string name, params Object[] args);
|
||||
public class Function
|
||||
{
|
||||
public decimal Result
|
||||
{
|
||||
get
|
||||
{
|
||||
return m_result;
|
||||
}
|
||||
}
|
||||
|
||||
public ArrayList Equation
|
||||
{
|
||||
get
|
||||
{
|
||||
return (ArrayList)m_equation.Clone();
|
||||
}
|
||||
}
|
||||
public ArrayList Postfix
|
||||
{
|
||||
get
|
||||
{
|
||||
return (ArrayList)m_postfix.Clone();
|
||||
}
|
||||
}
|
||||
|
||||
public EvaluateFunctionDelegate DefaultFunctionEvaluation
|
||||
{
|
||||
set
|
||||
{
|
||||
m_defaultFunctionEvaluation = value;
|
||||
}
|
||||
}
|
||||
|
||||
public bool Error
|
||||
{
|
||||
get
|
||||
{
|
||||
return m_bError;
|
||||
}
|
||||
}
|
||||
|
||||
public string ErrorDescription
|
||||
{
|
||||
get
|
||||
{
|
||||
return m_sErrorDescription;
|
||||
}
|
||||
}
|
||||
|
||||
public ArrayList Variables
|
||||
{
|
||||
get
|
||||
{
|
||||
ArrayList var = new ArrayList();
|
||||
foreach (Symbol sym in m_equation)
|
||||
{
|
||||
if ((sym.m_type == Type.Variable) && (!var.Contains(sym)))
|
||||
var.Add(sym);
|
||||
}
|
||||
return var;
|
||||
}
|
||||
set
|
||||
{
|
||||
foreach (Symbol sym in value)
|
||||
{
|
||||
for (int i = 0; i < m_postfix.Count; i++)
|
||||
{
|
||||
if ((sym.m_name == ((Symbol)m_postfix[i]).m_name) && (((Symbol)m_postfix[i]).m_type == Type.Variable))
|
||||
{
|
||||
Symbol sym1 = (Symbol)m_postfix[i];
|
||||
sym1.m_value = sym.m_value;
|
||||
m_postfix[i] = sym1;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public Function()
|
||||
{ }
|
||||
|
||||
public void Parse(string equation)
|
||||
{
|
||||
int state = 1;
|
||||
string temp = "";
|
||||
Symbol ctSymbol = new Symbol(); // Initialized to avoid error
|
||||
|
||||
m_bError = false;
|
||||
m_sErrorDescription = "None";
|
||||
|
||||
m_equation.Clear();
|
||||
m_postfix.Clear();
|
||||
|
||||
int nPos = 0;
|
||||
//-- Remove all white spaces from the equation string --
|
||||
equation = equation.Trim();
|
||||
while ((nPos = equation.IndexOf(' ')) != -1)
|
||||
equation = equation.Remove(nPos, 1);
|
||||
|
||||
for (int i = 0; i < equation.Length; i++)
|
||||
{
|
||||
switch (state)
|
||||
{
|
||||
case 1:
|
||||
if (Char.IsNumber(equation[i]))
|
||||
{
|
||||
state = 2;
|
||||
temp += equation[i];
|
||||
}
|
||||
else if (Char.IsLetter(equation[i]))
|
||||
{
|
||||
state = 3;
|
||||
temp += equation[i];
|
||||
}
|
||||
else
|
||||
{
|
||||
ctSymbol.m_name = equation[i].ToString();
|
||||
ctSymbol.m_value = 0;
|
||||
switch (ctSymbol.m_name)
|
||||
{
|
||||
case ",":
|
||||
case ".":
|
||||
ctSymbol.m_type = Type.Comma;
|
||||
break;
|
||||
case "(":
|
||||
case ")":
|
||||
case "[":
|
||||
case "]":
|
||||
case "{":
|
||||
case "}":
|
||||
ctSymbol.m_type = Type.Bracket;
|
||||
break;
|
||||
default:
|
||||
if (m_equation.Count == 0)
|
||||
ctSymbol.m_type = Type.UnaryOperator;
|
||||
else if ((((Symbol)m_equation[m_equation.Count - 1]).m_type == Type.Value) || (((Symbol)m_equation[m_equation.Count - 1]).m_type == Type.Variable)
|
||||
|| (((Symbol)m_equation[m_equation.Count - 1]).m_name == ")") || (((Symbol)m_equation[m_equation.Count - 1]).m_name == "]") || (((Symbol)m_equation[m_equation.Count - 1]).m_name == "}"))
|
||||
ctSymbol.m_type = Type.Operator;
|
||||
else
|
||||
ctSymbol.m_type = Type.UnaryOperator;
|
||||
break;
|
||||
}
|
||||
m_equation.Add(ctSymbol);
|
||||
}
|
||||
break;
|
||||
case 2:
|
||||
if ((Char.IsNumber(equation[i])) || (equation[i] == '.') || (equation[i] == ','))
|
||||
temp += equation[i];
|
||||
else if (!Char.IsLetter(equation[i]))
|
||||
{
|
||||
state = 1;
|
||||
ctSymbol.m_name = temp;
|
||||
ctSymbol.m_value = Decimal.Parse(temp, System.Globalization.CultureInfo.InvariantCulture);
|
||||
ctSymbol.m_type = Type.Value;
|
||||
m_equation.Add(ctSymbol);
|
||||
ctSymbol.m_name = equation[i].ToString();
|
||||
ctSymbol.m_value = 0;
|
||||
switch (ctSymbol.m_name)
|
||||
{
|
||||
case ",":
|
||||
ctSymbol.m_type = Type.Comma;
|
||||
break;
|
||||
case "(":
|
||||
case ")":
|
||||
case "[":
|
||||
case "]":
|
||||
case "{":
|
||||
case "}":
|
||||
ctSymbol.m_type = Type.Bracket;
|
||||
break;
|
||||
default:
|
||||
if (m_equation.Count == 0)
|
||||
ctSymbol.m_type = Type.UnaryOperator;
|
||||
else if ((((Symbol)m_equation[m_equation.Count - 1]).m_type == Type.Value) || (((Symbol)m_equation[m_equation.Count - 1]).m_type == Type.Variable)
|
||||
|| (((Symbol)m_equation[m_equation.Count - 1]).m_name == ")") || (((Symbol)m_equation[m_equation.Count - 1]).m_name == "]") || (((Symbol)m_equation[m_equation.Count - 1]).m_name == "}"))
|
||||
ctSymbol.m_type = Type.Operator;
|
||||
else
|
||||
ctSymbol.m_type = Type.UnaryOperator;
|
||||
break;
|
||||
}
|
||||
m_equation.Add(ctSymbol);
|
||||
temp = "";
|
||||
}
|
||||
break;
|
||||
case 3:
|
||||
if (Char.IsLetterOrDigit(equation[i]))
|
||||
temp += equation[i];
|
||||
else
|
||||
{
|
||||
state = 1;
|
||||
ctSymbol.m_name = temp;
|
||||
ctSymbol.m_value = 0;
|
||||
if (equation[i] == '(')
|
||||
ctSymbol.m_type = Type.Function;
|
||||
else
|
||||
{
|
||||
if (ctSymbol.m_name == "pi")
|
||||
ctSymbol.m_value = DecimalMath.PI;
|
||||
else if (ctSymbol.m_name == "e")
|
||||
ctSymbol.m_value = DecimalMath.E;
|
||||
ctSymbol.m_type = Type.Variable;
|
||||
}
|
||||
m_equation.Add(ctSymbol);
|
||||
ctSymbol.m_name = equation[i].ToString();
|
||||
ctSymbol.m_value = 0;
|
||||
switch (ctSymbol.m_name)
|
||||
{
|
||||
case ",":
|
||||
ctSymbol.m_type = Type.Comma;
|
||||
break;
|
||||
case "(":
|
||||
case ")":
|
||||
case "[":
|
||||
case "]":
|
||||
case "{":
|
||||
case "}":
|
||||
ctSymbol.m_type = Type.Bracket;
|
||||
break;
|
||||
default:
|
||||
if (m_equation.Count == 0)
|
||||
ctSymbol.m_type = Type.UnaryOperator;
|
||||
else if ((((Symbol)m_equation[m_equation.Count - 1]).m_type == Type.Value) || (((Symbol)m_equation[m_equation.Count - 1]).m_type == Type.Variable)
|
||||
|| (((Symbol)m_equation[m_equation.Count - 1]).m_name == ")") || (((Symbol)m_equation[m_equation.Count - 1]).m_name == "]") || (((Symbol)m_equation[m_equation.Count - 1]).m_name == "}"))
|
||||
ctSymbol.m_type = Type.Operator;
|
||||
else
|
||||
ctSymbol.m_type = Type.UnaryOperator;
|
||||
break;
|
||||
}
|
||||
m_equation.Add(ctSymbol);
|
||||
temp = "";
|
||||
}
|
||||
break;
|
||||
}
|
||||
}
|
||||
if (temp != "")
|
||||
{
|
||||
ctSymbol.m_name = temp;
|
||||
if (state == 2)
|
||||
{
|
||||
ctSymbol.m_value = Decimal.Parse(temp, System.Globalization.CultureInfo.InvariantCulture);
|
||||
ctSymbol.m_type = Type.Value;
|
||||
}
|
||||
else
|
||||
{
|
||||
if (ctSymbol.m_name == "pi")
|
||||
ctSymbol.m_value = DecimalMath.PI;
|
||||
else if (ctSymbol.m_name == "e")
|
||||
ctSymbol.m_value = DecimalMath.E;
|
||||
else
|
||||
ctSymbol.m_value = 0;
|
||||
ctSymbol.m_type = Type.Variable;
|
||||
}
|
||||
m_equation.Add(ctSymbol);
|
||||
}
|
||||
}
|
||||
|
||||
public void Infix2Postfix()
|
||||
{
|
||||
Symbol tpSym;
|
||||
Stack tpStack = new Stack();
|
||||
foreach (Symbol sym in m_equation)
|
||||
{
|
||||
if ((sym.m_type == Type.Value) || (sym.m_type == Type.Variable))
|
||||
m_postfix.Add(sym);
|
||||
else if ((sym.m_name == "(") || (sym.m_name == "[") || (sym.m_name == "{"))
|
||||
tpStack.Push(sym);
|
||||
else if ((sym.m_name == ")") || (sym.m_name == "]") || (sym.m_name == "}"))
|
||||
{
|
||||
if (tpStack.Count > 0)
|
||||
{
|
||||
tpSym = (Symbol)tpStack.Pop();
|
||||
while ((tpSym.m_name != "(") && (tpSym.m_name != "[") && (tpSym.m_name != "{"))
|
||||
{
|
||||
m_postfix.Add(tpSym);
|
||||
tpSym = (Symbol)tpStack.Pop();
|
||||
}
|
||||
}
|
||||
}
|
||||
else if (sym.m_type == Type.UnaryOperator)
|
||||
{
|
||||
tpStack.Push(sym);
|
||||
}
|
||||
else
|
||||
{
|
||||
if (tpStack.Count > 0)
|
||||
{
|
||||
tpSym = (Symbol)tpStack.Pop();
|
||||
while ((tpStack.Count != 0) && ((tpSym.m_type == Type.UnaryOperator) || (tpSym.m_type == Type.Operator) || (tpSym.m_type == Type.Function) || (tpSym.m_type == Type.Comma)) && (Precedence(tpSym) >= Precedence(sym)))
|
||||
{
|
||||
m_postfix.Add(tpSym);
|
||||
tpSym = (Symbol)tpStack.Pop();
|
||||
}
|
||||
if (((tpSym.m_type == Type.UnaryOperator) || (tpSym.m_type == Type.Operator) || (tpSym.m_type == Type.Function) || (tpSym.m_type == Type.Comma)) && (Precedence(tpSym) >= Precedence(sym)))
|
||||
m_postfix.Add(tpSym);
|
||||
else
|
||||
tpStack.Push(tpSym);
|
||||
}
|
||||
tpStack.Push(sym);
|
||||
}
|
||||
}
|
||||
while (tpStack.Count > 0)
|
||||
{
|
||||
tpSym = (Symbol)tpStack.Pop();
|
||||
m_postfix.Add(tpSym);
|
||||
}
|
||||
}
|
||||
|
||||
public void EvaluatePostfix()
|
||||
{
|
||||
Symbol tpSym1, tpSym2, tpResult;
|
||||
Stack tpStack = new Stack();
|
||||
ArrayList fnParam = new ArrayList();
|
||||
m_bError = false;
|
||||
foreach (Symbol sym in m_postfix)
|
||||
{
|
||||
if ((sym.m_type == Type.Value) || (sym.m_type == Type.Variable) || (sym.m_type == Type.Result))
|
||||
tpStack.Push(sym);
|
||||
else if (sym.m_type == Type.UnaryOperator)
|
||||
{
|
||||
tpSym1 = (Symbol)tpStack.Pop();
|
||||
tpResult = Evaluate(sym, tpSym1);
|
||||
if (tpResult.m_type == Type.Error)
|
||||
{
|
||||
m_bError = true;
|
||||
m_sErrorDescription = tpResult.m_name;
|
||||
return;
|
||||
}
|
||||
tpStack.Push(tpResult);
|
||||
}
|
||||
else if (sym.m_type == Type.Operator)
|
||||
{
|
||||
tpSym1 = (Symbol)tpStack.Pop();
|
||||
tpSym2 = (Symbol)tpStack.Pop();
|
||||
tpResult = Evaluate(tpSym2, sym, tpSym1);
|
||||
if (tpResult.m_type == Type.Error)
|
||||
{
|
||||
m_bError = true;
|
||||
m_sErrorDescription = tpResult.m_name;
|
||||
return;
|
||||
}
|
||||
tpStack.Push(tpResult);
|
||||
}
|
||||
else if (sym.m_type == Type.Comma)
|
||||
{
|
||||
tpStack.Push(sym);
|
||||
}
|
||||
else if (sym.m_type == Type.Function)
|
||||
{
|
||||
fnParam.Clear();
|
||||
tpSym1 = (Symbol)tpStack.Pop();
|
||||
if ((tpSym1.m_type == Type.Value) || (tpSym1.m_type == Type.Variable) || (tpSym1.m_type == Type.Result))
|
||||
{
|
||||
tpResult = EvaluateFunction(sym.m_name, tpSym1);
|
||||
if (tpResult.m_type == Type.Error)
|
||||
{
|
||||
m_bError = true;
|
||||
m_sErrorDescription = tpResult.m_name;
|
||||
return;
|
||||
}
|
||||
tpStack.Push(tpResult);
|
||||
}
|
||||
else if (tpSym1.m_type == Type.Comma)
|
||||
{
|
||||
while (tpSym1.m_type == Type.Comma)
|
||||
{
|
||||
tpSym1 = (Symbol)tpStack.Pop();
|
||||
fnParam.Add(tpSym1);
|
||||
tpSym1 = (Symbol)tpStack.Pop();
|
||||
}
|
||||
fnParam.Add(tpSym1);
|
||||
tpResult = EvaluateFunction(sym.m_name, fnParam.ToArray());
|
||||
if (tpResult.m_type == Type.Error)
|
||||
{
|
||||
m_bError = true;
|
||||
m_sErrorDescription = tpResult.m_name;
|
||||
return;
|
||||
}
|
||||
tpStack.Push(tpResult);
|
||||
}
|
||||
else
|
||||
{
|
||||
tpStack.Push(tpSym1);
|
||||
tpResult = EvaluateFunction(sym.m_name);
|
||||
if (tpResult.m_type == Type.Error)
|
||||
{
|
||||
m_bError = true;
|
||||
m_sErrorDescription = tpResult.m_name;
|
||||
return;
|
||||
}
|
||||
tpStack.Push(tpResult);
|
||||
}
|
||||
}
|
||||
}
|
||||
if (tpStack.Count == 1)
|
||||
{
|
||||
tpResult = (Symbol)tpStack.Pop();
|
||||
m_result = tpResult.m_value;
|
||||
}
|
||||
}
|
||||
|
||||
protected int Precedence(Symbol sym)
|
||||
{
|
||||
switch (sym.m_type)
|
||||
{
|
||||
case Type.Bracket:
|
||||
return 7;
|
||||
case Type.Function:
|
||||
return 5;
|
||||
case Type.UnaryOperator:
|
||||
return 4;
|
||||
case Type.Comma:
|
||||
return 0;
|
||||
}
|
||||
switch (sym.m_name)
|
||||
{
|
||||
case "^":
|
||||
return 3;
|
||||
case "/":
|
||||
case "*":
|
||||
case "%":
|
||||
return 2;
|
||||
case "+":
|
||||
case "-":
|
||||
return 1;
|
||||
}
|
||||
return -1;
|
||||
}
|
||||
|
||||
protected Symbol Evaluate(Symbol opr, Symbol sym1)
|
||||
{
|
||||
Symbol result;
|
||||
result.m_name = opr.m_name + sym1.m_name;
|
||||
result.m_type = Type.Result;
|
||||
result.m_value = 0;
|
||||
switch (opr.m_name)
|
||||
{
|
||||
case "+":
|
||||
result.m_value = sym1.m_value; ;
|
||||
break;
|
||||
case "-":
|
||||
result.m_value = -1 * sym1.m_value;
|
||||
break;
|
||||
default:
|
||||
result.m_type = Type.Error;
|
||||
result.m_name = "Undefine unary operator: " + opr.m_name + ".";
|
||||
break;
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
protected Symbol Evaluate(Symbol sym1, Symbol opr, Symbol sym2)
|
||||
{
|
||||
Symbol result;
|
||||
result.m_name = sym1.m_name + opr.m_name + sym2.m_name;
|
||||
result.m_type = Type.Result;
|
||||
result.m_value = 0;
|
||||
switch (opr.m_name)
|
||||
{
|
||||
case "^":
|
||||
result.m_value = DecimalMath.Pow(sym1.m_value, sym2.m_value);
|
||||
break;
|
||||
case "/":
|
||||
{
|
||||
if (sym2.m_value != 0)
|
||||
result.m_value = sym1.m_value / sym2.m_value;
|
||||
else
|
||||
{
|
||||
result.m_name = "Divide by Zero.";
|
||||
result.m_type = Type.Error;
|
||||
}
|
||||
break;
|
||||
}
|
||||
case "*":
|
||||
result.m_value = sym1.m_value * sym2.m_value;
|
||||
break;
|
||||
case "%":
|
||||
result.m_value = sym1.m_value % sym2.m_value;
|
||||
break;
|
||||
case "+":
|
||||
result.m_value = sym1.m_value + sym2.m_value;
|
||||
break;
|
||||
case "-":
|
||||
result.m_value = sym1.m_value - sym2.m_value;
|
||||
break;
|
||||
default:
|
||||
result.m_type = Type.Error;
|
||||
result.m_name = "Undefine operator: " + opr.m_name + ".";
|
||||
break;
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
protected Symbol EvaluateFunction(string name, params Object[] args)
|
||||
{
|
||||
Symbol result;
|
||||
result.m_name = "";
|
||||
result.m_type = Type.Result;
|
||||
result.m_value = 0;
|
||||
switch (name)
|
||||
{
|
||||
case "cos":
|
||||
if (args.Length == 1)
|
||||
{
|
||||
result.m_name = name + "(" + ((Symbol)args[0]).m_value.ToString() + ")";
|
||||
result.m_value = DecimalMath.Cos(((Symbol)args[0]).m_value * DecimalMath.PI / 180);
|
||||
}
|
||||
else
|
||||
{
|
||||
result.m_name = "Invalid number of parameters in: " + name + ".";
|
||||
result.m_type = Type.Error;
|
||||
}
|
||||
break;
|
||||
case "sin":
|
||||
if (args.Length == 1)
|
||||
{
|
||||
result.m_name = name + "(" + ((Symbol)args[0]).m_value.ToString() + ")";
|
||||
result.m_value = DecimalMath.Sin(((Symbol)args[0]).m_value * DecimalMath.PI / 180);
|
||||
}
|
||||
else
|
||||
{
|
||||
result.m_name = "Invalid number of parameters in: " + name + ".";
|
||||
result.m_type = Type.Error;
|
||||
}
|
||||
break;
|
||||
case "tan":
|
||||
if (args.Length == 1)
|
||||
{
|
||||
result.m_name = name + "(" + ((Symbol)args[0]).m_value.ToString() + ")";
|
||||
result.m_value = DecimalMath.Tan(((Symbol)args[0]).m_value * DecimalMath.PI / 180);
|
||||
}
|
||||
else
|
||||
{
|
||||
result.m_name = "Invalid number of parameters in: " + name + ".";
|
||||
result.m_type = Type.Error;
|
||||
}
|
||||
break;
|
||||
case "cosh":
|
||||
if (args.Length == 1)
|
||||
{
|
||||
result.m_name = name + "(" + ((Symbol)args[0]).m_value.ToString() + ")";
|
||||
result.m_value = DecimalMath.Cosh(((Symbol)args[0]).m_value);
|
||||
}
|
||||
else
|
||||
{
|
||||
result.m_name = "Invalid number of parameters in: " + name + ".";
|
||||
result.m_type = Type.Error;
|
||||
}
|
||||
break;
|
||||
case "sinh":
|
||||
if (args.Length == 1)
|
||||
{
|
||||
result.m_name = name + "(" + ((Symbol)args[0]).m_value.ToString() + ")";
|
||||
result.m_value = DecimalMath.Sinh(((Symbol)args[0]).m_value);
|
||||
}
|
||||
else
|
||||
{
|
||||
result.m_name = "Invalid number of parameters in: " + name + ".";
|
||||
result.m_type = Type.Error;
|
||||
}
|
||||
break;
|
||||
case "tanh":
|
||||
if (args.Length == 1)
|
||||
{
|
||||
result.m_name = name + "(" + ((Symbol)args[0]).m_value.ToString() + ")";
|
||||
result.m_value = DecimalMath.Tanh(((Symbol)args[0]).m_value);
|
||||
}
|
||||
else
|
||||
{
|
||||
result.m_name = "Invalid number of parameters in: " + name + ".";
|
||||
result.m_type = Type.Error;
|
||||
}
|
||||
break;
|
||||
case "log":
|
||||
if (args.Length == 1)
|
||||
{
|
||||
result.m_name = name + "(" + ((Symbol)args[0]).m_value.ToString() + ")";
|
||||
result.m_value = DecimalMath.Log10(((Symbol)args[0]).m_value);
|
||||
}
|
||||
else
|
||||
{
|
||||
result.m_name = "Invalid number of parameters in: " + name + ".";
|
||||
result.m_type = Type.Error;
|
||||
}
|
||||
break;
|
||||
case "ln":
|
||||
if (args.Length == 1)
|
||||
{
|
||||
result.m_name = name + "(" + ((Symbol)args[0]).m_value.ToString() + ")";
|
||||
result.m_value = DecimalMath.Log(((Symbol)args[0]).m_value);
|
||||
}
|
||||
else
|
||||
{
|
||||
result.m_name = "Invalid number of parameters in: " + name + ".";
|
||||
result.m_type = Type.Error;
|
||||
}
|
||||
break;
|
||||
case "logn":
|
||||
if (args.Length == 2)
|
||||
{
|
||||
result.m_name = name + "(" + ((Symbol)args[0]).m_value.ToString() + "'" + ((Symbol)args[1]).m_value.ToString() + ")";
|
||||
// log_b(x) = ln(x) / ln(b)
|
||||
result.m_value = DecimalMath.Log(((Symbol)args[0]).m_value) / DecimalMath.Log(((Symbol)args[1]).m_value);
|
||||
}
|
||||
else
|
||||
{
|
||||
result.m_name = "Invalid number of parameters in: " + name + ".";
|
||||
result.m_type = Type.Error;
|
||||
}
|
||||
break;
|
||||
case "sqrt":
|
||||
if (args.Length == 1)
|
||||
{
|
||||
result.m_name = name + "(" + ((Symbol)args[0]).m_value.ToString() + ")";
|
||||
result.m_value = DecimalMath.Sqrt(((Symbol)args[0]).m_value);
|
||||
}
|
||||
else
|
||||
{
|
||||
result.m_name = "Invalid number of parameters in: " + name + ".";
|
||||
result.m_type = Type.Error;
|
||||
}
|
||||
break;
|
||||
case "abs":
|
||||
if (args.Length == 1)
|
||||
{
|
||||
result.m_name = name + "(" + ((Symbol)args[0]).m_value.ToString() + ")";
|
||||
result.m_value = DecimalMath.Abs(((Symbol)args[0]).m_value);
|
||||
}
|
||||
else
|
||||
{
|
||||
result.m_name = "Invalid number of parameters in: " + name + ".";
|
||||
result.m_type = Type.Error;
|
||||
}
|
||||
break;
|
||||
case "acos":
|
||||
if (args.Length == 1)
|
||||
{
|
||||
result.m_name = name + "(" + ((Symbol)args[0]).m_value.ToString() + ")";
|
||||
result.m_value = DecimalMath.Acos(((Symbol)args[0]).m_value) * 180 / DecimalMath.PI;
|
||||
}
|
||||
else
|
||||
{
|
||||
result.m_name = "Invalid number of parameters in: " + name + ".";
|
||||
result.m_type = Type.Error;
|
||||
}
|
||||
break;
|
||||
case "asin":
|
||||
if (args.Length == 1)
|
||||
{
|
||||
result.m_name = name + "(" + ((Symbol)args[0]).m_value.ToString() + ")";
|
||||
result.m_value = DecimalMath.Asin(((Symbol)args[0]).m_value);
|
||||
}
|
||||
else
|
||||
{
|
||||
result.m_name = "Invalid number of parameters in: " + name + ".";
|
||||
result.m_type = Type.Error;
|
||||
}
|
||||
break;
|
||||
case "atan":
|
||||
if (args.Length == 1)
|
||||
{
|
||||
result.m_name = name + "(" + ((Symbol)args[0]).m_value.ToString() + ")";
|
||||
result.m_value = DecimalMath.Atan(((Symbol)args[0]).m_value) * 180 / DecimalMath.PI;
|
||||
}
|
||||
else
|
||||
{
|
||||
result.m_name = "Invalid number of parameters in: " + name + ".";
|
||||
result.m_type = Type.Error;
|
||||
}
|
||||
break;
|
||||
case "exp":
|
||||
if (args.Length == 1)
|
||||
{
|
||||
result.m_name = name + "(" + ((Symbol)args[0]).m_value.ToString() + ")";
|
||||
result.m_value = DecimalMath.Exp(((Symbol)args[0]).m_value);
|
||||
}
|
||||
else
|
||||
{
|
||||
result.m_name = "Invalid number of parameters in: " + name + ".";
|
||||
result.m_type = Type.Error;
|
||||
}
|
||||
break;
|
||||
default:
|
||||
if (m_defaultFunctionEvaluation != null)
|
||||
result = m_defaultFunctionEvaluation(name, args);
|
||||
else
|
||||
{
|
||||
result.m_name = "Function: " + name + ", not found.";
|
||||
result.m_type = Type.Error;
|
||||
}
|
||||
break;
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
protected bool m_bError = false;
|
||||
protected string m_sErrorDescription = "None";
|
||||
public decimal m_result = 0;
|
||||
public ArrayList m_equation = new ArrayList();
|
||||
public ArrayList m_postfix = new ArrayList();
|
||||
protected EvaluateFunctionDelegate m_defaultFunctionEvaluation;
|
||||
}
|
||||
318
MathApp/MathApp/Core/Matrix.cs
Normal file
318
MathApp/MathApp/Core/Matrix.cs
Normal file
|
|
@ -0,0 +1,318 @@
|
|||
using System;
|
||||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
using System.Globalization;
|
||||
|
||||
public class Matrix
|
||||
{
|
||||
private static Random random = new Random();
|
||||
|
||||
public static decimal[][] GenerateRandomMatrix(int rows, int columns, decimal min, decimal max)
|
||||
{
|
||||
decimal[][] res = new decimal[rows][];
|
||||
for (int i = 0; i < rows; i++)
|
||||
{
|
||||
res[i] = new decimal[columns];
|
||||
for (int j = 0; j < columns; j++)
|
||||
{
|
||||
// Replaced UnityEngine.Random with System.Random
|
||||
double range = (double)(max - min);
|
||||
double sample = random.NextDouble();
|
||||
double scaled = (sample * range) + (double)min;
|
||||
decimal value = (decimal)System.Math.Round(scaled, 2);
|
||||
res[i][j] = value;
|
||||
}
|
||||
}
|
||||
return res;
|
||||
}
|
||||
|
||||
public static decimal[][] Transpoze(decimal[][] _m)
|
||||
{
|
||||
decimal[][] res = new decimal[_m[0].Length][];
|
||||
for (int i = 0; i < _m[0].Length; i++)
|
||||
{
|
||||
res[i] = new decimal[_m.Length];
|
||||
}
|
||||
for (int i = 0; i < _m.Length; i++)
|
||||
{
|
||||
for (int j = 0; j < _m[0].Length; j++)
|
||||
{
|
||||
res[j][i] = _m[i][j];
|
||||
}
|
||||
}
|
||||
return res;
|
||||
}
|
||||
|
||||
public static decimal MatrixDeterminant(decimal[][] _m)
|
||||
{
|
||||
if (_m.Length == _m[0].Length)
|
||||
{
|
||||
int c = _m.Length;
|
||||
if (c == 2)
|
||||
{
|
||||
return (decimal)Math.Round(_m[0][0] * _m[1][1] - _m[0][1] * _m[1][0], 5);
|
||||
}
|
||||
else if (c == 1)
|
||||
{
|
||||
return (decimal)Math.Round(_m[0][0], 5);
|
||||
}
|
||||
else if (c == 0)
|
||||
{
|
||||
Console.WriteLine("Matrix is empty!");
|
||||
return 0;
|
||||
}
|
||||
else
|
||||
{
|
||||
decimal resDet = 0;
|
||||
for (int i = 0; i < c; i++)
|
||||
{
|
||||
for (int j = 0; j < c; j++)
|
||||
{
|
||||
decimal[][] newM = new decimal[c - 1][];
|
||||
for (int k = 0; k < c - 1; k++)
|
||||
{
|
||||
newM[k] = new decimal[c - 1];
|
||||
}
|
||||
|
||||
bool isAddedK = false, isAddedL = false;
|
||||
int offsetK = 0, offsetL = 0;
|
||||
|
||||
for (int k = 0; k < c; k++)
|
||||
{
|
||||
isAddedL = false;
|
||||
offsetL = 0;
|
||||
for (int l = 0; l < c; l++)
|
||||
{
|
||||
if (!(k == i || l == j))
|
||||
{
|
||||
newM[k + offsetK][l + offsetL] = _m[k][l];
|
||||
}
|
||||
else
|
||||
{
|
||||
if (!isAddedK && k == i)
|
||||
{
|
||||
offsetK = -1;
|
||||
isAddedK = true;
|
||||
}
|
||||
if (!isAddedL && l == j)
|
||||
{
|
||||
offsetL = -1;
|
||||
isAddedL = true;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
decimal cofactor = (decimal)Math.Pow(-1, 0 + j + 2) * MatrixDeterminant(newM);
|
||||
resDet += cofactor * _m[0][j];
|
||||
}
|
||||
}
|
||||
return (decimal)Math.Round(resDet, 5);
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
Console.WriteLine("This matrix is not a square matrix!");
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
|
||||
public static decimal[] SolveEquation(decimal[][] _m, decimal[] _v)
|
||||
{
|
||||
int len = _m.Length;
|
||||
|
||||
decimal[] res = new decimal[len];
|
||||
decimal d0 = MatrixDeterminant(_m);
|
||||
|
||||
for (int i = 0; i < len; i++)
|
||||
{
|
||||
decimal[][] testMa = new decimal[len][];
|
||||
for (int j = 0; j < len; j++)
|
||||
{
|
||||
testMa[j] = new decimal[len];
|
||||
for (int k = 0; k < len; k++)
|
||||
{
|
||||
testMa[j][k] = _m[j][k];
|
||||
}
|
||||
}
|
||||
for (int j = 0; j < len; j++)
|
||||
{
|
||||
testMa[j][i] = _v[j];
|
||||
}
|
||||
decimal di = MatrixDeterminant(testMa);
|
||||
res[i] = (decimal)Math.Round(di / d0, 5);
|
||||
}
|
||||
|
||||
return res;
|
||||
}
|
||||
|
||||
public static decimal[][] ConvertLinearEquationsToMatrix(List<string> equations, List<string> variables)
|
||||
{
|
||||
if (equations.Count != variables.Count) return null;
|
||||
|
||||
decimal[][] res = new decimal[equations.Count][];
|
||||
for (int i = 0; i < equations.Count; i++)
|
||||
{
|
||||
res[i] = new decimal[equations.Count];
|
||||
string eq = equations[i].Replace(" ", ""); // Remove spaces
|
||||
|
||||
// Split by operators but keep them?
|
||||
// Better approach: Go through each variable and find its coefficient in the string
|
||||
|
||||
for (int k = 0; k < variables.Count; k++)
|
||||
{
|
||||
string varName = variables[k];
|
||||
// Find all occurrences? A variable should appear once per equation linearly.
|
||||
// We need to look for "...[coeff]varName..."
|
||||
// Simple parser: Scan the equation string.
|
||||
|
||||
// Let's use a robust approach: Replace all other variables with 0 and evaluate?
|
||||
// No, that requires an expression evaluator.
|
||||
|
||||
// String manipulation approach (as per original code intent but fixed):
|
||||
int varIndex = eq.IndexOf(varName);
|
||||
if (varIndex != -1)
|
||||
{
|
||||
// Find coefficient ending at varIndex - 1
|
||||
string coeffStr = "";
|
||||
int curr = varIndex - 1;
|
||||
|
||||
// Scan backwards to find numbers or sign
|
||||
while (curr >= 0)
|
||||
{
|
||||
char c = eq[curr];
|
||||
if (char.IsDigit(c) || c == '.')
|
||||
{
|
||||
coeffStr = c + coeffStr;
|
||||
curr--;
|
||||
}
|
||||
else if (c == '+' || c == '-')
|
||||
{
|
||||
coeffStr = c + coeffStr;
|
||||
break; // Sign is the start of the coefficient
|
||||
}
|
||||
else
|
||||
{
|
||||
break; // Another char (maybe another variable?)
|
||||
}
|
||||
}
|
||||
|
||||
if (string.IsNullOrEmpty(coeffStr) || coeffStr == "+")
|
||||
{
|
||||
res[i][k] = 1;
|
||||
}
|
||||
else if (coeffStr == "-")
|
||||
{
|
||||
res[i][k] = -1;
|
||||
}
|
||||
else
|
||||
{
|
||||
if (decimal.TryParse(coeffStr, NumberStyles.Any, CultureInfo.InvariantCulture, out decimal val))
|
||||
{
|
||||
res[i][k] = val;
|
||||
}
|
||||
else
|
||||
{
|
||||
// Parse error fallback
|
||||
res[i][k] = 1;
|
||||
}
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
res[i][k] = 0;
|
||||
}
|
||||
}
|
||||
}
|
||||
return res;
|
||||
}
|
||||
|
||||
public static decimal[][] MatrixMultiplication(decimal[][] _m1, decimal[][] _m2)
|
||||
{
|
||||
if (_m1[0].Length == _m2.Length)
|
||||
{
|
||||
decimal[][] res = new decimal[_m1.Length][];
|
||||
for (int i = 0; i < _m1.Length; i++)
|
||||
{
|
||||
res[i] = new decimal[_m2[0].Length];
|
||||
}
|
||||
|
||||
for (int i = 0; i < res.Length; i++)
|
||||
{
|
||||
for (int j = 0; j < res[0].Length; j++)
|
||||
{
|
||||
decimal sum = 0;
|
||||
for (int k = 0; k < _m2.Length; k++)
|
||||
{
|
||||
sum += _m1[i][k] * _m2[k][j];
|
||||
}
|
||||
res[i][j] = sum;
|
||||
}
|
||||
}
|
||||
return res;
|
||||
}
|
||||
else
|
||||
{
|
||||
Console.WriteLine("Matrices cannot be multiplied!");
|
||||
return null;
|
||||
}
|
||||
}
|
||||
public static decimal[][] MatrixMultiplication(decimal[][] _m, decimal _c)
|
||||
{
|
||||
decimal[][] res = _m;
|
||||
for (int i = 0; i < _m.Length; i++)
|
||||
{
|
||||
for (int j = 0; j < _m[0].Length; j++)
|
||||
{
|
||||
res[i][j] *= _c;
|
||||
}
|
||||
}
|
||||
return res;
|
||||
}
|
||||
|
||||
public static decimal[] TurnListToVector(List<decimal> _vec)
|
||||
{
|
||||
decimal[] res = new decimal[_vec.Count];
|
||||
for (int i = 0; i < _vec.Count; i++)
|
||||
{
|
||||
res[i] = _vec[i];
|
||||
}
|
||||
return res;
|
||||
}
|
||||
public static decimal[][] TurnListToMatrix(List<List<decimal>> _m)
|
||||
{
|
||||
decimal[][] res = new decimal[_m.Count][];
|
||||
for (int i = 0; i < _m.Count; i++)
|
||||
{
|
||||
res[i] = new decimal[_m[i].Count];
|
||||
for (int j = 0; j < _m[i].Count; j++)
|
||||
{
|
||||
res[i][j] = _m[i][j];
|
||||
}
|
||||
}
|
||||
return res;
|
||||
}
|
||||
|
||||
public static string DebugMatrix(decimal[][] _m, string _sep = " ")
|
||||
{
|
||||
string sum = "";
|
||||
for (int i = 0; i < _m.Length; i++)
|
||||
{
|
||||
for (int j = 0; j < _m[0].Length; j++)
|
||||
{
|
||||
sum += _m[i][j] + ((j == _m[0].Length - 1) ? "\n" : _sep);
|
||||
}
|
||||
}
|
||||
return sum;
|
||||
}
|
||||
public static string DebugVector(decimal[] _v, string _sep = " ")
|
||||
{
|
||||
string sum = "";
|
||||
for (int i = 0; i < _v.Length; i++)
|
||||
{
|
||||
sum += _v[i] + "\n";
|
||||
}
|
||||
return sum;
|
||||
}
|
||||
|
||||
}
|
||||
174
MathApp/MathApp/Core/Matrix/CholeskyDecomposition.cs
Normal file
174
MathApp/MathApp/Core/Matrix/CholeskyDecomposition.cs
Normal file
|
|
@ -0,0 +1,174 @@
|
|||
using System;
|
||||
using System.Runtime.Serialization;
|
||||
|
||||
namespace DotNetMatrix
|
||||
{
|
||||
|
||||
/// <summary>Cholesky Decomposition.
|
||||
/// For a symmetric, positive definite matrix A, the Cholesky decomposition
|
||||
/// is an lower triangular matrix L so that A = L*L'.
|
||||
/// If the matrix is not symmetric or positive definite, the constructor
|
||||
/// returns a partial decomposition and sets an internal flag that may
|
||||
/// be queried by the isSPD() method.
|
||||
/// </summary>
|
||||
|
||||
[Serializable]
|
||||
public class CholeskyDecomposition : System.Runtime.Serialization.ISerializable
|
||||
{
|
||||
#region Class variables
|
||||
|
||||
/// <summary>Array for internal storage of decomposition.
|
||||
/// @serial internal array storage.
|
||||
/// </summary>
|
||||
private decimal[][] L;
|
||||
|
||||
/// <summary>Row and column dimension (square matrix).
|
||||
/// @serial matrix dimension.
|
||||
/// </summary>
|
||||
private int n;
|
||||
|
||||
/// <summary>Symmetric and positive definite flag.
|
||||
/// @serial is symmetric and positive definite flag.
|
||||
/// </summary>
|
||||
private bool isspd;
|
||||
|
||||
#endregion // Class variables
|
||||
|
||||
#region Constructor
|
||||
|
||||
/// <summary>Cholesky algorithm for symmetric and positive definite matrix.</summary>
|
||||
/// <param name="Arg"> Square, symmetric matrix.
|
||||
/// </param>
|
||||
/// <returns> Structure to access L and isspd flag.
|
||||
/// </returns>
|
||||
|
||||
public CholeskyDecomposition(GeneralMatrix Arg)
|
||||
{
|
||||
// Initialize.
|
||||
decimal[][] A = Arg.Array;
|
||||
n = Arg.RowDimension;
|
||||
L = new decimal[n][];
|
||||
for (int i = 0; i < n; i++)
|
||||
{
|
||||
L[i] = new decimal[n];
|
||||
}
|
||||
isspd = (Arg.ColumnDimension == n);
|
||||
// Main loop.
|
||||
for (int j = 0; j < n; j++)
|
||||
{
|
||||
decimal[] Lrowj = L[j];
|
||||
decimal d = 0.0m;
|
||||
for (int k = 0; k < j; k++)
|
||||
{
|
||||
decimal[] Lrowk = L[k];
|
||||
decimal s = 0.0m;
|
||||
for (int i = 0; i < k; i++)
|
||||
{
|
||||
s += Lrowk[i] * Lrowj[i];
|
||||
}
|
||||
Lrowj[k] = s = (A[j][k] - s) / L[k][k];
|
||||
d = d + s * s;
|
||||
isspd = isspd & (A[k][j] == A[j][k]);
|
||||
}
|
||||
d = A[j][j] - d;
|
||||
isspd = isspd & (d > 0.0m);
|
||||
L[j][j] = (decimal)Math.Sqrt(Math.Max((double)d, 0));
|
||||
for (int k = j + 1; k < n; k++)
|
||||
{
|
||||
L[j][k] = 0.0m;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#endregion // Constructor
|
||||
|
||||
#region Public Properties
|
||||
/// <summary>Is the matrix symmetric and positive definite?</summary>
|
||||
/// <returns> true if A is symmetric and positive definite.
|
||||
/// </returns>
|
||||
virtual public bool SPD
|
||||
{
|
||||
get
|
||||
{
|
||||
return isspd;
|
||||
}
|
||||
}
|
||||
#endregion // Public Properties
|
||||
|
||||
#region Public Methods
|
||||
|
||||
/// <summary>Return triangular factor.</summary>
|
||||
/// <returns> L
|
||||
/// </returns>
|
||||
|
||||
public virtual GeneralMatrix GetL()
|
||||
{
|
||||
return new GeneralMatrix(L, n, n);
|
||||
}
|
||||
|
||||
/// <summary>Solve A*X = B</summary>
|
||||
/// <param name="B"> A Matrix with as many rows as A and any number of columns.
|
||||
/// </param>
|
||||
/// <returns> X so that L*L'*X = B
|
||||
/// </returns>
|
||||
/// <exception cref="System.ArgumentException"> Matrix row dimensions must agree.
|
||||
/// </exception>
|
||||
/// <exception cref="System.SystemException"> Matrix is not symmetric positive definite.
|
||||
/// </exception>
|
||||
|
||||
public virtual GeneralMatrix Solve(GeneralMatrix B)
|
||||
{
|
||||
if (B.RowDimension != n)
|
||||
{
|
||||
throw new System.ArgumentException("Matrix row dimensions must agree.");
|
||||
}
|
||||
if (!isspd)
|
||||
{
|
||||
throw new System.SystemException("Matrix is not symmetric positive definite.");
|
||||
}
|
||||
|
||||
// Copy right hand side.
|
||||
decimal[][] X = B.ArrayCopy;
|
||||
int nx = B.ColumnDimension;
|
||||
|
||||
// Solve L*Y = B;
|
||||
for (int k = 0; k < n; k++)
|
||||
{
|
||||
for (int i = k + 1; i < n; i++)
|
||||
{
|
||||
for (int j = 0; j < nx; j++)
|
||||
{
|
||||
X[i][j] -= X[k][j] * L[i][k];
|
||||
}
|
||||
}
|
||||
for (int j = 0; j < nx; j++)
|
||||
{
|
||||
X[k][j] /= L[k][k];
|
||||
}
|
||||
}
|
||||
|
||||
// Solve L'*X = Y;
|
||||
for (int k = n - 1; k >= 0; k--)
|
||||
{
|
||||
for (int j = 0; j < nx; j++)
|
||||
{
|
||||
X[k][j] /= L[k][k];
|
||||
}
|
||||
for (int i = 0; i < k; i++)
|
||||
{
|
||||
for (int j = 0; j < nx; j++)
|
||||
{
|
||||
X[i][j] -= X[k][j] * L[k][i];
|
||||
}
|
||||
}
|
||||
}
|
||||
return new GeneralMatrix(X, n, nx);
|
||||
}
|
||||
#endregion // Public Methods
|
||||
|
||||
// A method called when serializing this class.
|
||||
void ISerializable.GetObjectData(SerializationInfo info, StreamingContext context)
|
||||
{
|
||||
}
|
||||
}
|
||||
}
|
||||
928
MathApp/MathApp/Core/Matrix/EigenvalueDecomposition.cs
Normal file
928
MathApp/MathApp/Core/Matrix/EigenvalueDecomposition.cs
Normal file
|
|
@ -0,0 +1,928 @@
|
|||
using System;
|
||||
using System.Runtime.InteropServices;
|
||||
using System.Runtime.Serialization;
|
||||
|
||||
namespace DotNetMatrix
|
||||
{
|
||||
[Serializable]
|
||||
public class EigenvalueDecomposition : System.Runtime.Serialization.ISerializable
|
||||
{
|
||||
private int n;
|
||||
private bool issymmetric;
|
||||
private decimal[] d, e;
|
||||
private decimal[][] V;
|
||||
private decimal[][] H;
|
||||
private decimal[] ort;
|
||||
|
||||
private void tred2()
|
||||
{
|
||||
for (int j = 0; j < n; j++)
|
||||
{
|
||||
d[j] = V[n - 1][j];
|
||||
}
|
||||
|
||||
for (int i = n - 1; i > 0; i--)
|
||||
{
|
||||
decimal scale = 0.0m;
|
||||
decimal h = 0.0m;
|
||||
for (int k = 0; k < i; k++)
|
||||
{
|
||||
scale = scale + System.Math.Abs(d[k]);
|
||||
}
|
||||
if (scale == 0.0m)
|
||||
{
|
||||
e[i] = d[i - 1];
|
||||
for (int j = 0; j < i; j++)
|
||||
{
|
||||
d[j] = V[i - 1][j];
|
||||
V[i][j] = 0.0m;
|
||||
V[j][i] = 0.0m;
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
for (int k = 0; k < i; k++)
|
||||
{
|
||||
d[k] /= scale;
|
||||
h += d[k] * d[k];
|
||||
}
|
||||
decimal f = d[i - 1];
|
||||
decimal g = (decimal)DecimalMath.Sqrt(h);
|
||||
if (f > 0)
|
||||
{
|
||||
g = - g;
|
||||
}
|
||||
e[i] = scale * g;
|
||||
h = h - f * g;
|
||||
d[i - 1] = f - g;
|
||||
for (int j = 0; j < i; j++)
|
||||
{
|
||||
e[j] = 0.0m;
|
||||
}
|
||||
|
||||
for (int j = 0; j < i; j++)
|
||||
{
|
||||
f = d[j];
|
||||
V[j][i] = f;
|
||||
g = e[j] + V[j][j] * f;
|
||||
for (int k = j + 1; k <= i - 1; k++)
|
||||
{
|
||||
g += V[k][j] * d[k];
|
||||
e[k] += V[k][j] * f;
|
||||
}
|
||||
e[j] = g;
|
||||
}
|
||||
f = 0.0m;
|
||||
for (int j = 0; j < i; j++)
|
||||
{
|
||||
e[j] /= h;
|
||||
f += e[j] * d[j];
|
||||
}
|
||||
decimal hh = f / (h + h);
|
||||
for (int j = 0; j < i; j++)
|
||||
{
|
||||
e[j] -= hh * d[j];
|
||||
}
|
||||
for (int j = 0; j < i; j++)
|
||||
{
|
||||
f = d[j];
|
||||
g = e[j];
|
||||
for (int k = j; k <= i - 1; k++)
|
||||
{
|
||||
V[k][j] -= (f * e[k] + g * d[k]);
|
||||
}
|
||||
d[j] = V[i - 1][j];
|
||||
V[i][j] = 0.0m;
|
||||
}
|
||||
}
|
||||
d[i] = h;
|
||||
}
|
||||
|
||||
for (int i = 0; i < n - 1; i++)
|
||||
{
|
||||
V[n - 1][i] = V[i][i];
|
||||
V[i][i] = 1.0m;
|
||||
decimal h = d[i + 1];
|
||||
if (h != 0.0m)
|
||||
{
|
||||
for (int k = 0; k <= i; k++)
|
||||
{
|
||||
d[k] = V[k][i + 1] / h;
|
||||
}
|
||||
for (int j = 0; j <= i; j++)
|
||||
{
|
||||
decimal g = 0.0m;
|
||||
for (int k = 0; k <= i; k++)
|
||||
{
|
||||
g += V[k][i + 1] * V[k][j];
|
||||
}
|
||||
for (int k = 0; k <= i; k++)
|
||||
{
|
||||
V[k][j] -= g * d[k];
|
||||
}
|
||||
}
|
||||
}
|
||||
for (int k = 0; k <= i; k++)
|
||||
{
|
||||
V[k][i + 1] = 0.0m;
|
||||
}
|
||||
}
|
||||
for (int j = 0; j < n; j++)
|
||||
{
|
||||
d[j] = V[n - 1][j];
|
||||
V[n - 1][j] = 0.0m;
|
||||
}
|
||||
V[n - 1][n - 1] = 1.0m;
|
||||
e[0] = 0.0m;
|
||||
}
|
||||
|
||||
private void tql2()
|
||||
{
|
||||
for (int i = 1; i < n; i++)
|
||||
{
|
||||
e[i - 1] = e[i];
|
||||
}
|
||||
e[n - 1] = 0.0m;
|
||||
|
||||
decimal f = 0.0m;
|
||||
decimal tst1 = 0.0m;
|
||||
decimal eps = (decimal)DecimalMath.Pow(2.0m, - 52.0m);
|
||||
for (int l = 0; l < n; l++)
|
||||
{
|
||||
tst1 = System.Math.Max(tst1, System.Math.Abs(d[l]) + System.Math.Abs(e[l]));
|
||||
int m = l;
|
||||
while (m < n)
|
||||
{
|
||||
if (System.Math.Abs(e[m]) <= eps * tst1)
|
||||
{
|
||||
break;
|
||||
}
|
||||
m++;
|
||||
}
|
||||
|
||||
if (m > l)
|
||||
{
|
||||
int iter = 0;
|
||||
do
|
||||
{
|
||||
iter = iter + 1;
|
||||
decimal g = d[l];
|
||||
decimal p = (d[l + 1] - g) / (2.0m * e[l]);
|
||||
decimal r = Maths.Hypot(p, 1.0m);
|
||||
if (p < 0)
|
||||
{
|
||||
r = - r;
|
||||
}
|
||||
d[l] = e[l] / (p + r);
|
||||
d[l + 1] = e[l] * (p + r);
|
||||
decimal dl1 = d[l + 1];
|
||||
decimal h = g - d[l];
|
||||
for (int i = l + 2; i < n; i++)
|
||||
{
|
||||
d[i] -= h;
|
||||
}
|
||||
f = f + h;
|
||||
|
||||
p = d[m];
|
||||
decimal c = 1.0m;
|
||||
decimal c2 = c;
|
||||
decimal c3 = c;
|
||||
decimal el1 = e[l + 1];
|
||||
decimal s = 0.0m;
|
||||
decimal s2 = 0.0m;
|
||||
for (int i = m - 1; i >= l; i--)
|
||||
{
|
||||
c3 = c2;
|
||||
c2 = c;
|
||||
s2 = s;
|
||||
g = c * e[i];
|
||||
h = c * p;
|
||||
r = Maths.Hypot(p, e[i]);
|
||||
e[i + 1] = s * r;
|
||||
s = e[i] / r;
|
||||
c = p / r;
|
||||
p = c * d[i] - s * g;
|
||||
d[i + 1] = h + s * (c * g + s * d[i]);
|
||||
|
||||
for (int k = 0; k < n; k++)
|
||||
{
|
||||
h = V[k][i + 1];
|
||||
V[k][i + 1] = s * V[k][i] + c * h;
|
||||
V[k][i] = c * V[k][i] - s * h;
|
||||
}
|
||||
}
|
||||
p = (- s) * s2 * c3 * el1 * e[l] / dl1;
|
||||
e[l] = s * p;
|
||||
d[l] = c * p;
|
||||
}
|
||||
while (System.Math.Abs(e[l]) > eps * tst1);
|
||||
}
|
||||
d[l] = d[l] + f;
|
||||
e[l] = 0.0m;
|
||||
}
|
||||
|
||||
for (int i = 0; i < n - 1; i++)
|
||||
{
|
||||
int k = i;
|
||||
decimal p = d[i];
|
||||
for (int j = i + 1; j < n; j++)
|
||||
{
|
||||
if (d[j] < p)
|
||||
{
|
||||
k = j;
|
||||
p = d[j];
|
||||
}
|
||||
}
|
||||
if (k != i)
|
||||
{
|
||||
d[k] = d[i];
|
||||
d[i] = p;
|
||||
for (int j = 0; j < n; j++)
|
||||
{
|
||||
p = V[j][i];
|
||||
V[j][i] = V[j][k];
|
||||
V[j][k] = p;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private void orthes()
|
||||
{
|
||||
int low = 0;
|
||||
int high = n - 1;
|
||||
|
||||
for (int m = low + 1; m <= high - 1; m++)
|
||||
{
|
||||
decimal scale = 0.0m;
|
||||
for (int i = m; i <= high; i++)
|
||||
{
|
||||
scale = scale + System.Math.Abs(H[i][m - 1]);
|
||||
}
|
||||
if (scale != 0.0m)
|
||||
{
|
||||
decimal h = 0.0m;
|
||||
for (int i = high; i >= m; i--)
|
||||
{
|
||||
ort[i] = H[i][m - 1] / scale;
|
||||
h += ort[i] * ort[i];
|
||||
}
|
||||
decimal g = (decimal)DecimalMath.Sqrt(h);
|
||||
if (ort[m] > 0)
|
||||
{
|
||||
g = - g;
|
||||
}
|
||||
h = h - ort[m] * g;
|
||||
ort[m] = ort[m] - g;
|
||||
|
||||
for (int j = m; j < n; j++)
|
||||
{
|
||||
decimal f = 0.0m;
|
||||
for (int i = high; i >= m; i--)
|
||||
{
|
||||
f += ort[i] * H[i][j];
|
||||
}
|
||||
f = f / h;
|
||||
for (int i = m; i <= high; i++)
|
||||
{
|
||||
H[i][j] -= f * ort[i];
|
||||
}
|
||||
}
|
||||
|
||||
for (int i = 0; i <= high; i++)
|
||||
{
|
||||
decimal f = 0.0m;
|
||||
for (int j = high; j >= m; j--)
|
||||
{
|
||||
f += ort[j] * H[i][j];
|
||||
}
|
||||
f = f / h;
|
||||
for (int j = m; j <= high; j++)
|
||||
{
|
||||
H[i][j] -= f * ort[j];
|
||||
}
|
||||
}
|
||||
ort[m] = scale * ort[m];
|
||||
H[m][m - 1] = scale * g;
|
||||
}
|
||||
}
|
||||
|
||||
for (int i = 0; i < n; i++)
|
||||
{
|
||||
for (int j = 0; j < n; j++)
|
||||
{
|
||||
V[i][j] = (i == j?1.0m:0.0m);
|
||||
}
|
||||
}
|
||||
|
||||
for (int m = high - 1; m >= low + 1; m--)
|
||||
{
|
||||
if (H[m][m - 1] != 0.0m)
|
||||
{
|
||||
for (int i = m + 1; i <= high; i++)
|
||||
{
|
||||
ort[i] = H[i][m - 1];
|
||||
}
|
||||
for (int j = m; j <= high; j++)
|
||||
{
|
||||
decimal g = 0.0m;
|
||||
for (int i = m; i <= high; i++)
|
||||
{
|
||||
g += ort[i] * V[i][j];
|
||||
}
|
||||
g = (g / ort[m]) / H[m][m - 1];
|
||||
for (int i = m; i <= high; i++)
|
||||
{
|
||||
V[i][j] += g * ort[i];
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
[NonSerialized()]
|
||||
private decimal cdivr, cdivi;
|
||||
|
||||
private void cdiv(decimal xr, decimal xi, decimal yr, decimal yi)
|
||||
{
|
||||
decimal r, d;
|
||||
if (System.Math.Abs(yr) > System.Math.Abs(yi))
|
||||
{
|
||||
r = yi / yr;
|
||||
d = yr + r * yi;
|
||||
cdivr = (xr + r * xi) / d;
|
||||
cdivi = (xi - r * xr) / d;
|
||||
}
|
||||
else
|
||||
{
|
||||
r = yr / yi;
|
||||
d = yi + r * yr;
|
||||
cdivr = (r * xr + xi) / d;
|
||||
cdivi = (r * xi - xr) / d;
|
||||
}
|
||||
}
|
||||
|
||||
private void hqr2()
|
||||
{
|
||||
int nn = this.n;
|
||||
int n = nn - 1;
|
||||
int low = 0;
|
||||
int high = nn - 1;
|
||||
decimal eps = (decimal)DecimalMath.Pow(2.0m, - 52.0m);
|
||||
decimal exshift = 0.0m;
|
||||
decimal p = 0, q = 0, r = 0, s = 0, z = 0, t, w, x, y;
|
||||
|
||||
decimal norm = 0.0m;
|
||||
for (int i = 0; i < nn; i++)
|
||||
{
|
||||
if (i < low | i > high)
|
||||
{
|
||||
d[i] = H[i][i];
|
||||
e[i] = 0.0m;
|
||||
}
|
||||
for (int j = System.Math.Max(i - 1, 0); j < nn; j++)
|
||||
{
|
||||
norm = norm + System.Math.Abs(H[i][j]);
|
||||
}
|
||||
}
|
||||
|
||||
int iter = 0;
|
||||
while (n >= low)
|
||||
{
|
||||
int l = n;
|
||||
while (l > low)
|
||||
{
|
||||
s = System.Math.Abs(H[l - 1][l - 1]) + System.Math.Abs(H[l][l]);
|
||||
if (s == 0.0m)
|
||||
{
|
||||
s = norm;
|
||||
}
|
||||
if (System.Math.Abs(H[l][l - 1]) < eps * s)
|
||||
{
|
||||
break;
|
||||
}
|
||||
l--;
|
||||
}
|
||||
|
||||
if (l == n)
|
||||
{
|
||||
H[n][n] = H[n][n] + exshift;
|
||||
d[n] = H[n][n];
|
||||
e[n] = 0.0m;
|
||||
n--;
|
||||
iter = 0;
|
||||
}
|
||||
else if (l == n - 1)
|
||||
{
|
||||
w = H[n][n - 1] * H[n - 1][n];
|
||||
p = (H[n - 1][n - 1] - H[n][n]) / 2.0m;
|
||||
q = p * p + w;
|
||||
z = (decimal)DecimalMath.Sqrt(System.Math.Abs(q));
|
||||
H[n][n] = H[n][n] + exshift;
|
||||
H[n - 1][n - 1] = H[n - 1][n - 1] + exshift;
|
||||
x = H[n][n];
|
||||
|
||||
if (q >= 0)
|
||||
{
|
||||
if (p >= 0)
|
||||
{
|
||||
z = p + z;
|
||||
}
|
||||
else
|
||||
{
|
||||
z = p - z;
|
||||
}
|
||||
d[n - 1] = x + z;
|
||||
d[n] = d[n - 1];
|
||||
if (z != 0.0m)
|
||||
{
|
||||
d[n] = x - w / z;
|
||||
}
|
||||
e[n - 1] = 0.0m;
|
||||
e[n] = 0.0m;
|
||||
x = H[n][n - 1];
|
||||
s = System.Math.Abs(x) + System.Math.Abs(z);
|
||||
p = x / s;
|
||||
q = z / s;
|
||||
r = (decimal)DecimalMath.Sqrt(p * p + q * q);
|
||||
p = p / r;
|
||||
q = q / r;
|
||||
|
||||
for (int j = n - 1; j < nn; j++)
|
||||
{
|
||||
z = H[n - 1][j];
|
||||
H[n - 1][j] = q * z + p * H[n][j];
|
||||
H[n][j] = q * H[n][j] - p * z;
|
||||
}
|
||||
|
||||
for (int i = 0; i <= n; i++)
|
||||
{
|
||||
z = H[i][n - 1];
|
||||
H[i][n - 1] = q * z + p * H[i][n];
|
||||
H[i][n] = q * H[i][n] - p * z;
|
||||
}
|
||||
|
||||
for (int i = low; i <= high; i++)
|
||||
{
|
||||
z = V[i][n - 1];
|
||||
V[i][n - 1] = q * z + p * V[i][n];
|
||||
V[i][n] = q * V[i][n] - p * z;
|
||||
}
|
||||
|
||||
}
|
||||
else
|
||||
{
|
||||
d[n - 1] = x + p;
|
||||
d[n] = x + p;
|
||||
e[n - 1] = z;
|
||||
e[n] = - z;
|
||||
}
|
||||
n = n - 2;
|
||||
iter = 0;
|
||||
|
||||
}
|
||||
else
|
||||
{
|
||||
x = H[n][n];
|
||||
y = 0.0m;
|
||||
w = 0.0m;
|
||||
if (l < n)
|
||||
{
|
||||
y = H[n - 1][n - 1];
|
||||
w = H[n][n - 1] * H[n - 1][n];
|
||||
}
|
||||
|
||||
if (iter == 10)
|
||||
{
|
||||
exshift += x;
|
||||
for (int i = low; i <= n; i++)
|
||||
{
|
||||
H[i][i] -= x;
|
||||
}
|
||||
s = System.Math.Abs(H[n][n - 1]) + System.Math.Abs(H[n - 1][n - 2]);
|
||||
x = y = 0.75m * s;
|
||||
w = (- 0.4375m) * s * s;
|
||||
}
|
||||
|
||||
if (iter == 30)
|
||||
{
|
||||
s = (y - x) / 2.0m;
|
||||
s = s * s + w;
|
||||
if (s > 0)
|
||||
{
|
||||
s = (decimal)DecimalMath.Sqrt(s);
|
||||
if (y < x)
|
||||
{
|
||||
s = - s;
|
||||
}
|
||||
s = x - w / ((y - x) / 2.0m + s);
|
||||
for (int i = low; i <= n; i++)
|
||||
{
|
||||
H[i][i] -= s;
|
||||
}
|
||||
exshift += s;
|
||||
x = y = w = 0.964m;
|
||||
}
|
||||
}
|
||||
|
||||
iter = iter + 1;
|
||||
|
||||
int m = n - 2;
|
||||
while (m >= l)
|
||||
{
|
||||
z = H[m][m];
|
||||
r = x - z;
|
||||
s = y - z;
|
||||
p = (r * s - w) / H[m + 1][m] + H[m][m + 1];
|
||||
q = H[m + 1][m + 1] - z - r - s;
|
||||
r = H[m + 2][m + 1];
|
||||
s = System.Math.Abs(p) + System.Math.Abs(q) + System.Math.Abs(r);
|
||||
p = p / s;
|
||||
q = q / s;
|
||||
r = r / s;
|
||||
if (m == l)
|
||||
{
|
||||
break;
|
||||
}
|
||||
if (System.Math.Abs(H[m][m - 1]) * (System.Math.Abs(q) + System.Math.Abs(r)) < eps * (System.Math.Abs(p) * (System.Math.Abs(H[m - 1][m - 1]) + System.Math.Abs(z) + System.Math.Abs(H[m + 1][m + 1]))))
|
||||
{
|
||||
break;
|
||||
}
|
||||
m--;
|
||||
}
|
||||
|
||||
for (int i = m + 2; i <= n; i++)
|
||||
{
|
||||
H[i][i - 2] = 0.0m;
|
||||
if (i > m + 2)
|
||||
{
|
||||
H[i][i - 3] = 0.0m;
|
||||
}
|
||||
}
|
||||
|
||||
for (int k = m; k <= n - 1; k++)
|
||||
{
|
||||
bool notlast = (k != n - 1);
|
||||
if (k != m)
|
||||
{
|
||||
p = H[k][k - 1];
|
||||
q = H[k + 1][k - 1];
|
||||
r = (notlast?H[k + 2][k - 1]:0.0m);
|
||||
x = System.Math.Abs(p) + System.Math.Abs(q) + System.Math.Abs(r);
|
||||
if (x != 0.0m)
|
||||
{
|
||||
p = p / x;
|
||||
q = q / x;
|
||||
r = r / x;
|
||||
}
|
||||
}
|
||||
if (x == 0.0m)
|
||||
{
|
||||
break;
|
||||
}
|
||||
s = (decimal)DecimalMath.Sqrt(p * p + q * q + r * r);
|
||||
if (p < 0)
|
||||
{
|
||||
s = - s;
|
||||
}
|
||||
if (s != 0)
|
||||
{
|
||||
if (k != m)
|
||||
{
|
||||
H[k][k - 1] = (- s) * x;
|
||||
}
|
||||
else if (l != m)
|
||||
{
|
||||
H[k][k - 1] = - H[k][k - 1];
|
||||
}
|
||||
p = p + s;
|
||||
x = p / s;
|
||||
y = q / s;
|
||||
z = r / s;
|
||||
q = q / p;
|
||||
r = r / p;
|
||||
|
||||
for (int j = k; j < nn; j++)
|
||||
{
|
||||
p = H[k][j] + q * H[k + 1][j];
|
||||
if (notlast)
|
||||
{
|
||||
p = p + r * H[k + 2][j];
|
||||
H[k + 2][j] = H[k + 2][j] - p * z;
|
||||
}
|
||||
H[k][j] = H[k][j] - p * x;
|
||||
H[k + 1][j] = H[k + 1][j] - p * y;
|
||||
}
|
||||
|
||||
for (int i = 0; i <= System.Math.Min(n, k + 3); i++)
|
||||
{
|
||||
p = x * H[i][k] + y * H[i][k + 1];
|
||||
if (notlast)
|
||||
{
|
||||
p = p + z * H[i][k + 2];
|
||||
H[i][k + 2] = H[i][k + 2] - p * r;
|
||||
}
|
||||
H[i][k] = H[i][k] - p;
|
||||
H[i][k + 1] = H[i][k + 1] - p * q;
|
||||
}
|
||||
|
||||
for (int i = low; i <= high; i++)
|
||||
{
|
||||
p = x * V[i][k] + y * V[i][k + 1];
|
||||
if (notlast)
|
||||
{
|
||||
p = p + z * V[i][k + 2];
|
||||
V[i][k + 2] = V[i][k + 2] - p * r;
|
||||
}
|
||||
V[i][k] = V[i][k] - p;
|
||||
V[i][k + 1] = V[i][k + 1] - p * q;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (norm == 0.0m)
|
||||
{
|
||||
return ;
|
||||
}
|
||||
|
||||
for (n = nn - 1; n >= 0; n--)
|
||||
{
|
||||
p = d[n];
|
||||
q = e[n];
|
||||
|
||||
if (q == 0)
|
||||
{
|
||||
int l = n;
|
||||
H[n][n] = 1.0m;
|
||||
for (int i = n - 1; i >= 0; i--)
|
||||
{
|
||||
w = H[i][i] - p;
|
||||
r = 0.0m;
|
||||
for (int j = l; j <= n; j++)
|
||||
{
|
||||
r = r + H[i][j] * H[j][n];
|
||||
}
|
||||
if (e[i] < 0.0m)
|
||||
{
|
||||
z = w;
|
||||
s = r;
|
||||
}
|
||||
else
|
||||
{
|
||||
l = i;
|
||||
if (e[i] == 0.0m)
|
||||
{
|
||||
if (w != 0.0m)
|
||||
{
|
||||
H[i][n] = (- r) / w;
|
||||
}
|
||||
else
|
||||
{
|
||||
H[i][n] = (- r) / (eps * norm);
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
x = H[i][i + 1];
|
||||
y = H[i + 1][i];
|
||||
q = (d[i] - p) * (d[i] - p) + e[i] * e[i];
|
||||
t = (x * s - z * r) / q;
|
||||
H[i][n] = t;
|
||||
if (System.Math.Abs(x) > System.Math.Abs(z))
|
||||
{
|
||||
H[i + 1][n] = (- r - w * t) / x;
|
||||
}
|
||||
else
|
||||
{
|
||||
H[i + 1][n] = (- s - y * t) / z;
|
||||
}
|
||||
}
|
||||
|
||||
t = System.Math.Abs(H[i][n]);
|
||||
if ((eps * t) * t > 1)
|
||||
{
|
||||
for (int j = i; j <= n; j++)
|
||||
{
|
||||
H[j][n] = H[j][n] / t;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
else if (q < 0)
|
||||
{
|
||||
int l = n - 1;
|
||||
if (System.Math.Abs(H[n][n - 1]) > System.Math.Abs(H[n - 1][n]))
|
||||
{
|
||||
H[n - 1][n - 1] = q / H[n][n - 1];
|
||||
H[n - 1][n] = (- (H[n][n] - p)) / H[n][n - 1];
|
||||
}
|
||||
else
|
||||
{
|
||||
cdiv(0.0m, - H[n - 1][n], H[n - 1][n - 1] - p, q);
|
||||
H[n - 1][n - 1] = cdivr;
|
||||
H[n - 1][n] = cdivi;
|
||||
}
|
||||
H[n][n - 1] = 0.0m;
|
||||
H[n][n] = 1.0m;
|
||||
for (int i = n - 2; i >= 0; i--)
|
||||
{
|
||||
decimal ra, sa, vr, vi;
|
||||
ra = 0.0m;
|
||||
sa = 0.0m;
|
||||
for (int j = l; j <= n; j++)
|
||||
{
|
||||
ra = ra + H[i][j] * H[j][n - 1];
|
||||
sa = sa + H[i][j] * H[j][n];
|
||||
}
|
||||
w = H[i][i] - p;
|
||||
|
||||
if (e[i] < 0.0m)
|
||||
{
|
||||
z = w;
|
||||
r = ra;
|
||||
s = sa;
|
||||
}
|
||||
else
|
||||
{
|
||||
l = i;
|
||||
if (e[i] == 0)
|
||||
{
|
||||
cdiv(- ra, - sa, w, q);
|
||||
H[i][n - 1] = cdivr;
|
||||
H[i][n] = cdivi;
|
||||
}
|
||||
else
|
||||
{
|
||||
x = H[i][i + 1];
|
||||
y = H[i + 1][i];
|
||||
vr = (d[i] - p) * (d[i] - p) + e[i] * e[i] - q * q;
|
||||
vi = (d[i] - p) * 2.0m * q;
|
||||
if (vr == 0.0m & vi == 0.0m)
|
||||
{
|
||||
vr = eps * norm * (System.Math.Abs(w) + System.Math.Abs(q) + System.Math.Abs(x) + System.Math.Abs(y) + System.Math.Abs(z));
|
||||
}
|
||||
cdiv(x * r - z * ra + q * sa, x * s - z * sa - q * ra, vr, vi);
|
||||
H[i][n - 1] = cdivr;
|
||||
H[i][n] = cdivi;
|
||||
if (System.Math.Abs(x) > (System.Math.Abs(z) + System.Math.Abs(q)))
|
||||
{
|
||||
H[i + 1][n - 1] = (- ra - w * H[i][n - 1] + q * H[i][n]) / x;
|
||||
H[i + 1][n] = (- sa - w * H[i][n] - q * H[i][n - 1]) / x;
|
||||
}
|
||||
else
|
||||
{
|
||||
cdiv(- r - y * H[i][n - 1], - s - y * H[i][n], z, q);
|
||||
H[i + 1][n - 1] = cdivr;
|
||||
H[i + 1][n] = cdivi;
|
||||
}
|
||||
}
|
||||
|
||||
t = System.Math.Max(System.Math.Abs(H[i][n - 1]), System.Math.Abs(H[i][n]));
|
||||
if ((eps * t) * t > 1)
|
||||
{
|
||||
for (int j = i; j <= n; j++)
|
||||
{
|
||||
H[j][n - 1] = H[j][n - 1] / t;
|
||||
H[j][n] = H[j][n] / t;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
for (int i = 0; i < nn; i++)
|
||||
{
|
||||
if (i < low | i > high)
|
||||
{
|
||||
for (int j = i; j < nn; j++)
|
||||
{
|
||||
V[i][j] = H[i][j];
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
for (int j = nn - 1; j >= low; j--)
|
||||
{
|
||||
for (int i = low; i <= high; i++)
|
||||
{
|
||||
z = 0.0m;
|
||||
for (int k = low; k <= System.Math.Min(j, high); k++)
|
||||
{
|
||||
z = z + V[i][k] * H[k][j];
|
||||
}
|
||||
V[i][j] = z;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public EigenvalueDecomposition(GeneralMatrix Arg)
|
||||
{
|
||||
decimal[][] A = Arg.Array;
|
||||
n = Arg.ColumnDimension;
|
||||
V = new decimal[n][];
|
||||
for (int i = 0; i < n; i++)
|
||||
{
|
||||
V[i] = new decimal[n];
|
||||
}
|
||||
d = new decimal[n];
|
||||
e = new decimal[n];
|
||||
|
||||
issymmetric = true;
|
||||
for (int j = 0; (j < n) & issymmetric; j++)
|
||||
{
|
||||
for (int i = 0; (i < n) & issymmetric; i++)
|
||||
{
|
||||
issymmetric = (A[i][j] == A[j][i]);
|
||||
}
|
||||
}
|
||||
|
||||
if (issymmetric)
|
||||
{
|
||||
for (int i = 0; i < n; i++)
|
||||
{
|
||||
for (int j = 0; j < n; j++)
|
||||
{
|
||||
V[i][j] = A[i][j];
|
||||
}
|
||||
}
|
||||
|
||||
tred2();
|
||||
tql2();
|
||||
}
|
||||
else
|
||||
{
|
||||
H = new decimal[n][];
|
||||
for (int i2 = 0; i2 < n; i2++)
|
||||
{
|
||||
H[i2] = new decimal[n];
|
||||
}
|
||||
ort = new decimal[n];
|
||||
|
||||
for (int j = 0; j < n; j++)
|
||||
{
|
||||
for (int i = 0; i < n; i++)
|
||||
{
|
||||
H[i][j] = A[i][j];
|
||||
}
|
||||
}
|
||||
|
||||
orthes();
|
||||
hqr2();
|
||||
}
|
||||
}
|
||||
|
||||
virtual public decimal[] RealEigenvalues
|
||||
{
|
||||
get
|
||||
{
|
||||
return d;
|
||||
}
|
||||
}
|
||||
virtual public decimal[] ImagEigenvalues
|
||||
{
|
||||
get
|
||||
{
|
||||
return e;
|
||||
}
|
||||
}
|
||||
virtual public GeneralMatrix D
|
||||
{
|
||||
get
|
||||
{
|
||||
GeneralMatrix X = new GeneralMatrix(n, n);
|
||||
decimal[][] D = X.Array;
|
||||
for (int i = 0; i < n; i++)
|
||||
{
|
||||
for (int j = 0; j < n; j++)
|
||||
{
|
||||
D[i][j] = 0.0m;
|
||||
}
|
||||
D[i][i] = d[i];
|
||||
if (e[i] > 0)
|
||||
{
|
||||
D[i][i + 1] = e[i];
|
||||
}
|
||||
else if (e[i] < 0)
|
||||
{
|
||||
D[i][i - 1] = e[i];
|
||||
}
|
||||
}
|
||||
return X;
|
||||
}
|
||||
}
|
||||
|
||||
public virtual GeneralMatrix GetV()
|
||||
{
|
||||
return new GeneralMatrix(V, n, n);
|
||||
}
|
||||
|
||||
void ISerializable.GetObjectData(SerializationInfo info, StreamingContext context)
|
||||
{
|
||||
}
|
||||
}
|
||||
}
|
||||
767
MathApp/MathApp/Core/Matrix/GeneralMatrix.cs
Normal file
767
MathApp/MathApp/Core/Matrix/GeneralMatrix.cs
Normal file
|
|
@ -0,0 +1,767 @@
|
|||
using System;
|
||||
using System.Runtime.Serialization;
|
||||
|
||||
namespace DotNetMatrix
|
||||
{
|
||||
internal class Maths
|
||||
{
|
||||
public static decimal Hypot(decimal a, decimal b)
|
||||
{
|
||||
decimal r;
|
||||
if (Math.Abs(a) > Math.Abs(b))
|
||||
{
|
||||
r = b/a;
|
||||
r = Math.Abs(a) * (decimal)DecimalMath.Sqrt(1 + r * r);
|
||||
}
|
||||
else if (b != 0)
|
||||
{
|
||||
r = a/b;
|
||||
r = Math.Abs(b) * (decimal)DecimalMath.Sqrt(1 + r * r);
|
||||
}
|
||||
else
|
||||
{
|
||||
r = 0.0m;
|
||||
}
|
||||
return r;
|
||||
}
|
||||
}
|
||||
|
||||
[Serializable]
|
||||
public class GeneralMatrix : System.ICloneable, System.Runtime.Serialization.ISerializable, System.IDisposable
|
||||
{
|
||||
private decimal[][] A;
|
||||
private int m, n;
|
||||
|
||||
public GeneralMatrix(int m, int n)
|
||||
{
|
||||
this.m = m;
|
||||
this.n = n;
|
||||
A = new decimal[m][];
|
||||
for (int i = 0; i < m; i++)
|
||||
{
|
||||
A[i] = new decimal[n];
|
||||
}
|
||||
}
|
||||
|
||||
public GeneralMatrix(int m, int n, decimal s)
|
||||
{
|
||||
this.m = m;
|
||||
this.n = n;
|
||||
A = new decimal[m][];
|
||||
for (int i = 0; i < m; i++)
|
||||
{
|
||||
A[i] = new decimal[n];
|
||||
for (int j = 0; j < n; j++)
|
||||
{
|
||||
A[i][j] = s;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public GeneralMatrix(decimal[][] A)
|
||||
{
|
||||
m = A.Length;
|
||||
n = A[0].Length;
|
||||
for (int i = 0; i < m; i++)
|
||||
{
|
||||
if (A[i].Length != n)
|
||||
{
|
||||
throw new System.ArgumentException("All rows must have the same length.");
|
||||
}
|
||||
}
|
||||
this.A = A;
|
||||
}
|
||||
|
||||
public GeneralMatrix(decimal[][] A, int m, int n)
|
||||
{
|
||||
this.A = A;
|
||||
this.m = m;
|
||||
this.n = n;
|
||||
}
|
||||
|
||||
public GeneralMatrix(decimal[] vals, int m)
|
||||
{
|
||||
this.m = m;
|
||||
n = (m != 0?vals.Length / m:0);
|
||||
if (m * n != vals.Length)
|
||||
{
|
||||
throw new System.ArgumentException("Array length must be a multiple of m.");
|
||||
}
|
||||
A = new decimal[m][];
|
||||
for (int i = 0; i < m; i++)
|
||||
{
|
||||
A[i] = new decimal[n];
|
||||
for (int j = 0; j < n; j++)
|
||||
{
|
||||
A[i][j] = vals[i + j * m];
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public virtual decimal[][] Array
|
||||
{
|
||||
get
|
||||
{
|
||||
return A;
|
||||
}
|
||||
}
|
||||
|
||||
public virtual decimal[][] ArrayCopy
|
||||
{
|
||||
get
|
||||
{
|
||||
decimal[][] C = new decimal[m][];
|
||||
for (int i = 0; i < m; i++)
|
||||
{
|
||||
C[i] = new decimal[n];
|
||||
for (int j = 0; j < n; j++)
|
||||
{
|
||||
C[i][j] = A[i][j];
|
||||
}
|
||||
}
|
||||
return C;
|
||||
}
|
||||
}
|
||||
|
||||
public virtual decimal[] ColumnPackedCopy
|
||||
{
|
||||
get
|
||||
{
|
||||
decimal[] vals = new decimal[m * n];
|
||||
for (int i = 0; i < m; i++)
|
||||
{
|
||||
for (int j = 0; j < n; j++)
|
||||
{
|
||||
vals[i + j * m] = A[i][j];
|
||||
}
|
||||
}
|
||||
return vals;
|
||||
}
|
||||
}
|
||||
|
||||
public virtual decimal[] RowPackedCopy
|
||||
{
|
||||
get
|
||||
{
|
||||
decimal[] vals = new decimal[m * n];
|
||||
for (int i = 0; i < m; i++)
|
||||
{
|
||||
for (int j = 0; j < n; j++)
|
||||
{
|
||||
vals[i * n + j] = A[i][j];
|
||||
}
|
||||
}
|
||||
return vals;
|
||||
}
|
||||
}
|
||||
|
||||
public virtual int RowDimension
|
||||
{
|
||||
get
|
||||
{
|
||||
return m;
|
||||
}
|
||||
}
|
||||
|
||||
public virtual int ColumnDimension
|
||||
{
|
||||
get
|
||||
{
|
||||
return n;
|
||||
}
|
||||
}
|
||||
|
||||
public static GeneralMatrix ConstructWithCopy(decimal[][] A)
|
||||
{
|
||||
int m = A.Length;
|
||||
int n = A[0].Length;
|
||||
GeneralMatrix X = new GeneralMatrix(m, n);
|
||||
decimal[][] C = X.Array;
|
||||
for (int i = 0; i < m; i++)
|
||||
{
|
||||
if (A[i].Length != n)
|
||||
{
|
||||
throw new System.ArgumentException("All rows must have the same length.");
|
||||
}
|
||||
for (int j = 0; j < n; j++)
|
||||
{
|
||||
C[i][j] = A[i][j];
|
||||
}
|
||||
}
|
||||
return X;
|
||||
}
|
||||
|
||||
public virtual GeneralMatrix Copy()
|
||||
{
|
||||
GeneralMatrix X = new GeneralMatrix(m, n);
|
||||
decimal[][] C = X.Array;
|
||||
for (int i = 0; i < m; i++)
|
||||
{
|
||||
for (int j = 0; j < n; j++)
|
||||
{
|
||||
C[i][j] = A[i][j];
|
||||
}
|
||||
}
|
||||
return X;
|
||||
}
|
||||
|
||||
public virtual decimal GetElement(int i, int j)
|
||||
{
|
||||
return A[i][j];
|
||||
}
|
||||
|
||||
public virtual GeneralMatrix GetMatrix(int i0, int i1, int j0, int j1)
|
||||
{
|
||||
GeneralMatrix X = new GeneralMatrix(i1 - i0 + 1, j1 - j0 + 1);
|
||||
decimal[][] B = X.Array;
|
||||
try
|
||||
{
|
||||
for (int i = i0; i <= i1; i++)
|
||||
{
|
||||
for (int j = j0; j <= j1; j++)
|
||||
{
|
||||
B[i - i0][j - j0] = A[i][j];
|
||||
}
|
||||
}
|
||||
}
|
||||
catch (System.IndexOutOfRangeException e)
|
||||
{
|
||||
throw new System.IndexOutOfRangeException("Submatrix indices", e);
|
||||
}
|
||||
return X;
|
||||
}
|
||||
|
||||
public virtual GeneralMatrix GetMatrix(int[] r, int[] c)
|
||||
{
|
||||
GeneralMatrix X = new GeneralMatrix(r.Length, c.Length);
|
||||
decimal[][] B = X.Array;
|
||||
try
|
||||
{
|
||||
for (int i = 0; i < r.Length; i++)
|
||||
{
|
||||
for (int j = 0; j < c.Length; j++)
|
||||
{
|
||||
B[i][j] = A[r[i]][c[j]];
|
||||
}
|
||||
}
|
||||
}
|
||||
catch (System.IndexOutOfRangeException e)
|
||||
{
|
||||
throw new System.IndexOutOfRangeException("Submatrix indices", e);
|
||||
}
|
||||
return X;
|
||||
}
|
||||
|
||||
public virtual GeneralMatrix GetMatrix(int i0, int i1, int[] c)
|
||||
{
|
||||
GeneralMatrix X = new GeneralMatrix(i1 - i0 + 1, c.Length);
|
||||
decimal[][] B = X.Array;
|
||||
try
|
||||
{
|
||||
for (int i = i0; i <= i1; i++)
|
||||
{
|
||||
for (int j = 0; j < c.Length; j++)
|
||||
{
|
||||
B[i - i0][j] = A[i][c[j]];
|
||||
}
|
||||
}
|
||||
}
|
||||
catch (System.IndexOutOfRangeException e)
|
||||
{
|
||||
throw new System.IndexOutOfRangeException("Submatrix indices", e);
|
||||
}
|
||||
return X;
|
||||
}
|
||||
|
||||
public virtual GeneralMatrix GetMatrix(int[] r, int j0, int j1)
|
||||
{
|
||||
GeneralMatrix X = new GeneralMatrix(r.Length, j1 - j0 + 1);
|
||||
decimal[][] B = X.Array;
|
||||
try
|
||||
{
|
||||
for (int i = 0; i < r.Length; i++)
|
||||
{
|
||||
for (int j = j0; j <= j1; j++)
|
||||
{
|
||||
B[i][j - j0] = A[r[i]][j];
|
||||
}
|
||||
}
|
||||
}
|
||||
catch (System.IndexOutOfRangeException e)
|
||||
{
|
||||
throw new System.IndexOutOfRangeException("Submatrix indices", e);
|
||||
}
|
||||
return X;
|
||||
}
|
||||
|
||||
public virtual void SetElement(int i, int j, decimal s)
|
||||
{
|
||||
A[i][j] = s;
|
||||
}
|
||||
|
||||
public virtual void SetMatrix(int i0, int i1, int j0, int j1, GeneralMatrix X)
|
||||
{
|
||||
try
|
||||
{
|
||||
for (int i = i0; i <= i1; i++)
|
||||
{
|
||||
for (int j = j0; j <= j1; j++)
|
||||
{
|
||||
A[i][j] = X.GetElement(i - i0, j - j0);
|
||||
}
|
||||
}
|
||||
}
|
||||
catch (System.IndexOutOfRangeException e)
|
||||
{
|
||||
throw new System.IndexOutOfRangeException("Submatrix indices", e);
|
||||
}
|
||||
}
|
||||
|
||||
public virtual void SetMatrix(int[] r, int[] c, GeneralMatrix X)
|
||||
{
|
||||
try
|
||||
{
|
||||
for (int i = 0; i < r.Length; i++)
|
||||
{
|
||||
for (int j = 0; j < c.Length; j++)
|
||||
{
|
||||
A[r[i]][c[j]] = X.GetElement(i, j);
|
||||
}
|
||||
}
|
||||
}
|
||||
catch (System.IndexOutOfRangeException e)
|
||||
{
|
||||
throw new System.IndexOutOfRangeException("Submatrix indices", e);
|
||||
}
|
||||
}
|
||||
|
||||
public virtual void SetMatrix(int[] r, int j0, int j1, GeneralMatrix X)
|
||||
{
|
||||
try
|
||||
{
|
||||
for (int i = 0; i < r.Length; i++)
|
||||
{
|
||||
for (int j = j0; j <= j1; j++)
|
||||
{
|
||||
A[r[i]][j] = X.GetElement(i, j - j0);
|
||||
}
|
||||
}
|
||||
}
|
||||
catch (System.IndexOutOfRangeException e)
|
||||
{
|
||||
throw new System.IndexOutOfRangeException("Submatrix indices", e);
|
||||
}
|
||||
}
|
||||
|
||||
public virtual void SetMatrix(int i0, int i1, int[] c, GeneralMatrix X)
|
||||
{
|
||||
try
|
||||
{
|
||||
for (int i = i0; i <= i1; i++)
|
||||
{
|
||||
for (int j = 0; j < c.Length; j++)
|
||||
{
|
||||
A[i][c[j]] = X.GetElement(i - i0, j);
|
||||
}
|
||||
}
|
||||
}
|
||||
catch (System.IndexOutOfRangeException e)
|
||||
{
|
||||
throw new System.IndexOutOfRangeException("Submatrix indices", e);
|
||||
}
|
||||
}
|
||||
|
||||
public virtual GeneralMatrix Transpose()
|
||||
{
|
||||
GeneralMatrix X = new GeneralMatrix(n, m);
|
||||
decimal[][] C = X.Array;
|
||||
for (int i = 0; i < m; i++)
|
||||
{
|
||||
for (int j = 0; j < n; j++)
|
||||
{
|
||||
C[j][i] = A[i][j];
|
||||
}
|
||||
}
|
||||
return X;
|
||||
}
|
||||
|
||||
public virtual decimal Norm1()
|
||||
{
|
||||
decimal f = 0;
|
||||
for (int j = 0; j < n; j++)
|
||||
{
|
||||
decimal s = 0;
|
||||
for (int i = 0; i < m; i++)
|
||||
{
|
||||
s += System.Math.Abs(A[i][j]);
|
||||
}
|
||||
f = System.Math.Max(f, s);
|
||||
}
|
||||
return f;
|
||||
}
|
||||
|
||||
public virtual decimal NormInf()
|
||||
{
|
||||
decimal f = 0;
|
||||
for (int i = 0; i < m; i++)
|
||||
{
|
||||
decimal s = 0;
|
||||
for (int j = 0; j < n; j++)
|
||||
{
|
||||
s += System.Math.Abs(A[i][j]);
|
||||
}
|
||||
f = System.Math.Max(f, s);
|
||||
}
|
||||
return f;
|
||||
}
|
||||
|
||||
public virtual decimal NormF()
|
||||
{
|
||||
decimal f = 0;
|
||||
for (int i = 0; i < m; i++)
|
||||
{
|
||||
for (int j = 0; j < n; j++)
|
||||
{
|
||||
f = Maths.Hypot(f, A[i][j]);
|
||||
}
|
||||
}
|
||||
return f;
|
||||
}
|
||||
|
||||
public virtual GeneralMatrix UnaryMinus()
|
||||
{
|
||||
GeneralMatrix X = new GeneralMatrix(m, n);
|
||||
decimal[][] C = X.Array;
|
||||
for (int i = 0; i < m; i++)
|
||||
{
|
||||
for (int j = 0; j < n; j++)
|
||||
{
|
||||
C[i][j] = - A[i][j];
|
||||
}
|
||||
}
|
||||
return X;
|
||||
}
|
||||
|
||||
public virtual GeneralMatrix Add(GeneralMatrix B)
|
||||
{
|
||||
CheckMatrixDimensions(B);
|
||||
GeneralMatrix X = new GeneralMatrix(m, n);
|
||||
decimal[][] C = X.Array;
|
||||
for (int i = 0; i < m; i++)
|
||||
{
|
||||
for (int j = 0; j < n; j++)
|
||||
{
|
||||
C[i][j] = A[i][j] + B.A[i][j];
|
||||
}
|
||||
}
|
||||
return X;
|
||||
}
|
||||
|
||||
public virtual GeneralMatrix AddEquals(GeneralMatrix B)
|
||||
{
|
||||
CheckMatrixDimensions(B);
|
||||
for (int i = 0; i < m; i++)
|
||||
{
|
||||
for (int j = 0; j < n; j++)
|
||||
{
|
||||
A[i][j] = A[i][j] + B.A[i][j];
|
||||
}
|
||||
}
|
||||
return this;
|
||||
}
|
||||
|
||||
public virtual GeneralMatrix Subtract(GeneralMatrix B)
|
||||
{
|
||||
CheckMatrixDimensions(B);
|
||||
GeneralMatrix X = new GeneralMatrix(m, n);
|
||||
decimal[][] C = X.Array;
|
||||
for (int i = 0; i < m; i++)
|
||||
{
|
||||
for (int j = 0; j < n; j++)
|
||||
{
|
||||
C[i][j] = A[i][j] - B.A[i][j];
|
||||
}
|
||||
}
|
||||
return X;
|
||||
}
|
||||
|
||||
public virtual GeneralMatrix SubtractEquals(GeneralMatrix B)
|
||||
{
|
||||
CheckMatrixDimensions(B);
|
||||
for (int i = 0; i < m; i++)
|
||||
{
|
||||
for (int j = 0; j < n; j++)
|
||||
{
|
||||
A[i][j] = A[i][j] - B.A[i][j];
|
||||
}
|
||||
}
|
||||
return this;
|
||||
}
|
||||
|
||||
public virtual GeneralMatrix ArrayMultiply(GeneralMatrix B)
|
||||
{
|
||||
CheckMatrixDimensions(B);
|
||||
GeneralMatrix X = new GeneralMatrix(m, n);
|
||||
decimal[][] C = X.Array;
|
||||
for (int i = 0; i < m; i++)
|
||||
{
|
||||
for (int j = 0; j < n; j++)
|
||||
{
|
||||
C[i][j] = A[i][j] * B.A[i][j];
|
||||
}
|
||||
}
|
||||
return X;
|
||||
}
|
||||
|
||||
public virtual GeneralMatrix ArrayMultiplyEquals(GeneralMatrix B)
|
||||
{
|
||||
CheckMatrixDimensions(B);
|
||||
for (int i = 0; i < m; i++)
|
||||
{
|
||||
for (int j = 0; j < n; j++)
|
||||
{
|
||||
A[i][j] = A[i][j] * B.A[i][j];
|
||||
}
|
||||
}
|
||||
return this;
|
||||
}
|
||||
|
||||
public virtual GeneralMatrix ArrayRightDivide(GeneralMatrix B)
|
||||
{
|
||||
CheckMatrixDimensions(B);
|
||||
GeneralMatrix X = new GeneralMatrix(m, n);
|
||||
decimal[][] C = X.Array;
|
||||
for (int i = 0; i < m; i++)
|
||||
{
|
||||
for (int j = 0; j < n; j++)
|
||||
{
|
||||
C[i][j] = A[i][j] / B.A[i][j];
|
||||
}
|
||||
}
|
||||
return X;
|
||||
}
|
||||
|
||||
public virtual GeneralMatrix ArrayRightDivideEquals(GeneralMatrix B)
|
||||
{
|
||||
CheckMatrixDimensions(B);
|
||||
for (int i = 0; i < m; i++)
|
||||
{
|
||||
for (int j = 0; j < n; j++)
|
||||
{
|
||||
A[i][j] = A[i][j] / B.A[i][j];
|
||||
}
|
||||
}
|
||||
return this;
|
||||
}
|
||||
|
||||
public virtual GeneralMatrix ArrayLeftDivide(GeneralMatrix B)
|
||||
{
|
||||
CheckMatrixDimensions(B);
|
||||
GeneralMatrix X = new GeneralMatrix(m, n);
|
||||
decimal[][] C = X.Array;
|
||||
for (int i = 0; i < m; i++)
|
||||
{
|
||||
for (int j = 0; j < n; j++)
|
||||
{
|
||||
C[i][j] = B.A[i][j] / A[i][j];
|
||||
}
|
||||
}
|
||||
return X;
|
||||
}
|
||||
|
||||
public virtual GeneralMatrix ArrayLeftDivideEquals(GeneralMatrix B)
|
||||
{
|
||||
CheckMatrixDimensions(B);
|
||||
for (int i = 0; i < m; i++)
|
||||
{
|
||||
for (int j = 0; j < n; j++)
|
||||
{
|
||||
A[i][j] = B.A[i][j] / A[i][j];
|
||||
}
|
||||
}
|
||||
return this;
|
||||
}
|
||||
|
||||
public virtual GeneralMatrix Multiply(decimal s)
|
||||
{
|
||||
GeneralMatrix X = new GeneralMatrix(m, n);
|
||||
decimal[][] C = X.Array;
|
||||
for (int i = 0; i < m; i++)
|
||||
{
|
||||
for (int j = 0; j < n; j++)
|
||||
{
|
||||
C[i][j] = s * A[i][j];
|
||||
}
|
||||
}
|
||||
return X;
|
||||
}
|
||||
|
||||
public virtual GeneralMatrix MultiplyEquals(decimal s)
|
||||
{
|
||||
for (int i = 0; i < m; i++)
|
||||
{
|
||||
for (int j = 0; j < n; j++)
|
||||
{
|
||||
A[i][j] = s * A[i][j];
|
||||
}
|
||||
}
|
||||
return this;
|
||||
}
|
||||
|
||||
public virtual GeneralMatrix Multiply(GeneralMatrix B)
|
||||
{
|
||||
if (B.m != n)
|
||||
{
|
||||
throw new System.ArgumentException("GeneralMatrix inner dimensions must agree.");
|
||||
}
|
||||
GeneralMatrix X = new GeneralMatrix(m, B.n);
|
||||
decimal[][] C = X.Array;
|
||||
decimal[] Bcolj = new decimal[n];
|
||||
for (int j = 0; j < B.n; j++)
|
||||
{
|
||||
for (int k = 0; k < n; k++)
|
||||
{
|
||||
Bcolj[k] = B.A[k][j];
|
||||
}
|
||||
for (int i = 0; i < m; i++)
|
||||
{
|
||||
decimal[] Arowi = A[i];
|
||||
decimal s = 0;
|
||||
for (int k = 0; k < n; k++)
|
||||
{
|
||||
s += Arowi[k] * Bcolj[k];
|
||||
}
|
||||
C[i][j] = s;
|
||||
}
|
||||
}
|
||||
return X;
|
||||
}
|
||||
|
||||
public static GeneralMatrix operator +(GeneralMatrix m1, GeneralMatrix m2)
|
||||
{
|
||||
return m1.Add(m2);
|
||||
}
|
||||
|
||||
public static GeneralMatrix operator -(GeneralMatrix m1, GeneralMatrix m2)
|
||||
{
|
||||
return m1.Subtract(m2);
|
||||
}
|
||||
|
||||
public static GeneralMatrix operator *(GeneralMatrix m1, GeneralMatrix m2)
|
||||
{
|
||||
return m1.Multiply(m2);
|
||||
}
|
||||
|
||||
public virtual LUDecomposition LUD()
|
||||
{
|
||||
return new LUDecomposition(this);
|
||||
}
|
||||
|
||||
public virtual QRDecomposition QRD()
|
||||
{
|
||||
return new QRDecomposition(this);
|
||||
}
|
||||
|
||||
public virtual CholeskyDecomposition chol()
|
||||
{
|
||||
return new CholeskyDecomposition(this);
|
||||
}
|
||||
|
||||
public virtual SingularValueDecomposition SVD()
|
||||
{
|
||||
return new SingularValueDecomposition(this);
|
||||
}
|
||||
|
||||
public virtual EigenvalueDecomposition Eigen()
|
||||
{
|
||||
return new EigenvalueDecomposition(this);
|
||||
}
|
||||
|
||||
public virtual GeneralMatrix Solve(GeneralMatrix B)
|
||||
{
|
||||
return (m == n ? (new LUDecomposition(this)).Solve(B):(new QRDecomposition(this)).Solve(B));
|
||||
}
|
||||
|
||||
public virtual GeneralMatrix SolveTranspose(GeneralMatrix B)
|
||||
{
|
||||
return Transpose().Solve(B.Transpose());
|
||||
}
|
||||
|
||||
public virtual GeneralMatrix Inverse()
|
||||
{
|
||||
return Solve(Identity(m, m));
|
||||
}
|
||||
|
||||
public virtual decimal Determinant()
|
||||
{
|
||||
return new LUDecomposition(this).Determinant();
|
||||
}
|
||||
|
||||
public virtual int Rank()
|
||||
{
|
||||
return new SingularValueDecomposition(this).Rank();
|
||||
}
|
||||
|
||||
public virtual decimal Condition()
|
||||
{
|
||||
return new SingularValueDecomposition(this).Condition();
|
||||
}
|
||||
|
||||
public virtual decimal Trace()
|
||||
{
|
||||
decimal t = 0;
|
||||
for (int i = 0; i < System.Math.Min(m, n); i++)
|
||||
{
|
||||
t += A[i][i];
|
||||
}
|
||||
return t;
|
||||
}
|
||||
|
||||
public static GeneralMatrix Identity(int m, int n)
|
||||
{
|
||||
GeneralMatrix A = new GeneralMatrix(m, n);
|
||||
decimal[][] X = A.Array;
|
||||
for (int i = 0; i < m; i++)
|
||||
{
|
||||
for (int j = 0; j < n; j++)
|
||||
{
|
||||
X[i][j] = (i == j ? 1.0m : 0.0m);
|
||||
}
|
||||
}
|
||||
return A;
|
||||
}
|
||||
|
||||
private void CheckMatrixDimensions(GeneralMatrix B)
|
||||
{
|
||||
if (B.m != m || B.n != n)
|
||||
{
|
||||
throw new System.ArgumentException("GeneralMatrix dimensions must agree.");
|
||||
}
|
||||
}
|
||||
|
||||
public void Dispose()
|
||||
{
|
||||
Dispose(true);
|
||||
}
|
||||
|
||||
private void Dispose(bool disposing)
|
||||
{
|
||||
if (disposing)
|
||||
GC.SuppressFinalize(this);
|
||||
}
|
||||
|
||||
~GeneralMatrix()
|
||||
{
|
||||
Dispose(false);
|
||||
}
|
||||
|
||||
public System.Object Clone()
|
||||
{
|
||||
return this.Copy();
|
||||
}
|
||||
|
||||
void ISerializable.GetObjectData(SerializationInfo info, StreamingContext context)
|
||||
{
|
||||
}
|
||||
}
|
||||
}
|
||||
322
MathApp/MathApp/Core/Matrix/LUDecomposition.cs
Normal file
322
MathApp/MathApp/Core/Matrix/LUDecomposition.cs
Normal file
|
|
@ -0,0 +1,322 @@
|
|||
using System;
|
||||
using System.Runtime.Serialization;
|
||||
|
||||
namespace DotNetMatrix
|
||||
{
|
||||
|
||||
/// <summary>LU Decomposition.
|
||||
/// For an m-by-n matrix A with m >= n, the LU decomposition is an m-by-n
|
||||
/// unit lower triangular matrix L, an n-by-n upper triangular matrix U,
|
||||
/// and a permutation vector piv of length m so that A(piv,:) = L*U.
|
||||
/// <code> If m < n, then L is m-by-m and U is m-by-n. </code>
|
||||
/// The LU decompostion with pivoting always exists, even if the matrix is
|
||||
/// singular, so the constructor will never fail. The primary use of the
|
||||
/// LU decomposition is in the solution of square systems of simultaneous
|
||||
/// linear equations. This will fail if IsNonSingular() returns false.
|
||||
/// </summary>
|
||||
|
||||
[Serializable]
|
||||
public class LUDecomposition : System.Runtime.Serialization.ISerializable
|
||||
{
|
||||
#region Class variables
|
||||
|
||||
/// <summary>Array for internal storage of decomposition.
|
||||
/// @serial internal array storage.
|
||||
/// </summary>
|
||||
private decimal[][] LU;
|
||||
|
||||
/// <summary>Row and column dimensions, and pivot sign.
|
||||
/// @serial column dimension.
|
||||
/// @serial row dimension.
|
||||
/// @serial pivot sign.
|
||||
/// </summary>
|
||||
private int m, n, pivsign;
|
||||
|
||||
/// <summary>Internal storage of pivot vector.
|
||||
/// @serial pivot vector.
|
||||
/// </summary>
|
||||
private int[] piv;
|
||||
|
||||
#endregion // Class variables
|
||||
|
||||
#region Constructor
|
||||
|
||||
/// <summary>LU Decomposition</summary>
|
||||
/// <param name="A"> Rectangular matrix
|
||||
/// </param>
|
||||
/// <returns> Structure to access L, U and piv.
|
||||
/// </returns>
|
||||
|
||||
public LUDecomposition(GeneralMatrix A)
|
||||
{
|
||||
// Use a "left-looking", dot-product, Crout/Doolittle algorithm.
|
||||
|
||||
LU = A.ArrayCopy;
|
||||
m = A.RowDimension;
|
||||
n = A.ColumnDimension;
|
||||
piv = new int[m];
|
||||
for (int i = 0; i < m; i++)
|
||||
{
|
||||
piv[i] = i;
|
||||
}
|
||||
pivsign = 1;
|
||||
decimal[] LUrowi;
|
||||
decimal[] LUcolj = new decimal[m];
|
||||
|
||||
// Outer loop.
|
||||
|
||||
for (int j = 0; j < n; j++)
|
||||
{
|
||||
|
||||
// Make a copy of the j-th column to localize references.
|
||||
|
||||
for (int i = 0; i < m; i++)
|
||||
{
|
||||
LUcolj[i] = LU[i][j];
|
||||
}
|
||||
|
||||
// Apply previous transformations.
|
||||
|
||||
for (int i = 0; i < m; i++)
|
||||
{
|
||||
LUrowi = LU[i];
|
||||
|
||||
// Most of the time is spent in the following dot product.
|
||||
|
||||
int kmax = System.Math.Min(i, j);
|
||||
decimal s = 0.0m;
|
||||
for (int k = 0; k < kmax; k++)
|
||||
{
|
||||
s += LUrowi[k] * LUcolj[k];
|
||||
}
|
||||
|
||||
LUrowi[j] = LUcolj[i] -= s;
|
||||
}
|
||||
|
||||
// Find pivot and exchange if necessary.
|
||||
|
||||
int p = j;
|
||||
for (int i = j + 1; i < m; i++)
|
||||
{
|
||||
if (System.Math.Abs(LUcolj[i]) > System.Math.Abs(LUcolj[p]))
|
||||
{
|
||||
p = i;
|
||||
}
|
||||
}
|
||||
if (p != j)
|
||||
{
|
||||
for (int k = 0; k < n; k++)
|
||||
{
|
||||
decimal t = LU[p][k]; LU[p][k] = LU[j][k]; LU[j][k] = t;
|
||||
}
|
||||
int k2 = piv[p]; piv[p] = piv[j]; piv[j] = k2;
|
||||
pivsign = - pivsign;
|
||||
}
|
||||
|
||||
// Compute multipliers.
|
||||
|
||||
if (j < m & LU[j][j] != 0.0m)
|
||||
{
|
||||
for (int i = j + 1; i < m; i++)
|
||||
{
|
||||
LU[i][j] /= LU[j][j];
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
#endregion // Constructor
|
||||
|
||||
#region Public Properties
|
||||
/// <summary>Is the matrix nonsingular?</summary>
|
||||
/// <returns> true if U, and hence A, is nonsingular.
|
||||
/// </returns>
|
||||
virtual public bool IsNonSingular
|
||||
{
|
||||
get
|
||||
{
|
||||
for (int j = 0; j < n; j++)
|
||||
{
|
||||
if (LU[j][j] == 0)
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>Return lower triangular factor</summary>
|
||||
/// <returns> L
|
||||
/// </returns>
|
||||
virtual public GeneralMatrix L
|
||||
{
|
||||
get
|
||||
{
|
||||
GeneralMatrix X = new GeneralMatrix(m, n);
|
||||
decimal[][] L = X.Array;
|
||||
for (int i = 0; i < m; i++)
|
||||
{
|
||||
for (int j = 0; j < n; j++)
|
||||
{
|
||||
if (i > j)
|
||||
{
|
||||
L[i][j] = LU[i][j];
|
||||
}
|
||||
else if (i == j)
|
||||
{
|
||||
L[i][j] = 1.0m;
|
||||
}
|
||||
else
|
||||
{
|
||||
L[i][j] = 0.0m;
|
||||
}
|
||||
}
|
||||
}
|
||||
return X;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>Return upper triangular factor</summary>
|
||||
/// <returns> U
|
||||
/// </returns>
|
||||
virtual public GeneralMatrix U
|
||||
{
|
||||
get
|
||||
{
|
||||
GeneralMatrix X = new GeneralMatrix(n, n);
|
||||
decimal[][] U = X.Array;
|
||||
for (int i = 0; i < n; i++)
|
||||
{
|
||||
for (int j = 0; j < n; j++)
|
||||
{
|
||||
if (i <= j)
|
||||
{
|
||||
U[i][j] = LU[i][j];
|
||||
}
|
||||
else
|
||||
{
|
||||
U[i][j] = 0.0m;
|
||||
}
|
||||
}
|
||||
}
|
||||
return X;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>Return pivot permutation vector</summary>
|
||||
/// <returns> piv
|
||||
/// </returns>
|
||||
virtual public int[] Pivot
|
||||
{
|
||||
get
|
||||
{
|
||||
int[] p = new int[m];
|
||||
for (int i = 0; i < m; i++)
|
||||
{
|
||||
p[i] = piv[i];
|
||||
}
|
||||
return p;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>Return pivot permutation vector as a one-dimensional decimal array</summary>
|
||||
/// <returns> (decimal) piv
|
||||
/// </returns>
|
||||
virtual public decimal[] decimalPivot
|
||||
{
|
||||
get
|
||||
{
|
||||
decimal[] vals = new decimal[m];
|
||||
for (int i = 0; i < m; i++)
|
||||
{
|
||||
vals[i] = (decimal) piv[i];
|
||||
}
|
||||
return vals;
|
||||
}
|
||||
}
|
||||
|
||||
#endregion // Public Properties
|
||||
|
||||
#region Public Methods
|
||||
|
||||
/// <summary>Determinant</summary>
|
||||
/// <returns> det(A)
|
||||
/// </returns>
|
||||
/// <exception cref="System.ArgumentException"> Matrix must be square
|
||||
/// </exception>
|
||||
|
||||
public virtual decimal Determinant()
|
||||
{
|
||||
if (m != n)
|
||||
{
|
||||
throw new System.ArgumentException("Matrix must be square.");
|
||||
}
|
||||
decimal d = (decimal) pivsign;
|
||||
for (int j = 0; j < n; j++)
|
||||
{
|
||||
d *= LU[j][j];
|
||||
}
|
||||
return d;
|
||||
}
|
||||
|
||||
/// <summary>Solve A*X = B</summary>
|
||||
/// <param name="B"> A Matrix with as many rows as A and any number of columns.
|
||||
/// </param>
|
||||
/// <returns> X so that L*U*X = B(piv,:)
|
||||
/// </returns>
|
||||
/// <exception cref="System.ArgumentException"> Matrix row dimensions must agree.
|
||||
/// </exception>
|
||||
/// <exception cref="System.SystemException"> Matrix is singular.
|
||||
/// </exception>
|
||||
|
||||
public virtual GeneralMatrix Solve(GeneralMatrix B)
|
||||
{
|
||||
if (B.RowDimension != m)
|
||||
{
|
||||
throw new System.ArgumentException("Matrix row dimensions must agree.");
|
||||
}
|
||||
if (!this.IsNonSingular)
|
||||
{
|
||||
throw new System.SystemException("Matrix is singular.");
|
||||
}
|
||||
|
||||
// Copy right hand side with pivoting
|
||||
int nx = B.ColumnDimension;
|
||||
GeneralMatrix Xmat = B.GetMatrix(piv, 0, nx - 1);
|
||||
decimal[][] X = Xmat.Array;
|
||||
|
||||
// Solve L*Y = B(piv,:)
|
||||
for (int k = 0; k < n; k++)
|
||||
{
|
||||
for (int i = k + 1; i < n; i++)
|
||||
{
|
||||
for (int j = 0; j < nx; j++)
|
||||
{
|
||||
X[i][j] -= X[k][j] * LU[i][k];
|
||||
}
|
||||
}
|
||||
}
|
||||
// Solve U*X = Y;
|
||||
for (int k = n - 1; k >= 0; k--)
|
||||
{
|
||||
for (int j = 0; j < nx; j++)
|
||||
{
|
||||
X[k][j] /= LU[k][k];
|
||||
}
|
||||
for (int i = 0; i < k; i++)
|
||||
{
|
||||
for (int j = 0; j < nx; j++)
|
||||
{
|
||||
X[i][j] -= X[k][j] * LU[i][k];
|
||||
}
|
||||
}
|
||||
}
|
||||
return Xmat;
|
||||
}
|
||||
|
||||
#endregion // Public Methods
|
||||
|
||||
// A method called when serializing this class.
|
||||
void ISerializable.GetObjectData(SerializationInfo info, StreamingContext context)
|
||||
{
|
||||
}
|
||||
}
|
||||
}
|
||||
287
MathApp/MathApp/Core/Matrix/QRDecomposition.cs
Normal file
287
MathApp/MathApp/Core/Matrix/QRDecomposition.cs
Normal file
|
|
@ -0,0 +1,287 @@
|
|||
using System;
|
||||
using System.Runtime.Serialization;
|
||||
|
||||
namespace DotNetMatrix
|
||||
{
|
||||
|
||||
/// <summary>QR Decomposition.
|
||||
/// For an m-by-n matrix A with m >= n, the QR decomposition is an m-by-n
|
||||
/// orthogonal matrix Q and an n-by-n upper triangular matrix R so that
|
||||
/// A = Q*R.
|
||||
///
|
||||
/// The QR decompostion always exists, even if the matrix does not have
|
||||
/// full rank, so the constructor will never fail. The primary use of the
|
||||
/// QR decomposition is in the least squares solution of nonsquare systems
|
||||
/// of simultaneous linear equations. This will fail if IsFullRank()
|
||||
/// returns false.
|
||||
/// </summary>
|
||||
|
||||
[Serializable]
|
||||
public class QRDecomposition : System.Runtime.Serialization.ISerializable
|
||||
{
|
||||
#region Class variables
|
||||
|
||||
/// <summary>Array for internal storage of decomposition.
|
||||
/// @serial internal array storage.
|
||||
/// </summary>
|
||||
private decimal[][] QR;
|
||||
|
||||
/// <summary>Row and column dimensions.
|
||||
/// @serial column dimension.
|
||||
/// @serial row dimension.
|
||||
/// </summary>
|
||||
private int m, n;
|
||||
|
||||
/// <summary>Array for internal storage of diagonal of R.
|
||||
/// @serial diagonal of R.
|
||||
/// </summary>
|
||||
private decimal[] Rdiag;
|
||||
|
||||
#endregion // Class variables
|
||||
|
||||
#region Constructor
|
||||
|
||||
/// <summary>QR Decomposition, computed by Householder reflections.</summary>
|
||||
/// <param name="A"> Rectangular matrix
|
||||
/// </param>
|
||||
/// <returns> Structure to access R and the Householder vectors and compute Q.
|
||||
/// </returns>
|
||||
|
||||
public QRDecomposition(GeneralMatrix A)
|
||||
{
|
||||
// Initialize.
|
||||
QR = A.ArrayCopy;
|
||||
m = A.RowDimension;
|
||||
n = A.ColumnDimension;
|
||||
Rdiag = new decimal[n];
|
||||
|
||||
// Main loop.
|
||||
for (int k = 0; k < n; k++)
|
||||
{
|
||||
// Compute 2-norm of k-th column without under/overflow.
|
||||
decimal nrm = 0;
|
||||
for (int i = k; i < m; i++)
|
||||
{
|
||||
nrm = Maths.Hypot(nrm, QR[i][k]);
|
||||
}
|
||||
|
||||
if (nrm != 0.0m)
|
||||
{
|
||||
// Form k-th Householder vector.
|
||||
if (QR[k][k] < 0)
|
||||
{
|
||||
nrm = - nrm;
|
||||
}
|
||||
for (int i = k; i < m; i++)
|
||||
{
|
||||
QR[i][k] /= nrm;
|
||||
}
|
||||
QR[k][k] += 1.0m;
|
||||
|
||||
// Apply transformation to remaining columns.
|
||||
for (int j = k + 1; j < n; j++)
|
||||
{
|
||||
decimal s = 0.0m;
|
||||
for (int i = k; i < m; i++)
|
||||
{
|
||||
s += QR[i][k] * QR[i][j];
|
||||
}
|
||||
s = (- s) / QR[k][k];
|
||||
for (int i = k; i < m; i++)
|
||||
{
|
||||
QR[i][j] += s * QR[i][k];
|
||||
}
|
||||
}
|
||||
}
|
||||
Rdiag[k] = - nrm;
|
||||
}
|
||||
}
|
||||
|
||||
#endregion // Constructor
|
||||
|
||||
#region Public Properties
|
||||
|
||||
/// <summary>Is the matrix full rank?</summary>
|
||||
/// <returns> true if R, and hence A, has full rank.
|
||||
/// </returns>
|
||||
virtual public bool FullRank
|
||||
{
|
||||
get
|
||||
{
|
||||
for (int j = 0; j < n; j++)
|
||||
{
|
||||
if (Rdiag[j] == 0)
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>Return the Householder vectors</summary>
|
||||
/// <returns> Lower trapezoidal matrix whose columns define the reflections
|
||||
/// </returns>
|
||||
virtual public GeneralMatrix H
|
||||
{
|
||||
get
|
||||
{
|
||||
GeneralMatrix X = new GeneralMatrix(m, n);
|
||||
decimal[][] H = X.Array;
|
||||
for (int i = 0; i < m; i++)
|
||||
{
|
||||
for (int j = 0; j < n; j++)
|
||||
{
|
||||
if (i >= j)
|
||||
{
|
||||
H[i][j] = QR[i][j];
|
||||
}
|
||||
else
|
||||
{
|
||||
H[i][j] = 0.0m;
|
||||
}
|
||||
}
|
||||
}
|
||||
return X;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
/// <summary>Return the upper triangular factor</summary>
|
||||
/// <returns> R
|
||||
/// </returns>
|
||||
virtual public GeneralMatrix R
|
||||
{
|
||||
get
|
||||
{
|
||||
GeneralMatrix X = new GeneralMatrix(n, n);
|
||||
decimal[][] R = X.Array;
|
||||
for (int i = 0; i < n; i++)
|
||||
{
|
||||
for (int j = 0; j < n; j++)
|
||||
{
|
||||
if (i < j)
|
||||
{
|
||||
R[i][j] = QR[i][j];
|
||||
}
|
||||
else if (i == j)
|
||||
{
|
||||
R[i][j] = Rdiag[i];
|
||||
}
|
||||
else
|
||||
{
|
||||
R[i][j] = 0.0m;
|
||||
}
|
||||
}
|
||||
}
|
||||
return X;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>Generate and return the (economy-sized) orthogonal factor</summary>
|
||||
/// <returns> Q
|
||||
/// </returns>
|
||||
virtual public GeneralMatrix Q
|
||||
{
|
||||
get
|
||||
{
|
||||
GeneralMatrix X = new GeneralMatrix(m, n);
|
||||
decimal[][] Q = X.Array;
|
||||
for (int k = n - 1; k >= 0; k--)
|
||||
{
|
||||
for (int i = 0; i < m; i++)
|
||||
{
|
||||
Q[i][k] = 0.0m;
|
||||
}
|
||||
Q[k][k] = 1.0m;
|
||||
for (int j = k; j < n; j++)
|
||||
{
|
||||
if (QR[k][k] != 0)
|
||||
{
|
||||
decimal s = 0.0m;
|
||||
for (int i = k; i < m; i++)
|
||||
{
|
||||
s += QR[i][k] * Q[i][j];
|
||||
}
|
||||
s = (- s) / QR[k][k];
|
||||
for (int i = k; i < m; i++)
|
||||
{
|
||||
Q[i][j] += s * QR[i][k];
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
return X;
|
||||
}
|
||||
}
|
||||
#endregion // Public Properties
|
||||
|
||||
#region Public Methods
|
||||
|
||||
/// <summary>Least squares solution of A*X = B</summary>
|
||||
/// <param name="B"> A Matrix with as many rows as A and any number of columns.
|
||||
/// </param>
|
||||
/// <returns> X that minimizes the two norm of Q*R*X-B.
|
||||
/// </returns>
|
||||
/// <exception cref="System.ArgumentException"> Matrix row dimensions must agree.
|
||||
/// </exception>
|
||||
/// <exception cref="System.SystemException"> Matrix is rank deficient.
|
||||
/// </exception>
|
||||
|
||||
public virtual GeneralMatrix Solve(GeneralMatrix B)
|
||||
{
|
||||
if (B.RowDimension != m)
|
||||
{
|
||||
throw new System.ArgumentException("GeneralMatrix row dimensions must agree.");
|
||||
}
|
||||
if (!this.FullRank)
|
||||
{
|
||||
throw new System.SystemException("Matrix is rank deficient.");
|
||||
}
|
||||
|
||||
// Copy right hand side
|
||||
int nx = B.ColumnDimension;
|
||||
decimal[][] X = B.ArrayCopy;
|
||||
|
||||
// Compute Y = transpose(Q)*B
|
||||
for (int k = 0; k < n; k++)
|
||||
{
|
||||
for (int j = 0; j < nx; j++)
|
||||
{
|
||||
decimal s = 0.0m;
|
||||
for (int i = k; i < m; i++)
|
||||
{
|
||||
s += QR[i][k] * X[i][j];
|
||||
}
|
||||
s = (- s) / QR[k][k];
|
||||
for (int i = k; i < m; i++)
|
||||
{
|
||||
X[i][j] += s * QR[i][k];
|
||||
}
|
||||
}
|
||||
}
|
||||
// Solve R*X = Y;
|
||||
for (int k = n - 1; k >= 0; k--)
|
||||
{
|
||||
for (int j = 0; j < nx; j++)
|
||||
{
|
||||
X[k][j] /= Rdiag[k];
|
||||
}
|
||||
for (int i = 0; i < k; i++)
|
||||
{
|
||||
for (int j = 0; j < nx; j++)
|
||||
{
|
||||
X[i][j] -= X[k][j] * QR[i][k];
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return (new GeneralMatrix(X, n, nx).GetMatrix(0, n - 1, 0, nx - 1));
|
||||
}
|
||||
|
||||
#endregion // Public Methods
|
||||
|
||||
// A method called when serializing this class.
|
||||
void ISerializable.GetObjectData(SerializationInfo info, StreamingContext context)
|
||||
{
|
||||
}
|
||||
}
|
||||
}
|
||||
512
MathApp/MathApp/Core/Matrix/SingularValueDecomposition.cs
Normal file
512
MathApp/MathApp/Core/Matrix/SingularValueDecomposition.cs
Normal file
|
|
@ -0,0 +1,512 @@
|
|||
using System;
|
||||
using System.Runtime.Serialization;
|
||||
|
||||
namespace DotNetMatrix
|
||||
{
|
||||
[Serializable]
|
||||
public class SingularValueDecomposition : System.Runtime.Serialization.ISerializable
|
||||
{
|
||||
private decimal[][] U, V;
|
||||
private decimal[] s;
|
||||
private int m, n;
|
||||
|
||||
public SingularValueDecomposition(GeneralMatrix Arg)
|
||||
{
|
||||
decimal[][] A = Arg.ArrayCopy;
|
||||
m = Arg.RowDimension;
|
||||
n = Arg.ColumnDimension;
|
||||
int nu = System.Math.Min(m, n);
|
||||
s = new decimal[System.Math.Min(m + 1, n)];
|
||||
U = new decimal[m][];
|
||||
for (int i = 0; i < m; i++)
|
||||
{
|
||||
U[i] = new decimal[nu];
|
||||
}
|
||||
V = new decimal[n][];
|
||||
for (int i2 = 0; i2 < n; i2++)
|
||||
{
|
||||
V[i2] = new decimal[n];
|
||||
}
|
||||
decimal[] e = new decimal[n];
|
||||
decimal[] work = new decimal[m];
|
||||
bool wantu = true;
|
||||
bool wantv = true;
|
||||
|
||||
int nct = System.Math.Min(m - 1, n);
|
||||
int nrt = System.Math.Max(0, System.Math.Min(n - 2, m));
|
||||
for (int k = 0; k < System.Math.Max(nct, nrt); k++)
|
||||
{
|
||||
if (k < nct)
|
||||
{
|
||||
s[k] = 0;
|
||||
for (int i = k; i < m; i++)
|
||||
{
|
||||
s[k] = Maths.Hypot(s[k], A[i][k]);
|
||||
}
|
||||
if (s[k] != 0.0m)
|
||||
{
|
||||
if (A[k][k] < 0.0m)
|
||||
{
|
||||
s[k] = - s[k];
|
||||
}
|
||||
for (int i = k; i < m; i++)
|
||||
{
|
||||
A[i][k] /= s[k];
|
||||
}
|
||||
A[k][k] += 1.0m;
|
||||
}
|
||||
s[k] = - s[k];
|
||||
}
|
||||
for (int j = k + 1; j < n; j++)
|
||||
{
|
||||
if ((k < nct) & (s[k] != 0.0m))
|
||||
{
|
||||
decimal t = 0;
|
||||
for (int i = k; i < m; i++)
|
||||
{
|
||||
t += A[i][k] * A[i][j];
|
||||
}
|
||||
t = (- t) / A[k][k];
|
||||
for (int i = k; i < m; i++)
|
||||
{
|
||||
A[i][j] += t * A[i][k];
|
||||
}
|
||||
}
|
||||
e[j] = A[k][j];
|
||||
}
|
||||
if (wantu & (k < nct))
|
||||
{
|
||||
for (int i = k; i < m; i++)
|
||||
{
|
||||
U[i][k] = A[i][k];
|
||||
}
|
||||
}
|
||||
if (k < nrt)
|
||||
{
|
||||
e[k] = 0;
|
||||
for (int i = k + 1; i < n; i++)
|
||||
{
|
||||
e[k] = Maths.Hypot(e[k], e[i]);
|
||||
}
|
||||
if (e[k] != 0.0m)
|
||||
{
|
||||
if (e[k + 1] < 0.0m)
|
||||
{
|
||||
e[k] = - e[k];
|
||||
}
|
||||
for (int i = k + 1; i < n; i++)
|
||||
{
|
||||
e[i] /= e[k];
|
||||
}
|
||||
e[k + 1] += 1.0m;
|
||||
}
|
||||
e[k] = - e[k];
|
||||
if ((k + 1 < m) & (e[k] != 0.0m))
|
||||
{
|
||||
for (int i = k + 1; i < m; i++)
|
||||
{
|
||||
work[i] = 0.0m;
|
||||
}
|
||||
for (int j = k + 1; j < n; j++)
|
||||
{
|
||||
for (int i = k + 1; i < m; i++)
|
||||
{
|
||||
work[i] += e[j] * A[i][j];
|
||||
}
|
||||
}
|
||||
for (int j = k + 1; j < n; j++)
|
||||
{
|
||||
decimal t = (- e[j]) / e[k + 1];
|
||||
for (int i = k + 1; i < m; i++)
|
||||
{
|
||||
A[i][j] += t * work[i];
|
||||
}
|
||||
}
|
||||
}
|
||||
if (wantv)
|
||||
{
|
||||
for (int i = k + 1; i < n; i++)
|
||||
{
|
||||
V[i][k] = e[i];
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
int p = System.Math.Min(n, m + 1);
|
||||
if (nct < n)
|
||||
{
|
||||
s[nct] = A[nct][nct];
|
||||
}
|
||||
if (m < p)
|
||||
{
|
||||
s[p - 1] = 0.0m;
|
||||
}
|
||||
if (nrt + 1 < p)
|
||||
{
|
||||
e[nrt] = A[nrt][p - 1];
|
||||
}
|
||||
e[p - 1] = 0.0m;
|
||||
|
||||
if (wantu)
|
||||
{
|
||||
for (int j = nct; j < nu; j++)
|
||||
{
|
||||
for (int i = 0; i < m; i++)
|
||||
{
|
||||
U[i][j] = 0.0m;
|
||||
}
|
||||
U[j][j] = 1.0m;
|
||||
}
|
||||
for (int k = nct - 1; k >= 0; k--)
|
||||
{
|
||||
if (s[k] != 0.0m)
|
||||
{
|
||||
for (int j = k + 1; j < nu; j++)
|
||||
{
|
||||
decimal t = 0;
|
||||
for (int i = k; i < m; i++)
|
||||
{
|
||||
t += U[i][k] * U[i][j];
|
||||
}
|
||||
t = (- t) / U[k][k];
|
||||
for (int i = k; i < m; i++)
|
||||
{
|
||||
U[i][j] += t * U[i][k];
|
||||
}
|
||||
}
|
||||
for (int i = k; i < m; i++)
|
||||
{
|
||||
U[i][k] = - U[i][k];
|
||||
}
|
||||
U[k][k] = 1.0m + U[k][k];
|
||||
for (int i = 0; i < k - 1; i++)
|
||||
{
|
||||
U[i][k] = 0.0m;
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
for (int i = 0; i < m; i++)
|
||||
{
|
||||
U[i][k] = 0.0m;
|
||||
}
|
||||
U[k][k] = 1.0m;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (wantv)
|
||||
{
|
||||
for (int k = n - 1; k >= 0; k--)
|
||||
{
|
||||
if ((k < nrt) & (e[k] != 0.0m))
|
||||
{
|
||||
for (int j = k + 1; j < nu; j++)
|
||||
{
|
||||
decimal t = 0;
|
||||
for (int i = k + 1; i < n; i++)
|
||||
{
|
||||
t += V[i][k] * V[i][j];
|
||||
}
|
||||
t = (- t) / V[k + 1][k];
|
||||
for (int i = k + 1; i < n; i++)
|
||||
{
|
||||
V[i][j] += t * V[i][k];
|
||||
}
|
||||
}
|
||||
}
|
||||
for (int i = 0; i < n; i++)
|
||||
{
|
||||
V[i][k] = 0.0m;
|
||||
}
|
||||
V[k][k] = 1.0m;
|
||||
}
|
||||
}
|
||||
|
||||
int pp = p - 1;
|
||||
int iter = 0;
|
||||
decimal eps = (decimal)Math.Pow(2.0f, - 52.0f);
|
||||
while (p > 0)
|
||||
{
|
||||
int k, kase;
|
||||
for (k = p - 2; k >= - 1; k--)
|
||||
{
|
||||
if (k == - 1)
|
||||
{
|
||||
break;
|
||||
}
|
||||
if (System.Math.Abs(e[k]) <= eps * (System.Math.Abs(s[k]) + System.Math.Abs(s[k + 1])))
|
||||
{
|
||||
e[k] = 0.0m;
|
||||
break;
|
||||
}
|
||||
}
|
||||
if (k == p - 2)
|
||||
{
|
||||
kase = 4;
|
||||
}
|
||||
else
|
||||
{
|
||||
int ks;
|
||||
for (ks = p - 1; ks >= k; ks--)
|
||||
{
|
||||
if (ks == k)
|
||||
{
|
||||
break;
|
||||
}
|
||||
decimal t = (ks != p?System.Math.Abs(e[ks]):0.0m) + (ks != k + 1?System.Math.Abs(e[ks - 1]):0.0m);
|
||||
if (System.Math.Abs(s[ks]) <= eps * t)
|
||||
{
|
||||
s[ks] = 0.0m;
|
||||
break;
|
||||
}
|
||||
}
|
||||
if (ks == k)
|
||||
{
|
||||
kase = 3;
|
||||
}
|
||||
else if (ks == p - 1)
|
||||
{
|
||||
kase = 1;
|
||||
}
|
||||
else
|
||||
{
|
||||
kase = 2;
|
||||
k = ks;
|
||||
}
|
||||
}
|
||||
k++;
|
||||
|
||||
switch (kase)
|
||||
{
|
||||
case 1:
|
||||
{
|
||||
decimal f = e[p - 2];
|
||||
e[p - 2] = 0.0m;
|
||||
for (int j = p - 2; j >= k; j--)
|
||||
{
|
||||
decimal t = Maths.Hypot(s[j], f);
|
||||
decimal cs = s[j] / t;
|
||||
decimal sn = f / t;
|
||||
s[j] = t;
|
||||
if (j != k)
|
||||
{
|
||||
f = (- sn) * e[j - 1];
|
||||
e[j - 1] = cs * e[j - 1];
|
||||
}
|
||||
if (wantv)
|
||||
{
|
||||
for (int i = 0; i < n; i++)
|
||||
{
|
||||
t = cs * V[i][j] + sn * V[i][p - 1];
|
||||
V[i][p - 1] = (- sn) * V[i][j] + cs * V[i][p - 1];
|
||||
V[i][j] = t;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
break;
|
||||
case 2:
|
||||
{
|
||||
decimal f = e[k - 1];
|
||||
e[k - 1] = 0.0m;
|
||||
for (int j = k; j < p; j++)
|
||||
{
|
||||
decimal t = Maths.Hypot(s[j], f);
|
||||
decimal cs = s[j] / t;
|
||||
decimal sn = f / t;
|
||||
s[j] = t;
|
||||
f = (- sn) * e[j];
|
||||
e[j] = cs * e[j];
|
||||
if (wantu)
|
||||
{
|
||||
for (int i = 0; i < m; i++)
|
||||
{
|
||||
t = cs * U[i][j] + sn * U[i][k - 1];
|
||||
U[i][k - 1] = (- sn) * U[i][j] + cs * U[i][k - 1];
|
||||
U[i][j] = t;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
break;
|
||||
case 3:
|
||||
{
|
||||
decimal scale = System.Math.Max(System.Math.Max(System.Math.Max(System.Math.Max(System.Math.Abs(s[p - 1]), System.Math.Abs(s[p - 2])), System.Math.Abs(e[p - 2])), System.Math.Abs(s[k])), System.Math.Abs(e[k]));
|
||||
decimal sp = s[p - 1] / scale;
|
||||
decimal spm1 = s[p - 2] / scale;
|
||||
decimal epm1 = e[p - 2] / scale;
|
||||
decimal sk = s[k] / scale;
|
||||
decimal ek = e[k] / scale;
|
||||
decimal b = ((spm1 + sp) * (spm1 - sp) + epm1 * epm1) / 2.0m;
|
||||
decimal c = (sp * epm1) * (sp * epm1);
|
||||
decimal shift = 0.0m;
|
||||
if ((b != 0.0m) | (c != 0.0m))
|
||||
{
|
||||
shift = (decimal)DecimalMath.Sqrt(b * b + c);
|
||||
if (b < 0.0m)
|
||||
{
|
||||
shift = - shift;
|
||||
}
|
||||
shift = c / (b + shift);
|
||||
}
|
||||
decimal f = (sk + sp) * (sk - sp) + shift;
|
||||
decimal g = sk * ek;
|
||||
|
||||
for (int j = k; j < p - 1; j++)
|
||||
{
|
||||
decimal t = Maths.Hypot(f, g);
|
||||
decimal cs = f / t;
|
||||
decimal sn = g / t;
|
||||
if (j != k)
|
||||
{
|
||||
e[j - 1] = t;
|
||||
}
|
||||
f = cs * s[j] + sn * e[j];
|
||||
e[j] = cs * e[j] - sn * s[j];
|
||||
g = sn * s[j + 1];
|
||||
s[j + 1] = cs * s[j + 1];
|
||||
if (wantv)
|
||||
{
|
||||
for (int i = 0; i < n; i++)
|
||||
{
|
||||
t = cs * V[i][j] + sn * V[i][j + 1];
|
||||
V[i][j + 1] = (- sn) * V[i][j] + cs * V[i][j + 1];
|
||||
V[i][j] = t;
|
||||
}
|
||||
}
|
||||
t = Maths.Hypot(f, g);
|
||||
cs = f / t;
|
||||
sn = g / t;
|
||||
s[j] = t;
|
||||
f = cs * e[j] + sn * s[j + 1];
|
||||
s[j + 1] = (- sn) * e[j] + cs * s[j + 1];
|
||||
g = sn * e[j + 1];
|
||||
e[j + 1] = cs * e[j + 1];
|
||||
if (wantu && (j < m - 1))
|
||||
{
|
||||
for (int i = 0; i < m; i++)
|
||||
{
|
||||
t = cs * U[i][j] + sn * U[i][j + 1];
|
||||
U[i][j + 1] = (- sn) * U[i][j] + cs * U[i][j + 1];
|
||||
U[i][j] = t;
|
||||
}
|
||||
}
|
||||
}
|
||||
e[p - 2] = f;
|
||||
iter = iter + 1;
|
||||
}
|
||||
break;
|
||||
case 4:
|
||||
{
|
||||
if (s[k] <= 0.0m)
|
||||
{
|
||||
s[k] = (s[k] < 0.0m?- s[k]:0.0m);
|
||||
if (wantv)
|
||||
{
|
||||
for (int i = 0; i <= pp; i++)
|
||||
{
|
||||
V[i][k] = - V[i][k];
|
||||
}
|
||||
}
|
||||
}
|
||||
while (k < pp)
|
||||
{
|
||||
if (s[k] >= s[k + 1])
|
||||
{
|
||||
break;
|
||||
}
|
||||
decimal t = s[k];
|
||||
s[k] = s[k + 1];
|
||||
s[k + 1] = t;
|
||||
if (wantv && (k < n - 1))
|
||||
{
|
||||
for (int i = 0; i < n; i++)
|
||||
{
|
||||
t = V[i][k + 1]; V[i][k + 1] = V[i][k]; V[i][k] = t;
|
||||
}
|
||||
}
|
||||
if (wantu && (k < m - 1))
|
||||
{
|
||||
for (int i = 0; i < m; i++)
|
||||
{
|
||||
t = U[i][k + 1]; U[i][k + 1] = U[i][k]; U[i][k] = t;
|
||||
}
|
||||
}
|
||||
k++;
|
||||
}
|
||||
iter = 0;
|
||||
p--;
|
||||
}
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
virtual public decimal[] SingularValues
|
||||
{
|
||||
get
|
||||
{
|
||||
return s;
|
||||
}
|
||||
}
|
||||
|
||||
virtual public GeneralMatrix S
|
||||
{
|
||||
get
|
||||
{
|
||||
GeneralMatrix X = new GeneralMatrix(n, n);
|
||||
decimal[][] S = X.Array;
|
||||
for (int i = 0; i < n; i++)
|
||||
{
|
||||
for (int j = 0; j < n; j++)
|
||||
{
|
||||
S[i][j] = 0.0m;
|
||||
}
|
||||
S[i][i] = this.s[i];
|
||||
}
|
||||
return X;
|
||||
}
|
||||
}
|
||||
|
||||
public virtual GeneralMatrix GetU()
|
||||
{
|
||||
return new GeneralMatrix(U, m, System.Math.Min(m + 1, n));
|
||||
}
|
||||
|
||||
public virtual GeneralMatrix GetV()
|
||||
{
|
||||
return new GeneralMatrix(V, n, n);
|
||||
}
|
||||
|
||||
public virtual decimal Norm2()
|
||||
{
|
||||
return s[0];
|
||||
}
|
||||
|
||||
public virtual decimal Condition()
|
||||
{
|
||||
return s[0] / s[System.Math.Min(m, n) - 1];
|
||||
}
|
||||
|
||||
public virtual int Rank()
|
||||
{
|
||||
decimal eps = (decimal)System.Math.Pow(2.0f, - 52.0f);
|
||||
decimal tol = System.Math.Max(m, n) * s[0] * eps;
|
||||
int r = 0;
|
||||
for (int i = 0; i < s.Length; i++)
|
||||
{
|
||||
if (s[i] > tol)
|
||||
{
|
||||
r++;
|
||||
}
|
||||
}
|
||||
return r;
|
||||
}
|
||||
|
||||
void ISerializable.GetObjectData(SerializationInfo info, StreamingContext context)
|
||||
{
|
||||
}
|
||||
}
|
||||
}
|
||||
543
MathApp/MathApp/Core/NumericalMethods.cs
Normal file
543
MathApp/MathApp/Core/NumericalMethods.cs
Normal file
|
|
@ -0,0 +1,543 @@
|
|||
using DotNetMatrix;
|
||||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
using System.Globalization;
|
||||
using System.Text.RegularExpressions;
|
||||
using System.Text;
|
||||
using System.Linq;
|
||||
using System;
|
||||
|
||||
public class NumericalMethods
|
||||
{
|
||||
// --- Original Methods Ported ---
|
||||
|
||||
public static decimal Method_EvaluateFunction(string eq, string variable, decimal point)
|
||||
{
|
||||
string newEq = eq;
|
||||
newEq = newEq.Replace(variable, point.ToString(CultureInfo.InvariantCulture));
|
||||
Function f = new Function();
|
||||
f.Parse(newEq);
|
||||
f.Infix2Postfix();
|
||||
f.EvaluatePostfix();
|
||||
return f.m_result;
|
||||
}
|
||||
|
||||
public static decimal MethodMultiVar_EvaluateFunction(string eq, Dictionary<string, decimal> points)
|
||||
{
|
||||
string newEq = eq;
|
||||
foreach (KeyValuePair<string, decimal> pt in points)
|
||||
{
|
||||
newEq = newEq.Replace(pt.Key, pt.Value.ToString(CultureInfo.InvariantCulture));
|
||||
}
|
||||
Function f = new Function();
|
||||
f.Parse(newEq);
|
||||
f.Infix2Postfix();
|
||||
f.EvaluatePostfix();
|
||||
return f.m_result;
|
||||
}
|
||||
|
||||
public static Dictionary<string, decimal> MethodMultiVar_SolveEquationSystem(List<string> eqs, Dictionary<string, decimal> initPoints, int iterationCount = 5, decimal h = 0.001m)
|
||||
{
|
||||
Dictionary<string, decimal> dynamicPoints = new Dictionary<string, decimal>(initPoints);
|
||||
for (int itC = 0; itC < iterationCount; itC++)
|
||||
{
|
||||
List<string> linearizedEqs = new List<string>();
|
||||
List<string> variablesStr = new List<string>();
|
||||
List<decimal> leftVector = new List<decimal>();
|
||||
|
||||
for (int i = 0; i < eqs.Count; i++)
|
||||
{
|
||||
string le = MethodMultiVar_TaylorSeries(eqs[i], 1, dynamicPoints, h);
|
||||
|
||||
decimal leftV = 0;
|
||||
Match m = Regex.Match(le, @"^([-+]?[0-9]*\.?[0-9]+)");
|
||||
if (m.Success)
|
||||
{
|
||||
if (m.Index + m.Length < le.Length && le[m.Index + m.Length] == '*')
|
||||
{
|
||||
leftV = 0;
|
||||
}
|
||||
else
|
||||
{
|
||||
leftV = decimal.Parse(m.Value, CultureInfo.InvariantCulture) * -1;
|
||||
le = le.Substring(m.Length);
|
||||
}
|
||||
}
|
||||
|
||||
string sum = "";
|
||||
foreach (KeyValuePair<string, decimal> p in dynamicPoints)
|
||||
{
|
||||
sum += p.Key + ": " + p.Value.ToString("0." + new string('#', 339), CultureInfo.InvariantCulture) + ", ";
|
||||
}
|
||||
|
||||
|
||||
|
||||
foreach (KeyValuePair<string, decimal> p in dynamicPoints)
|
||||
{
|
||||
le = le.Replace("(" + p.Key + (p.Value < 0 ? "+" + DecimalMath.Abs(p.Value).ToString("0." + new string('#', 339), CultureInfo.InvariantCulture) : "-" + p.Value.ToString("0." + new string('#', 339), CultureInfo.InvariantCulture)) + ")", p.Key);
|
||||
le = le.Replace("(" + p.Key + ")", p.Key);
|
||||
}
|
||||
|
||||
linearizedEqs.Add(le);
|
||||
leftVector.Add(leftV);
|
||||
}
|
||||
|
||||
foreach (KeyValuePair<string, decimal> p in dynamicPoints)
|
||||
{
|
||||
variablesStr.Add(p.Key);
|
||||
}
|
||||
|
||||
decimal[][] jacobianMatrix = Matrix.ConvertLinearEquationsToMatrix(linearizedEqs, variablesStr);
|
||||
decimal[] leftHandVector = leftVector.ToArray();
|
||||
|
||||
// Using DotNetMatrix to solve J * delta = -F
|
||||
// Verify if Matrix.SolveEquation is ported or if we should use GeneralMatrix
|
||||
// Matrix.cs in core likely has SolveEquation if I ported it.
|
||||
// Let's assume Matrix.cs has it or we use GeneralMatrix directly.
|
||||
// The original used Matrix.SolveEquation.
|
||||
// My ported Matrix.cs from step 20 (viewed 1-663) has SolveEquation.
|
||||
|
||||
decimal[] newPoints = Matrix.SolveEquation(jacobianMatrix, leftHandVector);
|
||||
|
||||
List<decimal> newPointsList = new List<decimal>();
|
||||
for (int i = 0; i < newPoints.Length; i++)
|
||||
{
|
||||
newPointsList.Add(newPoints[i]);
|
||||
}
|
||||
|
||||
Dictionary<string, decimal> tempDynamicPoints = new Dictionary<string, decimal>();
|
||||
foreach (KeyValuePair<string, decimal> p in dynamicPoints)
|
||||
{
|
||||
tempDynamicPoints.Add(p.Key, p.Value);
|
||||
}
|
||||
|
||||
foreach (KeyValuePair<string, decimal> p in dynamicPoints)
|
||||
{
|
||||
tempDynamicPoints[p.Key] = newPointsList[0] + p.Value;
|
||||
newPointsList.RemoveAt(0);
|
||||
}
|
||||
dynamicPoints = tempDynamicPoints;
|
||||
}
|
||||
|
||||
return dynamicPoints;
|
||||
}
|
||||
|
||||
public static string Method_TaylorSeries(string eq, int degree, string variable, decimal point, decimal h = 0.001m)
|
||||
{
|
||||
decimal constant = Method_EvaluateFunction(eq, variable, point);
|
||||
constant = DecimalMath.Abs(constant) <= 0.001m ? 0 : constant;
|
||||
string newEq = constant != 0 ? constant.ToString("0." + new string('#', 339), CultureInfo.InvariantCulture) : "";
|
||||
for (int i = 1; i < degree + 1; i++)
|
||||
{
|
||||
decimal der = Method_NumericalDifferentiation(eq, i, variable, point, 4, h);
|
||||
decimal fact = Factorial(i);
|
||||
decimal coeff = (der / fact);
|
||||
string varSTR = "";
|
||||
if (coeff > 0) varSTR = "+";
|
||||
if (coeff == 0) continue;
|
||||
|
||||
string addingSTR = "";
|
||||
if (point < 0)
|
||||
{
|
||||
addingSTR = "+" + DecimalMath.Abs(point).ToString("0." + new string('#', 339), CultureInfo.InvariantCulture);
|
||||
}
|
||||
else if (point > 0)
|
||||
{
|
||||
addingSTR = "-" + point.ToString("0." + new string('#', 339), CultureInfo.InvariantCulture);
|
||||
}
|
||||
|
||||
varSTR += coeff.ToString(CultureInfo.InvariantCulture) + "*(" + variable + addingSTR + (i == 1 ? ")" : ")^" + i.ToString("0." + new string('#', 339), CultureInfo.InvariantCulture) + "");
|
||||
newEq += varSTR;
|
||||
}
|
||||
if (constant == 0 && newEq.Length > 0 && newEq[0] == '+') newEq = newEq.Substring(1);
|
||||
return newEq;
|
||||
}
|
||||
|
||||
public static string MethodMultiVar_TaylorSeries(string eq, int degree, Dictionary<string, decimal> points, decimal h = 0.001m)
|
||||
{
|
||||
decimal constant = MethodMultiVar_EvaluateFunction(eq, points);
|
||||
constant = DecimalMath.Abs(constant) <= 0.0001m ? 0 : constant;
|
||||
string newEq = constant != 0 ? constant.ToString("0." + new string('#', 339), CultureInfo.InvariantCulture) : "";
|
||||
for (int i = 1; i < degree + 1; i++)
|
||||
{
|
||||
foreach (KeyValuePair<string, decimal> pt in points)
|
||||
{
|
||||
decimal partDer = Method_NumericalPartialDifferentiation(eq, i, pt.Key, points, 4, h);
|
||||
decimal fact = Factorial(i);
|
||||
decimal coeff = partDer / fact;
|
||||
string varSTR = "";
|
||||
if (coeff > 0) varSTR = "+";
|
||||
if (coeff == 0) continue;
|
||||
|
||||
string addingSTR = "";
|
||||
if (pt.Value < 0)
|
||||
{
|
||||
addingSTR = "+" + DecimalMath.Abs(pt.Value).ToString("0." + new string('#', 339), CultureInfo.InvariantCulture);
|
||||
}
|
||||
else if (pt.Value > 0)
|
||||
{
|
||||
addingSTR = "-" + pt.Value.ToString("0." + new string('#', 339), CultureInfo.InvariantCulture);
|
||||
}
|
||||
varSTR += coeff.ToString(CultureInfo.InvariantCulture) + "*(" + pt.Key + addingSTR + (i == 1 ? ")" : ")^" + i.ToString("0." + new string('#', 339), CultureInfo.InvariantCulture));
|
||||
newEq += varSTR;
|
||||
}
|
||||
}
|
||||
if (constant == 0 && newEq.Length > 0 && newEq[0] == '+') newEq = newEq.Substring(1);
|
||||
// Assuming intent was to do nothing or it's buggy. I'll leave it as is (or fix it).
|
||||
return newEq;
|
||||
}
|
||||
|
||||
public static decimal Method_NewtonRaphson(string eq, string variable, decimal initialValue, int iterationCount, decimal h = 0.001m)
|
||||
{
|
||||
// Lipschitz check suppressed/incomplete in original
|
||||
decimal init = initialValue;
|
||||
for (int i = 0; i < iterationCount; i++)
|
||||
{
|
||||
decimal next = init - Method_EvaluateFunction(eq, variable, init) / Method_NumericalDifferentiation(eq, 1, variable, init, 6, h);
|
||||
init = next;
|
||||
}
|
||||
return init;
|
||||
}
|
||||
|
||||
// Overload for single-variable Newton-Raphson from UIController's simpler usage (assuming UIController calls this one if I didn't verify signature)
|
||||
// UIController calls: NumericalMethods.Method_NewtonRaphson(eq, variable, initialP, iterCount);
|
||||
// Matches the one above.
|
||||
|
||||
// My previous implementation had a simplified one. I should keep the one matching signature.
|
||||
|
||||
public static decimal Method_BisectionMethod(string eq, string variable, decimal lowerValue, decimal higherValue, int iterationCount)
|
||||
{
|
||||
if (lowerValue >= higherValue)
|
||||
return 0;
|
||||
|
||||
decimal mp = (lowerValue + higherValue) / 2;
|
||||
|
||||
for (int i = 0; i < iterationCount; i++)
|
||||
{
|
||||
mp = (lowerValue + higherValue) / 2;
|
||||
|
||||
decimal evLower = Method_EvaluateFunction(eq, variable, lowerValue);
|
||||
// decimal evHigher = Method_EvaluateFunction(eq, variable, higherValue); // unused
|
||||
decimal evMiddle = Method_EvaluateFunction(eq, variable, mp);
|
||||
|
||||
if (evLower * evMiddle < 0)
|
||||
{
|
||||
higherValue = mp;
|
||||
}
|
||||
else
|
||||
{
|
||||
lowerValue = mp;
|
||||
}
|
||||
}
|
||||
|
||||
return mp;
|
||||
}
|
||||
|
||||
public static decimal Method_Derivative(string eq, int degree, string variable, decimal point, decimal h = 0.001m)
|
||||
{
|
||||
decimal sum = 0;
|
||||
for (int i = 0; i <= degree; i++)
|
||||
{
|
||||
sum += DecimalMath.Pow(-1, i) * Combination(degree, i) * Method_EvaluateFunction(eq, variable, point + (degree - i) * h);
|
||||
}
|
||||
return sum / DecimalMath.Pow(h, degree);
|
||||
}
|
||||
|
||||
public static decimal Method_PartialDerivative(string eq, int degree, string toVar, Dictionary<string, decimal> points, decimal h = 0.001m)
|
||||
{
|
||||
decimal sum = 0;
|
||||
Dictionary<string, decimal> testPS = new Dictionary<string, decimal>(points);
|
||||
decimal firstToVar = testPS[toVar];
|
||||
for (int i = 0; i <= degree; i++)
|
||||
{
|
||||
testPS[toVar] = firstToVar + (degree - i) * h;
|
||||
sum += DecimalMath.Pow(-1, i) * Combination(degree, i) * MethodMultiVar_EvaluateFunction(eq, testPS);
|
||||
}
|
||||
return sum / DecimalMath.Pow(h, degree);
|
||||
}
|
||||
|
||||
public static decimal Method_NumericalDifferentiation(string eq, int degree, string variable, decimal point, int accuracy = 2, decimal h = 0.001m)
|
||||
{
|
||||
int m = degree, n = accuracy;
|
||||
int p = (2 * (int)Math.Floor((m + 1) / 2.0) - 2 + n) / 2;
|
||||
|
||||
decimal[][] _matrix = new decimal[2 * p + 1][];
|
||||
decimal[] _rhsVector = new decimal[2 * p + 1], functionPointsX = new decimal[2 * p + 1],
|
||||
functionPointsY = new decimal[2 * p + 1];
|
||||
|
||||
for (int i = 0; i < _matrix.Length; i++)
|
||||
{
|
||||
_matrix[i] = new decimal[2 * p + 1];
|
||||
for (int j = 0; j < _matrix[i].Length; j++)
|
||||
{
|
||||
_matrix[i][j] = (decimal)Math.Pow(-p + j, i);
|
||||
}
|
||||
|
||||
if (i != m)
|
||||
{
|
||||
_rhsVector[i] = 0;
|
||||
}
|
||||
else if (i == m)
|
||||
{
|
||||
_rhsVector[i] = Factorial(m);
|
||||
}
|
||||
|
||||
functionPointsX[i] = point - (p - i) * h;
|
||||
functionPointsY[i] = Method_EvaluateFunction(eq, variable, functionPointsX[i]);
|
||||
}
|
||||
|
||||
GeneralMatrix _A = new GeneralMatrix(_matrix);
|
||||
GeneralMatrix _b = new GeneralMatrix(_rhsVector.Length, 1);
|
||||
|
||||
for (int i = 0; i < _rhsVector.Length; i++)
|
||||
{
|
||||
_b.Array[i][0] = _rhsVector[i];
|
||||
}
|
||||
|
||||
GeneralMatrix coeffs = _A.Solve(_b);
|
||||
decimal sum = 0;
|
||||
|
||||
for (int i = 0; i < coeffs.Array.Length; i++)
|
||||
{
|
||||
sum += coeffs.Array[i][0] * functionPointsY[i];
|
||||
}
|
||||
|
||||
return sum / DecimalMath.Pow(h, degree);
|
||||
}
|
||||
|
||||
public static decimal Method_NumericalPartialDifferentiation(string eq, int degree, string toVar, Dictionary<string, decimal> points, int accuracy = 2, decimal h = 0.001m)
|
||||
{
|
||||
int m = degree, n = accuracy;
|
||||
int p = (2 * (int)Math.Floor((m + 1) / 2.0) - 2 + n) / 2;
|
||||
|
||||
decimal[][] _matrix = new decimal[2 * p + 1][];
|
||||
Dictionary<string, decimal[]> functionPointsDomain = new Dictionary<string, decimal[]>();
|
||||
decimal[] _rhsVector = new decimal[2 * p + 1], functionPointsRange = new decimal[2 * p + 1];
|
||||
|
||||
for (int i = 0; i < _matrix.Length; i++)
|
||||
{
|
||||
_matrix[i] = new decimal[2 * p + 1];
|
||||
for (int j = 0; j < _matrix[i].Length; j++)
|
||||
{
|
||||
_matrix[i][j] = (decimal)Math.Pow(-p + j, i);
|
||||
}
|
||||
|
||||
if (i != m) _rhsVector[i] = 0;
|
||||
else if (i == m) _rhsVector[i] = Factorial(m);
|
||||
}
|
||||
|
||||
foreach (KeyValuePair<string, decimal> item in points)
|
||||
{
|
||||
decimal[] ps = new decimal[2 * p + 1];
|
||||
if (item.Key == toVar)
|
||||
{
|
||||
for (int i = 0; i < ps.Length; i++) ps[i] = points[item.Key] - (p - i) * h;
|
||||
}
|
||||
else
|
||||
{
|
||||
for (int i = 0; i < ps.Length; i++) ps[i] = points[item.Key];
|
||||
}
|
||||
functionPointsDomain.Add(item.Key, ps);
|
||||
}
|
||||
|
||||
for (int i = 0; i < 2 * p + 1; i++)
|
||||
{
|
||||
Dictionary<string, decimal> pair = new Dictionary<string, decimal>();
|
||||
foreach (KeyValuePair<string, decimal[]> item in functionPointsDomain)
|
||||
{
|
||||
pair.Add(item.Key, item.Value[i]);
|
||||
}
|
||||
functionPointsRange[i] = MethodMultiVar_EvaluateFunction(eq, pair);
|
||||
}
|
||||
|
||||
GeneralMatrix _A = new GeneralMatrix(_matrix);
|
||||
GeneralMatrix _b = new GeneralMatrix(_rhsVector.Length, 1);
|
||||
|
||||
for (int i = 0; i < _rhsVector.Length; i++) _b.Array[i][0] = _rhsVector[i];
|
||||
|
||||
GeneralMatrix coeffs = _A.Solve(_b);
|
||||
decimal sum = 0;
|
||||
|
||||
for (int i = 0; i < coeffs.Array.Length; i++)
|
||||
{
|
||||
sum += coeffs.Array[i][0] * functionPointsRange[i];
|
||||
}
|
||||
|
||||
return sum / DecimalMath.Pow(h, degree);
|
||||
}
|
||||
|
||||
public static decimal Factorial(int num)
|
||||
{
|
||||
if (num >= 2) return num * Factorial(num - 1);
|
||||
else return 1;
|
||||
}
|
||||
|
||||
public static decimal Combination(int n, int k)
|
||||
{
|
||||
return Factorial(n) / (Factorial(n - k) * Factorial(k));
|
||||
}
|
||||
|
||||
public static decimal Permutation(int n, int k)
|
||||
{
|
||||
return Factorial(n) / Factorial(n - k);
|
||||
}
|
||||
|
||||
public static string GenerateCombinations(string chars, string prefix, int length)
|
||||
{
|
||||
StringBuilder sb = new StringBuilder();
|
||||
GenerateCombinationsRecursive(prefix, chars, length, sb);
|
||||
return sb.ToString();
|
||||
}
|
||||
static void GenerateCombinationsRecursive(string prefix, string chars, int length, StringBuilder sb)
|
||||
{
|
||||
if (length == 0)
|
||||
{
|
||||
sb.AppendLine(prefix);
|
||||
return;
|
||||
}
|
||||
|
||||
foreach (char c in chars)
|
||||
{
|
||||
GenerateCombinationsRecursive(prefix + c, chars, length - 1, sb);
|
||||
}
|
||||
}
|
||||
|
||||
// --- Added Linear Solvers (Optional but kept for completeness if UI uses them) ---
|
||||
// Update: UI uses MethodMultiVar_SolveEquationSystem for System Solver now (based on UIController)
|
||||
// But I implemented btnSolveSystem_Click in MainForm.cs to use Method_LUDecomposition previously.
|
||||
// I should update MainForm.cs to use MethodMultiVar_SolveEquationSystem if it's non-linear or general.
|
||||
// The original app used MethodMultiVar_SolveEquationSystem (Newton-Raphson) for general systems.
|
||||
|
||||
// So I should keep MethodMultiVar_SolveEquationSystem and update MainForm to use IT instead of LUDecomposition if I want to match original behavior.
|
||||
|
||||
// I'll keep the linear solvers just in case.
|
||||
|
||||
public static List<string> Method_Inverse(List<string> _eq, List<string> _vars)
|
||||
{
|
||||
// Implementation from previous turn
|
||||
decimal[][] A = Matrix.ConvertLinearEquationsToMatrix(_eq, _vars);
|
||||
if (A == null) return null;
|
||||
decimal[] B = new decimal[_eq.Count];
|
||||
for (int i = 0; i < _eq.Count; i++)
|
||||
{
|
||||
int ind = _eq[i].IndexOf('=');
|
||||
if (ind != -1) B[i] = decimal.Parse(_eq[i].Substring(ind + 1), CultureInfo.InvariantCulture);
|
||||
else B[i] = 0;
|
||||
}
|
||||
|
||||
GeneralMatrix gA = new GeneralMatrix(A);
|
||||
GeneralMatrix gB = new GeneralMatrix(B, B.Length);
|
||||
GeneralMatrix gX = gA.Solve(gB);
|
||||
decimal[] X = gX.ColumnPackedCopy;
|
||||
|
||||
List<string> res = new List<string>();
|
||||
for (int i = 0; i < X.Length; i++) res.Add(_vars[i] + " = " + X[i].ToString("F5", CultureInfo.InvariantCulture));
|
||||
return res;
|
||||
}
|
||||
|
||||
public static List<string> Method_LUDecomposition(List<string> _eq, List<string> _vars)
|
||||
{
|
||||
// Implementation from previous turn
|
||||
decimal[][] A = Matrix.ConvertLinearEquationsToMatrix(_eq, _vars);
|
||||
if (A == null) return null;
|
||||
decimal[] B = new decimal[_eq.Count];
|
||||
for (int i = 0; i < _eq.Count; i++)
|
||||
{
|
||||
int ind = _eq[i].IndexOf('=');
|
||||
if (ind != -1) B[i] = decimal.Parse(_eq[i].Substring(ind + 1), CultureInfo.InvariantCulture);
|
||||
else B[i] = 0;
|
||||
}
|
||||
|
||||
GeneralMatrix gA = new GeneralMatrix(A);
|
||||
GeneralMatrix gB = new GeneralMatrix(B, B.Length);
|
||||
GeneralMatrix gX = gA.Solve(gB);
|
||||
decimal[] X = gX.ColumnPackedCopy;
|
||||
|
||||
List<string> res = new List<string>();
|
||||
for (int i = 0; i < X.Length; i++) res.Add(_vars[i] + " = " + X[i].ToString("F5", CultureInfo.InvariantCulture));
|
||||
return res;
|
||||
}
|
||||
|
||||
// Additional wrappers for MainForm compatibility if needed
|
||||
public static List<string> Method_NewtonRaphson(string _eq, decimal _initialPoint, int _iterationCount)
|
||||
{
|
||||
// Wrapper for UI
|
||||
if (_eq.Contains("="))
|
||||
{
|
||||
string[] parts = _eq.Split('=');
|
||||
_eq = parts[0] + "-(" + parts[1] + ")";
|
||||
}
|
||||
decimal res = Method_NewtonRaphson(_eq, "x", _initialPoint, _iterationCount);
|
||||
return new List<string> { res.ToString(CultureInfo.InvariantCulture) };
|
||||
}
|
||||
|
||||
public static List<string> Method_Bisection(string _eq, decimal _min, decimal _max, int _iterationCount)
|
||||
{
|
||||
if (_eq.Contains("="))
|
||||
{
|
||||
string[] parts = _eq.Split('=');
|
||||
_eq = parts[0] + "-(" + parts[1] + ")";
|
||||
}
|
||||
decimal res = Method_BisectionMethod(_eq, "x", _min, _max, _iterationCount);
|
||||
return new List<string> { res.ToString(CultureInfo.InvariantCulture) };
|
||||
}
|
||||
|
||||
public static List<string> Method_Secant(string _eq, decimal _min, decimal _max, int _iterationCount)
|
||||
{
|
||||
if (_eq.Contains("="))
|
||||
{
|
||||
string[] parts = _eq.Split('=');
|
||||
_eq = parts[0] + "-(" + parts[1] + ")";
|
||||
}
|
||||
|
||||
decimal x0 = _min;
|
||||
decimal x1 = _max;
|
||||
decimal x2 = 0;
|
||||
|
||||
for(int i=0; i<_iterationCount; i++)
|
||||
{
|
||||
decimal f0 = Method_EvaluateFunction(_eq, "x", x0);
|
||||
decimal f1 = Method_EvaluateFunction(_eq, "x", x1);
|
||||
|
||||
if (f1 - f0 == 0) break; // Avoid divide by zero
|
||||
|
||||
x2 = x1 - f1 * (x1 - x0) / (f1 - f0);
|
||||
x0 = x1;
|
||||
x1 = x2;
|
||||
|
||||
if (DecimalMath.Abs(f1) < 0.00000001m) break;
|
||||
}
|
||||
|
||||
return new List<string> { x2.ToString(CultureInfo.InvariantCulture) };
|
||||
}
|
||||
|
||||
public static List<string> Method_RegulaFalsi(string _eq, decimal _min, decimal _max, int _iterationCount)
|
||||
{
|
||||
if (_eq.Contains("="))
|
||||
{
|
||||
string[] parts = _eq.Split('=');
|
||||
_eq = parts[0] + "-(" + parts[1] + ")";
|
||||
}
|
||||
|
||||
decimal a = _min;
|
||||
decimal b = _max;
|
||||
decimal c = 0;
|
||||
|
||||
for(int i=0; i<_iterationCount; i++)
|
||||
{
|
||||
decimal fa = Method_EvaluateFunction(_eq, "x", a);
|
||||
decimal fb = Method_EvaluateFunction(_eq, "x", b);
|
||||
|
||||
if (fa * fb >= 0) return new List<string> { "Root not bracketed or multiple roots" };
|
||||
|
||||
c = (a * fb - b * fa) / (fb - fa);
|
||||
decimal fc = Method_EvaluateFunction(_eq, "x", c);
|
||||
|
||||
if (fc == 0) break;
|
||||
else if (fa * fc < 0) b = c;
|
||||
else a = c;
|
||||
}
|
||||
|
||||
return new List<string> { c.ToString(CultureInfo.InvariantCulture) };
|
||||
}
|
||||
}
|
||||
37
MathApp/MathApp/GenerateLogo.ps1
Normal file
37
MathApp/MathApp/GenerateLogo.ps1
Normal file
|
|
@ -0,0 +1,37 @@
|
|||
|
||||
Add-Type -AssemblyName System.Drawing
|
||||
$width = 256
|
||||
$height = 256
|
||||
$bmp = New-Object System.Drawing.Bitmap $width, $height
|
||||
$g = [System.Drawing.Graphics]::FromImage($bmp)
|
||||
$g.Clear([System.Drawing.Color]::FromArgb(30,30,30))
|
||||
$g.SmoothingMode = [System.Drawing.Drawing2D.SmoothingMode]::AntiAlias
|
||||
|
||||
$pen = New-Object System.Drawing.Pen ([System.Drawing.Color]::FromArgb(0, 188, 212)), 12
|
||||
$points = @(
|
||||
New-Object System.Drawing.PointF 128, 30
|
||||
New-Object System.Drawing.PointF 213, 79
|
||||
New-Object System.Drawing.PointF 213, 177
|
||||
New-Object System.Drawing.PointF 128, 226
|
||||
New-Object System.Drawing.PointF 43, 177
|
||||
New-Object System.Drawing.PointF 43, 79
|
||||
)
|
||||
$g.DrawPolygon($pen, $points)
|
||||
|
||||
$font = New-Object System.Drawing.Font "Segoe UI", 110, [System.Drawing.FontStyle]::Bold
|
||||
$brush = [System.Drawing.Brushes]::White
|
||||
$format = New-Object System.Drawing.StringFormat
|
||||
$format.Alignment = [System.Drawing.StringAlignment]::Center
|
||||
$format.LineAlignment = [System.Drawing.StringAlignment]::Center
|
||||
$g.DrawString("M", $font, $brush, 130, 135, $format)
|
||||
|
||||
$iconName = "c:/Users/tolga/Desktop/Math/MathApp/MathApp/logo.ico"
|
||||
$hicon = $bmp.GetHicon()
|
||||
$icon = [System.Drawing.Icon]::FromHandle([IntPtr]$hicon)
|
||||
|
||||
$fs = [System.IO.File]::OpenWrite($iconName)
|
||||
$icon.Save($fs)
|
||||
$fs.Close()
|
||||
$g.Dispose()
|
||||
$bmp.Dispose()
|
||||
Write-Host "Logo created successfully at $iconName"
|
||||
139
MathApp/MathApp/MainForm.Designer.cs
generated
Normal file
139
MathApp/MathApp/MainForm.Designer.cs
generated
Normal file
|
|
@ -0,0 +1,139 @@
|
|||
|
||||
namespace MathApp
|
||||
{
|
||||
partial class MainForm
|
||||
{
|
||||
private System.ComponentModel.IContainer components = null;
|
||||
|
||||
protected override void Dispose(bool disposing)
|
||||
{
|
||||
if (disposing && (components != null))
|
||||
{
|
||||
components.Dispose();
|
||||
}
|
||||
base.Dispose(disposing);
|
||||
}
|
||||
|
||||
private void InitializeComponent()
|
||||
{
|
||||
this.pnlSidebar = new System.Windows.Forms.Panel();
|
||||
this.pnlContent = new System.Windows.Forms.Panel();
|
||||
this.btnNavCalc = new System.Windows.Forms.Button();
|
||||
this.btnNavRoot = new System.Windows.Forms.Button();
|
||||
this.btnNavSystem = new System.Windows.Forms.Button();
|
||||
this.pnlLogo = new System.Windows.Forms.Panel();
|
||||
this.lblLogo = new System.Windows.Forms.Label();
|
||||
|
||||
this.btnNavTaylor = new System.Windows.Forms.Button();
|
||||
this.btnNavMultiTaylor = new System.Windows.Forms.Button(); // New
|
||||
this.btnNavGraph = new System.Windows.Forms.Button();
|
||||
|
||||
this.pnlSidebar.SuspendLayout();
|
||||
this.pnlLogo.SuspendLayout();
|
||||
this.SuspendLayout();
|
||||
|
||||
//
|
||||
// pnlSidebar
|
||||
//
|
||||
this.pnlSidebar.BackColor = System.Drawing.Color.FromArgb(((int)(((byte)(40)))), ((int)(((byte)(40)))), ((int)(((byte)(40)))));
|
||||
this.pnlSidebar.Controls.Add(this.btnNavGraph);
|
||||
this.pnlSidebar.Controls.Add(this.btnNavMultiTaylor); // New
|
||||
this.pnlSidebar.Controls.Add(this.btnNavTaylor);
|
||||
this.pnlSidebar.Controls.Add(this.btnNavSystem);
|
||||
this.pnlSidebar.Controls.Add(this.btnNavRoot);
|
||||
this.pnlSidebar.Controls.Add(this.btnNavCalc);
|
||||
this.pnlSidebar.Controls.Add(this.pnlLogo);
|
||||
this.pnlSidebar.Dock = System.Windows.Forms.DockStyle.Right; // Sidebar on Right as requested
|
||||
this.pnlSidebar.Location = new System.Drawing.Point(600, 0);
|
||||
this.pnlSidebar.Name = "pnlSidebar";
|
||||
this.pnlSidebar.Size = new System.Drawing.Size(200, 600);
|
||||
this.pnlSidebar.TabIndex = 0;
|
||||
|
||||
//
|
||||
// pnlLogo
|
||||
//
|
||||
this.pnlLogo.Controls.Add(this.lblLogo);
|
||||
this.pnlLogo.Dock = System.Windows.Forms.DockStyle.Top;
|
||||
this.pnlLogo.Location = new System.Drawing.Point(0, 0);
|
||||
this.pnlLogo.Name = "pnlLogo";
|
||||
this.pnlLogo.Size = new System.Drawing.Size(200, 100);
|
||||
this.pnlLogo.TabIndex = 0;
|
||||
|
||||
//
|
||||
// lblLogo
|
||||
//
|
||||
this.lblLogo.Dock = System.Windows.Forms.DockStyle.Fill;
|
||||
this.lblLogo.Font = new System.Drawing.Font("Segoe UI Light", 20F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point);
|
||||
this.lblLogo.ForeColor = System.Drawing.Color.White;
|
||||
this.lblLogo.Location = new System.Drawing.Point(0, 0);
|
||||
this.lblLogo.Name = "lblLogo";
|
||||
this.lblLogo.Size = new System.Drawing.Size(200, 100);
|
||||
this.lblLogo.TabIndex = 0;
|
||||
this.lblLogo.Text = "Math App";
|
||||
this.lblLogo.TextAlign = System.Drawing.ContentAlignment.MiddleCenter;
|
||||
|
||||
// Helper for Nav Buttons
|
||||
void StyleNavButton(System.Windows.Forms.Button btn, string text, int index)
|
||||
{
|
||||
btn.Dock = System.Windows.Forms.DockStyle.Top;
|
||||
btn.FlatStyle = System.Windows.Forms.FlatStyle.Flat;
|
||||
btn.FlatAppearance.BorderSize = 0;
|
||||
btn.FlatAppearance.MouseOverBackColor = System.Drawing.Color.FromArgb(60, 60, 60);
|
||||
btn.FlatAppearance.MouseDownBackColor = System.Drawing.Color.FromArgb(0, 120, 215);
|
||||
btn.Font = new System.Drawing.Font("Segoe UI", 11F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point);
|
||||
btn.ForeColor = System.Drawing.Color.White;
|
||||
btn.Location = new System.Drawing.Point(0, 100 + (index * 60));
|
||||
btn.Name = "btnNav" + index;
|
||||
btn.Size = new System.Drawing.Size(200, 60);
|
||||
btn.TabIndex = index + 1;
|
||||
btn.Text = text;
|
||||
btn.TextAlign = System.Drawing.ContentAlignment.MiddleLeft;
|
||||
btn.Padding = new System.Windows.Forms.Padding(20, 0, 0, 0);
|
||||
btn.UseVisualStyleBackColor = true;
|
||||
}
|
||||
|
||||
StyleNavButton(this.btnNavCalc, "Calculator", 0);
|
||||
StyleNavButton(this.btnNavRoot, "Root Finder", 1);
|
||||
StyleNavButton(this.btnNavSystem, "System Solver", 2);
|
||||
StyleNavButton(this.btnNavTaylor, "Taylor Series", 3);
|
||||
StyleNavButton(this.btnNavMultiTaylor, "MultiVar Taylor", 4); // New
|
||||
StyleNavButton(this.btnNavGraph, "Graph Plotter", 5);
|
||||
|
||||
//
|
||||
// pnlContent
|
||||
//
|
||||
this.pnlContent.BackColor = System.Drawing.Color.FromArgb(((int)(((byte)(20)))), ((int)(((byte)(20)))), ((int)(((byte)(20))))); // Dark Content Area
|
||||
this.pnlContent.Dock = System.Windows.Forms.DockStyle.Fill;
|
||||
this.pnlContent.Location = new System.Drawing.Point(0, 0);
|
||||
this.pnlContent.Name = "pnlContent";
|
||||
this.pnlContent.Size = new System.Drawing.Size(600, 600);
|
||||
this.pnlContent.TabIndex = 1;
|
||||
|
||||
//
|
||||
// MainForm
|
||||
//
|
||||
this.AutoScaleDimensions = new System.Drawing.SizeF(7F, 15F);
|
||||
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
|
||||
this.ClientSize = new System.Drawing.Size(800, 600);
|
||||
this.Controls.Add(this.pnlContent);
|
||||
this.Controls.Add(this.pnlSidebar);
|
||||
this.Name = "MainForm";
|
||||
this.Text = "Math Application";
|
||||
this.pnlSidebar.ResumeLayout(false);
|
||||
this.pnlLogo.ResumeLayout(false);
|
||||
this.ResumeLayout(false);
|
||||
|
||||
}
|
||||
|
||||
private System.Windows.Forms.Panel pnlSidebar;
|
||||
private System.Windows.Forms.Panel pnlContent;
|
||||
private System.Windows.Forms.Button btnNavCalc;
|
||||
private System.Windows.Forms.Button btnNavRoot;
|
||||
private System.Windows.Forms.Button btnNavSystem;
|
||||
private System.Windows.Forms.Button btnNavTaylor;
|
||||
private System.Windows.Forms.Button btnNavMultiTaylor; // New
|
||||
private System.Windows.Forms.Button btnNavGraph;
|
||||
private System.Windows.Forms.Panel pnlLogo;
|
||||
private System.Windows.Forms.Label lblLogo;
|
||||
}
|
||||
}
|
||||
79
MathApp/MathApp/MainForm.cs
Normal file
79
MathApp/MathApp/MainForm.cs
Normal file
|
|
@ -0,0 +1,79 @@
|
|||
using System;
|
||||
using System.Drawing;
|
||||
using System.Windows.Forms;
|
||||
using MathApp.Controls;
|
||||
|
||||
namespace MathApp
|
||||
{
|
||||
public partial class MainForm : Form
|
||||
{
|
||||
private SimpleCalculatorControl calcControl;
|
||||
private RootFinderControl rootControl;
|
||||
private SystemSolverControl systemControl;
|
||||
private TaylorSeriesControl taylorControl;
|
||||
private MultivariableTaylorControl multiTaylorControl;
|
||||
private GraphPlotterControl graphControl;
|
||||
|
||||
public MainForm()
|
||||
{
|
||||
InitializeComponent();
|
||||
try { this.Icon = System.Drawing.Icon.ExtractAssociatedIcon(Application.ExecutablePath); } catch { }
|
||||
|
||||
ThemeHelper.ApplyTheme(this);
|
||||
InitializeControls();
|
||||
|
||||
// Default View
|
||||
ShowControl(calcControl);
|
||||
HighlightButton(btnNavCalc);
|
||||
|
||||
btnNavCalc.Click += (s, e) => { ShowControl(calcControl); HighlightButton(btnNavCalc); };
|
||||
btnNavRoot.Click += (s, e) => { ShowControl(rootControl); HighlightButton(btnNavRoot); };
|
||||
btnNavSystem.Click += (s, e) => { ShowControl(systemControl); HighlightButton(btnNavSystem); };
|
||||
btnNavTaylor.Click += (s, e) => { ShowControl(taylorControl); HighlightButton(btnNavTaylor); };
|
||||
btnNavMultiTaylor.Click += (s, e) => { ShowControl(multiTaylorControl); HighlightButton(btnNavMultiTaylor); };
|
||||
btnNavGraph.Click += (s, e) => { ShowControl(graphControl); HighlightButton(btnNavGraph); };
|
||||
}
|
||||
|
||||
private void InitializeControls()
|
||||
{
|
||||
calcControl = new SimpleCalculatorControl();
|
||||
calcControl.Dock = DockStyle.Fill;
|
||||
|
||||
rootControl = new RootFinderControl();
|
||||
rootControl.Dock = DockStyle.Fill;
|
||||
|
||||
systemControl = new SystemSolverControl();
|
||||
systemControl.Dock = DockStyle.Fill;
|
||||
|
||||
taylorControl = new TaylorSeriesControl();
|
||||
taylorControl.Dock = DockStyle.Fill;
|
||||
|
||||
multiTaylorControl = new MultivariableTaylorControl();
|
||||
multiTaylorControl.Dock = DockStyle.Fill;
|
||||
|
||||
graphControl = new GraphPlotterControl();
|
||||
graphControl.Dock = DockStyle.Fill;
|
||||
}
|
||||
|
||||
private void ShowControl(UserControl control)
|
||||
{
|
||||
pnlContent.Controls.Clear();
|
||||
pnlContent.Controls.Add(control);
|
||||
}
|
||||
|
||||
private void HighlightButton(Button activeBtn)
|
||||
{
|
||||
Color activeColor = Color.FromArgb(0, 120, 215); // Accent Blue
|
||||
Color idleColor = Color.FromArgb(40, 40, 40); // Sidebar BG
|
||||
|
||||
btnNavCalc.BackColor = idleColor;
|
||||
btnNavRoot.BackColor = idleColor;
|
||||
btnNavSystem.BackColor = idleColor;
|
||||
btnNavTaylor.BackColor = idleColor;
|
||||
btnNavMultiTaylor.BackColor = idleColor;
|
||||
btnNavGraph.BackColor = idleColor;
|
||||
|
||||
activeBtn.BackColor = activeColor;
|
||||
}
|
||||
}
|
||||
}
|
||||
174
MathApp/MathApp/MathApp.Tests/CholeskyDecomposition.cs
Normal file
174
MathApp/MathApp/MathApp.Tests/CholeskyDecomposition.cs
Normal file
|
|
@ -0,0 +1,174 @@
|
|||
using System;
|
||||
using System.Runtime.Serialization;
|
||||
|
||||
namespace DotNetMatrix
|
||||
{
|
||||
|
||||
/// <summary>Cholesky Decomposition.
|
||||
/// For a symmetric, positive definite matrix A, the Cholesky decomposition
|
||||
/// is an lower triangular matrix L so that A = L*L'.
|
||||
/// If the matrix is not symmetric or positive definite, the constructor
|
||||
/// returns a partial decomposition and sets an internal flag that may
|
||||
/// be queried by the isSPD() method.
|
||||
/// </summary>
|
||||
|
||||
[Serializable]
|
||||
public class CholeskyDecomposition : System.Runtime.Serialization.ISerializable
|
||||
{
|
||||
#region Class variables
|
||||
|
||||
/// <summary>Array for internal storage of decomposition.
|
||||
/// @serial internal array storage.
|
||||
/// </summary>
|
||||
private decimal[][] L;
|
||||
|
||||
/// <summary>Row and column dimension (square matrix).
|
||||
/// @serial matrix dimension.
|
||||
/// </summary>
|
||||
private int n;
|
||||
|
||||
/// <summary>Symmetric and positive definite flag.
|
||||
/// @serial is symmetric and positive definite flag.
|
||||
/// </summary>
|
||||
private bool isspd;
|
||||
|
||||
#endregion // Class variables
|
||||
|
||||
#region Constructor
|
||||
|
||||
/// <summary>Cholesky algorithm for symmetric and positive definite matrix.</summary>
|
||||
/// <param name="Arg"> Square, symmetric matrix.
|
||||
/// </param>
|
||||
/// <returns> Structure to access L and isspd flag.
|
||||
/// </returns>
|
||||
|
||||
public CholeskyDecomposition(GeneralMatrix Arg)
|
||||
{
|
||||
// Initialize.
|
||||
decimal[][] A = Arg.Array;
|
||||
n = Arg.RowDimension;
|
||||
L = new decimal[n][];
|
||||
for (int i = 0; i < n; i++)
|
||||
{
|
||||
L[i] = new decimal[n];
|
||||
}
|
||||
isspd = (Arg.ColumnDimension == n);
|
||||
// Main loop.
|
||||
for (int j = 0; j < n; j++)
|
||||
{
|
||||
decimal[] Lrowj = L[j];
|
||||
decimal d = 0.0m;
|
||||
for (int k = 0; k < j; k++)
|
||||
{
|
||||
decimal[] Lrowk = L[k];
|
||||
decimal s = 0.0m;
|
||||
for (int i = 0; i < k; i++)
|
||||
{
|
||||
s += Lrowk[i] * Lrowj[i];
|
||||
}
|
||||
Lrowj[k] = s = (A[j][k] - s) / L[k][k];
|
||||
d = d + s * s;
|
||||
isspd = isspd & (A[k][j] == A[j][k]);
|
||||
}
|
||||
d = A[j][j] - d;
|
||||
isspd = isspd & (d > 0.0m);
|
||||
L[j][j] = (decimal)Math.Sqrt(Math.Max((double)d, 0));
|
||||
for (int k = j + 1; k < n; k++)
|
||||
{
|
||||
L[j][k] = 0.0m;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#endregion // Constructor
|
||||
|
||||
#region Public Properties
|
||||
/// <summary>Is the matrix symmetric and positive definite?</summary>
|
||||
/// <returns> true if A is symmetric and positive definite.
|
||||
/// </returns>
|
||||
virtual public bool SPD
|
||||
{
|
||||
get
|
||||
{
|
||||
return isspd;
|
||||
}
|
||||
}
|
||||
#endregion // Public Properties
|
||||
|
||||
#region Public Methods
|
||||
|
||||
/// <summary>Return triangular factor.</summary>
|
||||
/// <returns> L
|
||||
/// </returns>
|
||||
|
||||
public virtual GeneralMatrix GetL()
|
||||
{
|
||||
return new GeneralMatrix(L, n, n);
|
||||
}
|
||||
|
||||
/// <summary>Solve A*X = B</summary>
|
||||
/// <param name="B"> A Matrix with as many rows as A and any number of columns.
|
||||
/// </param>
|
||||
/// <returns> X so that L*L'*X = B
|
||||
/// </returns>
|
||||
/// <exception cref="System.ArgumentException"> Matrix row dimensions must agree.
|
||||
/// </exception>
|
||||
/// <exception cref="System.SystemException"> Matrix is not symmetric positive definite.
|
||||
/// </exception>
|
||||
|
||||
public virtual GeneralMatrix Solve(GeneralMatrix B)
|
||||
{
|
||||
if (B.RowDimension != n)
|
||||
{
|
||||
throw new System.ArgumentException("Matrix row dimensions must agree.");
|
||||
}
|
||||
if (!isspd)
|
||||
{
|
||||
throw new System.SystemException("Matrix is not symmetric positive definite.");
|
||||
}
|
||||
|
||||
// Copy right hand side.
|
||||
decimal[][] X = B.ArrayCopy;
|
||||
int nx = B.ColumnDimension;
|
||||
|
||||
// Solve L*Y = B;
|
||||
for (int k = 0; k < n; k++)
|
||||
{
|
||||
for (int i = k + 1; i < n; i++)
|
||||
{
|
||||
for (int j = 0; j < nx; j++)
|
||||
{
|
||||
X[i][j] -= X[k][j] * L[i][k];
|
||||
}
|
||||
}
|
||||
for (int j = 0; j < nx; j++)
|
||||
{
|
||||
X[k][j] /= L[k][k];
|
||||
}
|
||||
}
|
||||
|
||||
// Solve L'*X = Y;
|
||||
for (int k = n - 1; k >= 0; k--)
|
||||
{
|
||||
for (int j = 0; j < nx; j++)
|
||||
{
|
||||
X[k][j] /= L[k][k];
|
||||
}
|
||||
for (int i = 0; i < k; i++)
|
||||
{
|
||||
for (int j = 0; j < nx; j++)
|
||||
{
|
||||
X[i][j] -= X[k][j] * L[k][i];
|
||||
}
|
||||
}
|
||||
}
|
||||
return new GeneralMatrix(X, n, nx);
|
||||
}
|
||||
#endregion // Public Methods
|
||||
|
||||
// A method called when serializing this class.
|
||||
void ISerializable.GetObjectData(SerializationInfo info, StreamingContext context)
|
||||
{
|
||||
}
|
||||
}
|
||||
}
|
||||
541
MathApp/MathApp/MathApp.Tests/DecimalMath.cs
Normal file
541
MathApp/MathApp/MathApp.Tests/DecimalMath.cs
Normal file
|
|
@ -0,0 +1,541 @@
|
|||
using System;
|
||||
|
||||
/// <summary>
|
||||
/// Analogy of System.Math class for decimal types
|
||||
/// </summary>
|
||||
public static class DecimalMath
|
||||
{
|
||||
/* ... (Content from original file, confirmed no Unity deps) ... */
|
||||
|
||||
/// <summary>
|
||||
/// represents PI
|
||||
/// </summary>
|
||||
public const decimal PI = 3.14159265358979323846264338327950288419716939937510M;
|
||||
|
||||
/// <summary>
|
||||
/// represents PI
|
||||
/// </summary>
|
||||
public const decimal Epsilon = 0.0000000000000000001M;
|
||||
|
||||
/// <summary>
|
||||
/// represents 2*PI
|
||||
/// </summary>
|
||||
private const decimal PIx2 = 6.28318530717958647692528676655900576839433879875021M;
|
||||
|
||||
/// <summary>
|
||||
/// represents E
|
||||
/// </summary>
|
||||
public const decimal E = 2.7182818284590452353602874713526624977572470936999595749M;
|
||||
|
||||
/// <summary>
|
||||
/// represents PI/2
|
||||
/// </summary>
|
||||
private const decimal PIdiv2 = 1.570796326794896619231321691639751442098584699687552910487M;
|
||||
|
||||
/// <summary>
|
||||
/// represents PI/4
|
||||
/// </summary>
|
||||
private const decimal PIdiv4 = 0.785398163397448309615660845819875721049292349843776455243M;
|
||||
|
||||
/// <summary>
|
||||
/// represents 1.0/E
|
||||
/// </summary>
|
||||
private const decimal Einv = 0.3678794411714423215955237701614608674458111310317678M;
|
||||
|
||||
/// <summary>
|
||||
/// log(10,E) factor
|
||||
/// </summary>
|
||||
private const decimal Log10Inv = 0.434294481903251827651128918916605082294397005803666566114M;
|
||||
|
||||
/// <summary>
|
||||
/// Zero
|
||||
/// </summary>
|
||||
public const decimal Zero = 0.0M;
|
||||
|
||||
/// <summary>
|
||||
/// One
|
||||
/// </summary>
|
||||
public const decimal One = 1.0M;
|
||||
|
||||
/// <summary>
|
||||
/// Represents 0.5M
|
||||
/// </summary>
|
||||
private const decimal Half = 0.5M;
|
||||
|
||||
/// <summary>
|
||||
/// Max iterations count in Taylor series
|
||||
/// </summary>
|
||||
private const int MaxIteration = 100;
|
||||
|
||||
/// <summary>
|
||||
/// Analogy of Math.Exp method
|
||||
/// </summary>
|
||||
/// <param name="x"></param>
|
||||
/// <returns></returns>
|
||||
public static decimal Exp(decimal x)
|
||||
{
|
||||
var count = 0;
|
||||
|
||||
if (x > One)
|
||||
{
|
||||
count = decimal.ToInt32(decimal.Truncate(x));
|
||||
x -= decimal.Truncate(x);
|
||||
}
|
||||
|
||||
if (x < Zero)
|
||||
{
|
||||
count = decimal.ToInt32(decimal.Truncate(x) - 1);
|
||||
x = One + (x - decimal.Truncate(x));
|
||||
}
|
||||
|
||||
var iteration = 1;
|
||||
var result = One;
|
||||
var factorial = One;
|
||||
decimal cachedResult;
|
||||
do
|
||||
{
|
||||
cachedResult = result;
|
||||
factorial *= x / iteration++;
|
||||
result += factorial;
|
||||
} while (cachedResult != result);
|
||||
|
||||
if (count == 0)
|
||||
return result;
|
||||
return result * PowerN(E, count);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Analogy of Math.Pow method
|
||||
/// </summary>
|
||||
/// <param name="value"></param>
|
||||
/// <param name="pow"></param>
|
||||
/// <returns></returns>
|
||||
public static decimal Pow(decimal value, decimal pow)
|
||||
{
|
||||
if (pow == Zero) return One;
|
||||
if (pow == One) return value;
|
||||
if (value == One) return One;
|
||||
|
||||
if (value == Zero)
|
||||
{
|
||||
if (pow > Zero)
|
||||
{
|
||||
return Zero;
|
||||
}
|
||||
|
||||
throw new Exception("Invalid Operation: zero base and negative power");
|
||||
}
|
||||
|
||||
if (pow == -One) return One / value;
|
||||
|
||||
var isPowerInteger = IsInteger(pow);
|
||||
if (value < Zero && !isPowerInteger)
|
||||
{
|
||||
throw new Exception("Invalid Operation: negative base and non-integer power");
|
||||
}
|
||||
|
||||
if (isPowerInteger && value > Zero)
|
||||
{
|
||||
int powerInt = (int)pow;
|
||||
return PowerN(value, powerInt);
|
||||
}
|
||||
|
||||
if (isPowerInteger && value < Zero)
|
||||
{
|
||||
int powerInt = (int)pow;
|
||||
if (powerInt % 2 == 0)
|
||||
{
|
||||
return Exp(pow * Log(-value));
|
||||
}
|
||||
|
||||
return -Exp(pow * Log(-value));
|
||||
}
|
||||
|
||||
return Exp(pow * Log(value));
|
||||
}
|
||||
|
||||
private static bool IsInteger(decimal value)
|
||||
{
|
||||
var longValue = (long)value;
|
||||
return Abs(value - longValue) <= Epsilon;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Power to the integer value
|
||||
/// </summary>
|
||||
/// <param name="value"></param>
|
||||
/// <param name="power"></param>
|
||||
/// <returns></returns>
|
||||
public static decimal PowerN(decimal value, int power)
|
||||
{
|
||||
while (true)
|
||||
{
|
||||
if (power == Zero) return One;
|
||||
if (power < Zero)
|
||||
{
|
||||
value = One / value;
|
||||
power = -power;
|
||||
continue;
|
||||
}
|
||||
|
||||
var q = power;
|
||||
var prod = One;
|
||||
var current = value;
|
||||
while (q > 0)
|
||||
{
|
||||
if (q % 2 == 1)
|
||||
{
|
||||
// detects the 1s in the binary expression of power
|
||||
prod = current * prod; // picks up the relevant power
|
||||
q--;
|
||||
}
|
||||
|
||||
current *= current; // value^i -> value^(2*i)
|
||||
q >>= 1;
|
||||
}
|
||||
|
||||
return prod;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Analogy of Math.Log10
|
||||
/// </summary>
|
||||
/// <param name="x"></param>
|
||||
/// <returns></returns>
|
||||
public static decimal Log10(decimal x)
|
||||
{
|
||||
return Log(x) * Log10Inv;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Analogy of Math.Log
|
||||
/// </summary>
|
||||
/// <param name="x"></param>
|
||||
/// <returns></returns>
|
||||
public static decimal Log(decimal x)
|
||||
{
|
||||
if (x <= Zero)
|
||||
{
|
||||
throw new ArgumentException("x must be greater than zero");
|
||||
}
|
||||
var count = 0;
|
||||
while (x >= One)
|
||||
{
|
||||
x *= Einv;
|
||||
count++;
|
||||
}
|
||||
while (x <= Einv)
|
||||
{
|
||||
x *= E;
|
||||
count--;
|
||||
}
|
||||
x--;
|
||||
if (x == Zero) return count;
|
||||
var result = Zero;
|
||||
var iteration = 0;
|
||||
var y = One;
|
||||
var cacheResult = result - One;
|
||||
while (cacheResult != result && iteration < MaxIteration)
|
||||
{
|
||||
iteration++;
|
||||
cacheResult = result;
|
||||
y *= -x;
|
||||
result += y / iteration;
|
||||
}
|
||||
return count - result;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Analogy of Math.Cos
|
||||
/// </summary>
|
||||
/// <param name="x"></param>
|
||||
/// <returns></returns>
|
||||
public static decimal Cos(decimal x)
|
||||
{
|
||||
//truncating to [-2*PI;2*PI]
|
||||
TruncateToPeriodicInterval(ref x);
|
||||
|
||||
// now x in (-2pi,2pi)
|
||||
if (x >= PI && x <= PIx2)
|
||||
{
|
||||
return -Cos(x - PI);
|
||||
}
|
||||
if (x >= -PIx2 && x <= -PI)
|
||||
{
|
||||
return -Cos(x + PI);
|
||||
}
|
||||
|
||||
x *= x;
|
||||
//y=1-x/2!+x^2/4!-x^3/6!...
|
||||
var xx = -x * Half;
|
||||
var y = One + xx;
|
||||
var cachedY = y - One;//init cache with different value
|
||||
for (var i = 1; cachedY != y && i < MaxIteration; i++)
|
||||
{
|
||||
cachedY = y;
|
||||
decimal factor = i * ((i << 1) + 3) + 1; //2i^2+2i+i+1=2i^2+3i+1
|
||||
factor = -Half / factor;
|
||||
xx *= x * factor;
|
||||
y += xx;
|
||||
}
|
||||
return y;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Analogy of Math.Tan
|
||||
/// </summary>
|
||||
/// <param name="x"></param>
|
||||
/// <returns></returns>
|
||||
public static decimal Tan(decimal x)
|
||||
{
|
||||
var cos = Cos(x);
|
||||
if (cos == Zero) throw new ArgumentException(nameof(x));
|
||||
//calculate sin using cos
|
||||
var sin = CalculateSinFromCos(x, cos);
|
||||
return sin / cos;
|
||||
}
|
||||
/// <summary>
|
||||
/// Helper function for calculating sin(x) from cos(x)
|
||||
/// </summary>
|
||||
/// <param name="x"></param>
|
||||
/// <param name="cos"></param>
|
||||
/// <returns></returns>
|
||||
private static decimal CalculateSinFromCos(decimal x, decimal cos)
|
||||
{
|
||||
var moduleOfSin = Sqrt(One - (cos * cos));
|
||||
var sineIsPositive = IsSignOfSinePositive(x);
|
||||
if (sineIsPositive) return moduleOfSin;
|
||||
return -moduleOfSin;
|
||||
}
|
||||
/// <summary>
|
||||
/// Analogy of Math.Sin
|
||||
/// </summary>
|
||||
/// <param name="x"></param>
|
||||
/// <returns></returns>
|
||||
public static decimal Sin(decimal x)
|
||||
{
|
||||
var cos = Cos(x);
|
||||
return CalculateSinFromCos(x, cos);
|
||||
}
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// Truncates to [-2*PI;2*PI]
|
||||
/// </summary>
|
||||
/// <param name="x"></param>
|
||||
private static void TruncateToPeriodicInterval(ref decimal x)
|
||||
{
|
||||
while (x >= PIx2)
|
||||
{
|
||||
var divide = Math.Abs(decimal.ToInt32(x / PIx2));
|
||||
x -= divide * PIx2;
|
||||
}
|
||||
|
||||
while (x <= -PIx2)
|
||||
{
|
||||
var divide = Math.Abs(decimal.ToInt32(x / PIx2));
|
||||
x += divide * PIx2;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
private static bool IsSignOfSinePositive(decimal x)
|
||||
{
|
||||
//truncating to [-2*PI;2*PI]
|
||||
TruncateToPeriodicInterval(ref x);
|
||||
|
||||
//now x in [-2*PI;2*PI]
|
||||
if (x >= -PIx2 && x <= -PI) return true;
|
||||
if (x >= -PI && x <= Zero) return false;
|
||||
if (x >= Zero && x <= PI) return true;
|
||||
if (x >= PI && x <= PIx2) return false;
|
||||
|
||||
//will not be reached
|
||||
throw new ArgumentException(nameof(x));
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Analogy of Math.Sqrt
|
||||
/// </summary>
|
||||
/// <param name="x"></param>
|
||||
/// <param name="epsilon">lasts iteration while error less than this epsilon</param>
|
||||
/// <returns></returns>
|
||||
public static decimal Sqrt(decimal x, decimal epsilon = Zero)
|
||||
{
|
||||
if (x < Zero) throw new OverflowException("Cannot calculate square root from a negative number");
|
||||
//initial approximation
|
||||
decimal current = (decimal)Math.Sqrt((double)x), previous;
|
||||
do
|
||||
{
|
||||
previous = current;
|
||||
if (previous == Zero) return Zero;
|
||||
current = (previous + x / previous) * Half;
|
||||
} while (Abs(previous - current) > epsilon);
|
||||
return current;
|
||||
}
|
||||
/// <summary>
|
||||
/// Analogy of Math.Sinh
|
||||
/// </summary>
|
||||
/// <param name="x"></param>
|
||||
/// <returns></returns>
|
||||
public static decimal Sinh(decimal x)
|
||||
{
|
||||
var y = Exp(x);
|
||||
var yy = One / y;
|
||||
return (y - yy) * Half;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Analogy of Math.Cosh
|
||||
/// </summary>
|
||||
/// <param name="x"></param>
|
||||
/// <returns></returns>
|
||||
public static decimal Cosh(decimal x)
|
||||
{
|
||||
var y = Exp(x);
|
||||
var yy = One / y;
|
||||
return (y + yy) * Half;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Analogy of Math.Sign
|
||||
/// </summary>
|
||||
/// <param name="x"></param>
|
||||
/// <returns></returns>
|
||||
public static int Sign(decimal x)
|
||||
{
|
||||
return x < Zero ? -1 : (x > Zero ? 1 : 0);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Analogy of Math.Tanh
|
||||
/// </summary>
|
||||
/// <param name="x"></param>
|
||||
/// <returns></returns>
|
||||
public static decimal Tanh(decimal x)
|
||||
{
|
||||
var y = Exp(x);
|
||||
var yy = One / y;
|
||||
return (y - yy) / (y + yy);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Analogy of Math.Abs
|
||||
/// </summary>
|
||||
/// <param name="x"></param>
|
||||
/// <returns></returns>
|
||||
public static decimal Abs(decimal x)
|
||||
{
|
||||
if (x <= Zero)
|
||||
{
|
||||
return -x;
|
||||
}
|
||||
return x;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Analogy of Math.Asin
|
||||
/// </summary>
|
||||
/// <param name="x"></param>
|
||||
/// <returns></returns>
|
||||
public static decimal Asin(decimal x)
|
||||
{
|
||||
if (x > One || x < -One)
|
||||
{
|
||||
throw new ArgumentException("x must be in [-1,1]");
|
||||
}
|
||||
//known values
|
||||
if (x == Zero) return Zero;
|
||||
if (x == One) return PIdiv2;
|
||||
//asin function is odd function
|
||||
if (x < Zero) return -Asin(-x);
|
||||
|
||||
//my optimize trick here
|
||||
|
||||
// used a math formula to speed up :
|
||||
// asin(x)=0.5*(pi/2-asin(1-2*x*x))
|
||||
// if x>=0 is true
|
||||
|
||||
var newX = One - 2 * x * x;
|
||||
|
||||
//for calculating new value near to zero than current
|
||||
//because we gain more speed with values near to zero
|
||||
if (Abs(x) > Abs(newX))
|
||||
{
|
||||
var t = Asin(newX);
|
||||
return Half * (PIdiv2 - t);
|
||||
}
|
||||
var y = Zero;
|
||||
var result = x;
|
||||
decimal cachedResult;
|
||||
var i = 1;
|
||||
y += result;
|
||||
var xx = x * x;
|
||||
do
|
||||
{
|
||||
cachedResult = result;
|
||||
result *= xx * (One - Half / (i));
|
||||
y += result / ((i << 1) + 1);
|
||||
i++;
|
||||
} while (cachedResult != result);
|
||||
return y;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Analogy of Math.Atan
|
||||
/// </summary>
|
||||
/// <param name="x"></param>
|
||||
/// <returns></returns>
|
||||
public static decimal Atan(decimal x)
|
||||
{
|
||||
if (x == Zero) return Zero;
|
||||
if (x == One) return PIdiv4;
|
||||
return Asin(x / Sqrt(One + x * x));
|
||||
}
|
||||
/// <summary>
|
||||
/// Analogy of Math.Acos
|
||||
/// </summary>
|
||||
/// <param name="x"></param>
|
||||
/// <returns></returns>
|
||||
public static decimal Acos(decimal x)
|
||||
{
|
||||
if (x == Zero) return PIdiv2;
|
||||
if (x == One) return Zero;
|
||||
if (x < Zero) return PI - Acos(-x);
|
||||
return PIdiv2 - Asin(x);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Analogy of Math.Atan2
|
||||
/// for more see this
|
||||
/// <seealso cref="http://i.imgur.com/TRLjs8R.png"/>
|
||||
/// </summary>
|
||||
/// <param name="y"></param>
|
||||
/// <param name="x"></param>
|
||||
/// <returns></returns>
|
||||
public static decimal Atan2(decimal y, decimal x)
|
||||
{
|
||||
if (x > Zero)
|
||||
{
|
||||
return Atan(y / x);
|
||||
}
|
||||
if (x < Zero && y >= Zero)
|
||||
{
|
||||
return Atan(y / x) + PI;
|
||||
}
|
||||
if (x < Zero && y < Zero)
|
||||
{
|
||||
return Atan(y / x) - PI;
|
||||
}
|
||||
if (x == Zero && y > Zero)
|
||||
{
|
||||
return PIdiv2;
|
||||
}
|
||||
if (x == Zero && y < Zero)
|
||||
{
|
||||
return -PIdiv2;
|
||||
}
|
||||
throw new ArgumentException("invalid atan2 arguments");
|
||||
}
|
||||
}
|
||||
928
MathApp/MathApp/MathApp.Tests/EigenvalueDecomposition.cs
Normal file
928
MathApp/MathApp/MathApp.Tests/EigenvalueDecomposition.cs
Normal file
|
|
@ -0,0 +1,928 @@
|
|||
using System;
|
||||
using System.Runtime.InteropServices;
|
||||
using System.Runtime.Serialization;
|
||||
|
||||
namespace DotNetMatrix
|
||||
{
|
||||
[Serializable]
|
||||
public class EigenvalueDecomposition : System.Runtime.Serialization.ISerializable
|
||||
{
|
||||
private int n;
|
||||
private bool issymmetric;
|
||||
private decimal[] d, e;
|
||||
private decimal[][] V;
|
||||
private decimal[][] H;
|
||||
private decimal[] ort;
|
||||
|
||||
private void tred2()
|
||||
{
|
||||
for (int j = 0; j < n; j++)
|
||||
{
|
||||
d[j] = V[n - 1][j];
|
||||
}
|
||||
|
||||
for (int i = n - 1; i > 0; i--)
|
||||
{
|
||||
decimal scale = 0.0m;
|
||||
decimal h = 0.0m;
|
||||
for (int k = 0; k < i; k++)
|
||||
{
|
||||
scale = scale + System.Math.Abs(d[k]);
|
||||
}
|
||||
if (scale == 0.0m)
|
||||
{
|
||||
e[i] = d[i - 1];
|
||||
for (int j = 0; j < i; j++)
|
||||
{
|
||||
d[j] = V[i - 1][j];
|
||||
V[i][j] = 0.0m;
|
||||
V[j][i] = 0.0m;
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
for (int k = 0; k < i; k++)
|
||||
{
|
||||
d[k] /= scale;
|
||||
h += d[k] * d[k];
|
||||
}
|
||||
decimal f = d[i - 1];
|
||||
decimal g = (decimal)DecimalMath.Sqrt(h);
|
||||
if (f > 0)
|
||||
{
|
||||
g = - g;
|
||||
}
|
||||
e[i] = scale * g;
|
||||
h = h - f * g;
|
||||
d[i - 1] = f - g;
|
||||
for (int j = 0; j < i; j++)
|
||||
{
|
||||
e[j] = 0.0m;
|
||||
}
|
||||
|
||||
for (int j = 0; j < i; j++)
|
||||
{
|
||||
f = d[j];
|
||||
V[j][i] = f;
|
||||
g = e[j] + V[j][j] * f;
|
||||
for (int k = j + 1; k <= i - 1; k++)
|
||||
{
|
||||
g += V[k][j] * d[k];
|
||||
e[k] += V[k][j] * f;
|
||||
}
|
||||
e[j] = g;
|
||||
}
|
||||
f = 0.0m;
|
||||
for (int j = 0; j < i; j++)
|
||||
{
|
||||
e[j] /= h;
|
||||
f += e[j] * d[j];
|
||||
}
|
||||
decimal hh = f / (h + h);
|
||||
for (int j = 0; j < i; j++)
|
||||
{
|
||||
e[j] -= hh * d[j];
|
||||
}
|
||||
for (int j = 0; j < i; j++)
|
||||
{
|
||||
f = d[j];
|
||||
g = e[j];
|
||||
for (int k = j; k <= i - 1; k++)
|
||||
{
|
||||
V[k][j] -= (f * e[k] + g * d[k]);
|
||||
}
|
||||
d[j] = V[i - 1][j];
|
||||
V[i][j] = 0.0m;
|
||||
}
|
||||
}
|
||||
d[i] = h;
|
||||
}
|
||||
|
||||
for (int i = 0; i < n - 1; i++)
|
||||
{
|
||||
V[n - 1][i] = V[i][i];
|
||||
V[i][i] = 1.0m;
|
||||
decimal h = d[i + 1];
|
||||
if (h != 0.0m)
|
||||
{
|
||||
for (int k = 0; k <= i; k++)
|
||||
{
|
||||
d[k] = V[k][i + 1] / h;
|
||||
}
|
||||
for (int j = 0; j <= i; j++)
|
||||
{
|
||||
decimal g = 0.0m;
|
||||
for (int k = 0; k <= i; k++)
|
||||
{
|
||||
g += V[k][i + 1] * V[k][j];
|
||||
}
|
||||
for (int k = 0; k <= i; k++)
|
||||
{
|
||||
V[k][j] -= g * d[k];
|
||||
}
|
||||
}
|
||||
}
|
||||
for (int k = 0; k <= i; k++)
|
||||
{
|
||||
V[k][i + 1] = 0.0m;
|
||||
}
|
||||
}
|
||||
for (int j = 0; j < n; j++)
|
||||
{
|
||||
d[j] = V[n - 1][j];
|
||||
V[n - 1][j] = 0.0m;
|
||||
}
|
||||
V[n - 1][n - 1] = 1.0m;
|
||||
e[0] = 0.0m;
|
||||
}
|
||||
|
||||
private void tql2()
|
||||
{
|
||||
for (int i = 1; i < n; i++)
|
||||
{
|
||||
e[i - 1] = e[i];
|
||||
}
|
||||
e[n - 1] = 0.0m;
|
||||
|
||||
decimal f = 0.0m;
|
||||
decimal tst1 = 0.0m;
|
||||
decimal eps = (decimal)DecimalMath.Pow(2.0m, - 52.0m);
|
||||
for (int l = 0; l < n; l++)
|
||||
{
|
||||
tst1 = System.Math.Max(tst1, System.Math.Abs(d[l]) + System.Math.Abs(e[l]));
|
||||
int m = l;
|
||||
while (m < n)
|
||||
{
|
||||
if (System.Math.Abs(e[m]) <= eps * tst1)
|
||||
{
|
||||
break;
|
||||
}
|
||||
m++;
|
||||
}
|
||||
|
||||
if (m > l)
|
||||
{
|
||||
int iter = 0;
|
||||
do
|
||||
{
|
||||
iter = iter + 1;
|
||||
decimal g = d[l];
|
||||
decimal p = (d[l + 1] - g) / (2.0m * e[l]);
|
||||
decimal r = Maths.Hypot(p, 1.0m);
|
||||
if (p < 0)
|
||||
{
|
||||
r = - r;
|
||||
}
|
||||
d[l] = e[l] / (p + r);
|
||||
d[l + 1] = e[l] * (p + r);
|
||||
decimal dl1 = d[l + 1];
|
||||
decimal h = g - d[l];
|
||||
for (int i = l + 2; i < n; i++)
|
||||
{
|
||||
d[i] -= h;
|
||||
}
|
||||
f = f + h;
|
||||
|
||||
p = d[m];
|
||||
decimal c = 1.0m;
|
||||
decimal c2 = c;
|
||||
decimal c3 = c;
|
||||
decimal el1 = e[l + 1];
|
||||
decimal s = 0.0m;
|
||||
decimal s2 = 0.0m;
|
||||
for (int i = m - 1; i >= l; i--)
|
||||
{
|
||||
c3 = c2;
|
||||
c2 = c;
|
||||
s2 = s;
|
||||
g = c * e[i];
|
||||
h = c * p;
|
||||
r = Maths.Hypot(p, e[i]);
|
||||
e[i + 1] = s * r;
|
||||
s = e[i] / r;
|
||||
c = p / r;
|
||||
p = c * d[i] - s * g;
|
||||
d[i + 1] = h + s * (c * g + s * d[i]);
|
||||
|
||||
for (int k = 0; k < n; k++)
|
||||
{
|
||||
h = V[k][i + 1];
|
||||
V[k][i + 1] = s * V[k][i] + c * h;
|
||||
V[k][i] = c * V[k][i] - s * h;
|
||||
}
|
||||
}
|
||||
p = (- s) * s2 * c3 * el1 * e[l] / dl1;
|
||||
e[l] = s * p;
|
||||
d[l] = c * p;
|
||||
}
|
||||
while (System.Math.Abs(e[l]) > eps * tst1);
|
||||
}
|
||||
d[l] = d[l] + f;
|
||||
e[l] = 0.0m;
|
||||
}
|
||||
|
||||
for (int i = 0; i < n - 1; i++)
|
||||
{
|
||||
int k = i;
|
||||
decimal p = d[i];
|
||||
for (int j = i + 1; j < n; j++)
|
||||
{
|
||||
if (d[j] < p)
|
||||
{
|
||||
k = j;
|
||||
p = d[j];
|
||||
}
|
||||
}
|
||||
if (k != i)
|
||||
{
|
||||
d[k] = d[i];
|
||||
d[i] = p;
|
||||
for (int j = 0; j < n; j++)
|
||||
{
|
||||
p = V[j][i];
|
||||
V[j][i] = V[j][k];
|
||||
V[j][k] = p;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private void orthes()
|
||||
{
|
||||
int low = 0;
|
||||
int high = n - 1;
|
||||
|
||||
for (int m = low + 1; m <= high - 1; m++)
|
||||
{
|
||||
decimal scale = 0.0m;
|
||||
for (int i = m; i <= high; i++)
|
||||
{
|
||||
scale = scale + System.Math.Abs(H[i][m - 1]);
|
||||
}
|
||||
if (scale != 0.0m)
|
||||
{
|
||||
decimal h = 0.0m;
|
||||
for (int i = high; i >= m; i--)
|
||||
{
|
||||
ort[i] = H[i][m - 1] / scale;
|
||||
h += ort[i] * ort[i];
|
||||
}
|
||||
decimal g = (decimal)DecimalMath.Sqrt(h);
|
||||
if (ort[m] > 0)
|
||||
{
|
||||
g = - g;
|
||||
}
|
||||
h = h - ort[m] * g;
|
||||
ort[m] = ort[m] - g;
|
||||
|
||||
for (int j = m; j < n; j++)
|
||||
{
|
||||
decimal f = 0.0m;
|
||||
for (int i = high; i >= m; i--)
|
||||
{
|
||||
f += ort[i] * H[i][j];
|
||||
}
|
||||
f = f / h;
|
||||
for (int i = m; i <= high; i++)
|
||||
{
|
||||
H[i][j] -= f * ort[i];
|
||||
}
|
||||
}
|
||||
|
||||
for (int i = 0; i <= high; i++)
|
||||
{
|
||||
decimal f = 0.0m;
|
||||
for (int j = high; j >= m; j--)
|
||||
{
|
||||
f += ort[j] * H[i][j];
|
||||
}
|
||||
f = f / h;
|
||||
for (int j = m; j <= high; j++)
|
||||
{
|
||||
H[i][j] -= f * ort[j];
|
||||
}
|
||||
}
|
||||
ort[m] = scale * ort[m];
|
||||
H[m][m - 1] = scale * g;
|
||||
}
|
||||
}
|
||||
|
||||
for (int i = 0; i < n; i++)
|
||||
{
|
||||
for (int j = 0; j < n; j++)
|
||||
{
|
||||
V[i][j] = (i == j?1.0m:0.0m);
|
||||
}
|
||||
}
|
||||
|
||||
for (int m = high - 1; m >= low + 1; m--)
|
||||
{
|
||||
if (H[m][m - 1] != 0.0m)
|
||||
{
|
||||
for (int i = m + 1; i <= high; i++)
|
||||
{
|
||||
ort[i] = H[i][m - 1];
|
||||
}
|
||||
for (int j = m; j <= high; j++)
|
||||
{
|
||||
decimal g = 0.0m;
|
||||
for (int i = m; i <= high; i++)
|
||||
{
|
||||
g += ort[i] * V[i][j];
|
||||
}
|
||||
g = (g / ort[m]) / H[m][m - 1];
|
||||
for (int i = m; i <= high; i++)
|
||||
{
|
||||
V[i][j] += g * ort[i];
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
[NonSerialized()]
|
||||
private decimal cdivr, cdivi;
|
||||
|
||||
private void cdiv(decimal xr, decimal xi, decimal yr, decimal yi)
|
||||
{
|
||||
decimal r, d;
|
||||
if (System.Math.Abs(yr) > System.Math.Abs(yi))
|
||||
{
|
||||
r = yi / yr;
|
||||
d = yr + r * yi;
|
||||
cdivr = (xr + r * xi) / d;
|
||||
cdivi = (xi - r * xr) / d;
|
||||
}
|
||||
else
|
||||
{
|
||||
r = yr / yi;
|
||||
d = yi + r * yr;
|
||||
cdivr = (r * xr + xi) / d;
|
||||
cdivi = (r * xi - xr) / d;
|
||||
}
|
||||
}
|
||||
|
||||
private void hqr2()
|
||||
{
|
||||
int nn = this.n;
|
||||
int n = nn - 1;
|
||||
int low = 0;
|
||||
int high = nn - 1;
|
||||
decimal eps = (decimal)DecimalMath.Pow(2.0m, - 52.0m);
|
||||
decimal exshift = 0.0m;
|
||||
decimal p = 0, q = 0, r = 0, s = 0, z = 0, t, w, x, y;
|
||||
|
||||
decimal norm = 0.0m;
|
||||
for (int i = 0; i < nn; i++)
|
||||
{
|
||||
if (i < low | i > high)
|
||||
{
|
||||
d[i] = H[i][i];
|
||||
e[i] = 0.0m;
|
||||
}
|
||||
for (int j = System.Math.Max(i - 1, 0); j < nn; j++)
|
||||
{
|
||||
norm = norm + System.Math.Abs(H[i][j]);
|
||||
}
|
||||
}
|
||||
|
||||
int iter = 0;
|
||||
while (n >= low)
|
||||
{
|
||||
int l = n;
|
||||
while (l > low)
|
||||
{
|
||||
s = System.Math.Abs(H[l - 1][l - 1]) + System.Math.Abs(H[l][l]);
|
||||
if (s == 0.0m)
|
||||
{
|
||||
s = norm;
|
||||
}
|
||||
if (System.Math.Abs(H[l][l - 1]) < eps * s)
|
||||
{
|
||||
break;
|
||||
}
|
||||
l--;
|
||||
}
|
||||
|
||||
if (l == n)
|
||||
{
|
||||
H[n][n] = H[n][n] + exshift;
|
||||
d[n] = H[n][n];
|
||||
e[n] = 0.0m;
|
||||
n--;
|
||||
iter = 0;
|
||||
}
|
||||
else if (l == n - 1)
|
||||
{
|
||||
w = H[n][n - 1] * H[n - 1][n];
|
||||
p = (H[n - 1][n - 1] - H[n][n]) / 2.0m;
|
||||
q = p * p + w;
|
||||
z = (decimal)DecimalMath.Sqrt(System.Math.Abs(q));
|
||||
H[n][n] = H[n][n] + exshift;
|
||||
H[n - 1][n - 1] = H[n - 1][n - 1] + exshift;
|
||||
x = H[n][n];
|
||||
|
||||
if (q >= 0)
|
||||
{
|
||||
if (p >= 0)
|
||||
{
|
||||
z = p + z;
|
||||
}
|
||||
else
|
||||
{
|
||||
z = p - z;
|
||||
}
|
||||
d[n - 1] = x + z;
|
||||
d[n] = d[n - 1];
|
||||
if (z != 0.0m)
|
||||
{
|
||||
d[n] = x - w / z;
|
||||
}
|
||||
e[n - 1] = 0.0m;
|
||||
e[n] = 0.0m;
|
||||
x = H[n][n - 1];
|
||||
s = System.Math.Abs(x) + System.Math.Abs(z);
|
||||
p = x / s;
|
||||
q = z / s;
|
||||
r = (decimal)DecimalMath.Sqrt(p * p + q * q);
|
||||
p = p / r;
|
||||
q = q / r;
|
||||
|
||||
for (int j = n - 1; j < nn; j++)
|
||||
{
|
||||
z = H[n - 1][j];
|
||||
H[n - 1][j] = q * z + p * H[n][j];
|
||||
H[n][j] = q * H[n][j] - p * z;
|
||||
}
|
||||
|
||||
for (int i = 0; i <= n; i++)
|
||||
{
|
||||
z = H[i][n - 1];
|
||||
H[i][n - 1] = q * z + p * H[i][n];
|
||||
H[i][n] = q * H[i][n] - p * z;
|
||||
}
|
||||
|
||||
for (int i = low; i <= high; i++)
|
||||
{
|
||||
z = V[i][n - 1];
|
||||
V[i][n - 1] = q * z + p * V[i][n];
|
||||
V[i][n] = q * V[i][n] - p * z;
|
||||
}
|
||||
|
||||
}
|
||||
else
|
||||
{
|
||||
d[n - 1] = x + p;
|
||||
d[n] = x + p;
|
||||
e[n - 1] = z;
|
||||
e[n] = - z;
|
||||
}
|
||||
n = n - 2;
|
||||
iter = 0;
|
||||
|
||||
}
|
||||
else
|
||||
{
|
||||
x = H[n][n];
|
||||
y = 0.0m;
|
||||
w = 0.0m;
|
||||
if (l < n)
|
||||
{
|
||||
y = H[n - 1][n - 1];
|
||||
w = H[n][n - 1] * H[n - 1][n];
|
||||
}
|
||||
|
||||
if (iter == 10)
|
||||
{
|
||||
exshift += x;
|
||||
for (int i = low; i <= n; i++)
|
||||
{
|
||||
H[i][i] -= x;
|
||||
}
|
||||
s = System.Math.Abs(H[n][n - 1]) + System.Math.Abs(H[n - 1][n - 2]);
|
||||
x = y = 0.75m * s;
|
||||
w = (- 0.4375m) * s * s;
|
||||
}
|
||||
|
||||
if (iter == 30)
|
||||
{
|
||||
s = (y - x) / 2.0m;
|
||||
s = s * s + w;
|
||||
if (s > 0)
|
||||
{
|
||||
s = (decimal)DecimalMath.Sqrt(s);
|
||||
if (y < x)
|
||||
{
|
||||
s = - s;
|
||||
}
|
||||
s = x - w / ((y - x) / 2.0m + s);
|
||||
for (int i = low; i <= n; i++)
|
||||
{
|
||||
H[i][i] -= s;
|
||||
}
|
||||
exshift += s;
|
||||
x = y = w = 0.964m;
|
||||
}
|
||||
}
|
||||
|
||||
iter = iter + 1;
|
||||
|
||||
int m = n - 2;
|
||||
while (m >= l)
|
||||
{
|
||||
z = H[m][m];
|
||||
r = x - z;
|
||||
s = y - z;
|
||||
p = (r * s - w) / H[m + 1][m] + H[m][m + 1];
|
||||
q = H[m + 1][m + 1] - z - r - s;
|
||||
r = H[m + 2][m + 1];
|
||||
s = System.Math.Abs(p) + System.Math.Abs(q) + System.Math.Abs(r);
|
||||
p = p / s;
|
||||
q = q / s;
|
||||
r = r / s;
|
||||
if (m == l)
|
||||
{
|
||||
break;
|
||||
}
|
||||
if (System.Math.Abs(H[m][m - 1]) * (System.Math.Abs(q) + System.Math.Abs(r)) < eps * (System.Math.Abs(p) * (System.Math.Abs(H[m - 1][m - 1]) + System.Math.Abs(z) + System.Math.Abs(H[m + 1][m + 1]))))
|
||||
{
|
||||
break;
|
||||
}
|
||||
m--;
|
||||
}
|
||||
|
||||
for (int i = m + 2; i <= n; i++)
|
||||
{
|
||||
H[i][i - 2] = 0.0m;
|
||||
if (i > m + 2)
|
||||
{
|
||||
H[i][i - 3] = 0.0m;
|
||||
}
|
||||
}
|
||||
|
||||
for (int k = m; k <= n - 1; k++)
|
||||
{
|
||||
bool notlast = (k != n - 1);
|
||||
if (k != m)
|
||||
{
|
||||
p = H[k][k - 1];
|
||||
q = H[k + 1][k - 1];
|
||||
r = (notlast?H[k + 2][k - 1]:0.0m);
|
||||
x = System.Math.Abs(p) + System.Math.Abs(q) + System.Math.Abs(r);
|
||||
if (x != 0.0m)
|
||||
{
|
||||
p = p / x;
|
||||
q = q / x;
|
||||
r = r / x;
|
||||
}
|
||||
}
|
||||
if (x == 0.0m)
|
||||
{
|
||||
break;
|
||||
}
|
||||
s = (decimal)DecimalMath.Sqrt(p * p + q * q + r * r);
|
||||
if (p < 0)
|
||||
{
|
||||
s = - s;
|
||||
}
|
||||
if (s != 0)
|
||||
{
|
||||
if (k != m)
|
||||
{
|
||||
H[k][k - 1] = (- s) * x;
|
||||
}
|
||||
else if (l != m)
|
||||
{
|
||||
H[k][k - 1] = - H[k][k - 1];
|
||||
}
|
||||
p = p + s;
|
||||
x = p / s;
|
||||
y = q / s;
|
||||
z = r / s;
|
||||
q = q / p;
|
||||
r = r / p;
|
||||
|
||||
for (int j = k; j < nn; j++)
|
||||
{
|
||||
p = H[k][j] + q * H[k + 1][j];
|
||||
if (notlast)
|
||||
{
|
||||
p = p + r * H[k + 2][j];
|
||||
H[k + 2][j] = H[k + 2][j] - p * z;
|
||||
}
|
||||
H[k][j] = H[k][j] - p * x;
|
||||
H[k + 1][j] = H[k + 1][j] - p * y;
|
||||
}
|
||||
|
||||
for (int i = 0; i <= System.Math.Min(n, k + 3); i++)
|
||||
{
|
||||
p = x * H[i][k] + y * H[i][k + 1];
|
||||
if (notlast)
|
||||
{
|
||||
p = p + z * H[i][k + 2];
|
||||
H[i][k + 2] = H[i][k + 2] - p * r;
|
||||
}
|
||||
H[i][k] = H[i][k] - p;
|
||||
H[i][k + 1] = H[i][k + 1] - p * q;
|
||||
}
|
||||
|
||||
for (int i = low; i <= high; i++)
|
||||
{
|
||||
p = x * V[i][k] + y * V[i][k + 1];
|
||||
if (notlast)
|
||||
{
|
||||
p = p + z * V[i][k + 2];
|
||||
V[i][k + 2] = V[i][k + 2] - p * r;
|
||||
}
|
||||
V[i][k] = V[i][k] - p;
|
||||
V[i][k + 1] = V[i][k + 1] - p * q;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (norm == 0.0m)
|
||||
{
|
||||
return ;
|
||||
}
|
||||
|
||||
for (n = nn - 1; n >= 0; n--)
|
||||
{
|
||||
p = d[n];
|
||||
q = e[n];
|
||||
|
||||
if (q == 0)
|
||||
{
|
||||
int l = n;
|
||||
H[n][n] = 1.0m;
|
||||
for (int i = n - 1; i >= 0; i--)
|
||||
{
|
||||
w = H[i][i] - p;
|
||||
r = 0.0m;
|
||||
for (int j = l; j <= n; j++)
|
||||
{
|
||||
r = r + H[i][j] * H[j][n];
|
||||
}
|
||||
if (e[i] < 0.0m)
|
||||
{
|
||||
z = w;
|
||||
s = r;
|
||||
}
|
||||
else
|
||||
{
|
||||
l = i;
|
||||
if (e[i] == 0.0m)
|
||||
{
|
||||
if (w != 0.0m)
|
||||
{
|
||||
H[i][n] = (- r) / w;
|
||||
}
|
||||
else
|
||||
{
|
||||
H[i][n] = (- r) / (eps * norm);
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
x = H[i][i + 1];
|
||||
y = H[i + 1][i];
|
||||
q = (d[i] - p) * (d[i] - p) + e[i] * e[i];
|
||||
t = (x * s - z * r) / q;
|
||||
H[i][n] = t;
|
||||
if (System.Math.Abs(x) > System.Math.Abs(z))
|
||||
{
|
||||
H[i + 1][n] = (- r - w * t) / x;
|
||||
}
|
||||
else
|
||||
{
|
||||
H[i + 1][n] = (- s - y * t) / z;
|
||||
}
|
||||
}
|
||||
|
||||
t = System.Math.Abs(H[i][n]);
|
||||
if ((eps * t) * t > 1)
|
||||
{
|
||||
for (int j = i; j <= n; j++)
|
||||
{
|
||||
H[j][n] = H[j][n] / t;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
else if (q < 0)
|
||||
{
|
||||
int l = n - 1;
|
||||
if (System.Math.Abs(H[n][n - 1]) > System.Math.Abs(H[n - 1][n]))
|
||||
{
|
||||
H[n - 1][n - 1] = q / H[n][n - 1];
|
||||
H[n - 1][n] = (- (H[n][n] - p)) / H[n][n - 1];
|
||||
}
|
||||
else
|
||||
{
|
||||
cdiv(0.0m, - H[n - 1][n], H[n - 1][n - 1] - p, q);
|
||||
H[n - 1][n - 1] = cdivr;
|
||||
H[n - 1][n] = cdivi;
|
||||
}
|
||||
H[n][n - 1] = 0.0m;
|
||||
H[n][n] = 1.0m;
|
||||
for (int i = n - 2; i >= 0; i--)
|
||||
{
|
||||
decimal ra, sa, vr, vi;
|
||||
ra = 0.0m;
|
||||
sa = 0.0m;
|
||||
for (int j = l; j <= n; j++)
|
||||
{
|
||||
ra = ra + H[i][j] * H[j][n - 1];
|
||||
sa = sa + H[i][j] * H[j][n];
|
||||
}
|
||||
w = H[i][i] - p;
|
||||
|
||||
if (e[i] < 0.0m)
|
||||
{
|
||||
z = w;
|
||||
r = ra;
|
||||
s = sa;
|
||||
}
|
||||
else
|
||||
{
|
||||
l = i;
|
||||
if (e[i] == 0)
|
||||
{
|
||||
cdiv(- ra, - sa, w, q);
|
||||
H[i][n - 1] = cdivr;
|
||||
H[i][n] = cdivi;
|
||||
}
|
||||
else
|
||||
{
|
||||
x = H[i][i + 1];
|
||||
y = H[i + 1][i];
|
||||
vr = (d[i] - p) * (d[i] - p) + e[i] * e[i] - q * q;
|
||||
vi = (d[i] - p) * 2.0m * q;
|
||||
if (vr == 0.0m & vi == 0.0m)
|
||||
{
|
||||
vr = eps * norm * (System.Math.Abs(w) + System.Math.Abs(q) + System.Math.Abs(x) + System.Math.Abs(y) + System.Math.Abs(z));
|
||||
}
|
||||
cdiv(x * r - z * ra + q * sa, x * s - z * sa - q * ra, vr, vi);
|
||||
H[i][n - 1] = cdivr;
|
||||
H[i][n] = cdivi;
|
||||
if (System.Math.Abs(x) > (System.Math.Abs(z) + System.Math.Abs(q)))
|
||||
{
|
||||
H[i + 1][n - 1] = (- ra - w * H[i][n - 1] + q * H[i][n]) / x;
|
||||
H[i + 1][n] = (- sa - w * H[i][n] - q * H[i][n - 1]) / x;
|
||||
}
|
||||
else
|
||||
{
|
||||
cdiv(- r - y * H[i][n - 1], - s - y * H[i][n], z, q);
|
||||
H[i + 1][n - 1] = cdivr;
|
||||
H[i + 1][n] = cdivi;
|
||||
}
|
||||
}
|
||||
|
||||
t = System.Math.Max(System.Math.Abs(H[i][n - 1]), System.Math.Abs(H[i][n]));
|
||||
if ((eps * t) * t > 1)
|
||||
{
|
||||
for (int j = i; j <= n; j++)
|
||||
{
|
||||
H[j][n - 1] = H[j][n - 1] / t;
|
||||
H[j][n] = H[j][n] / t;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
for (int i = 0; i < nn; i++)
|
||||
{
|
||||
if (i < low | i > high)
|
||||
{
|
||||
for (int j = i; j < nn; j++)
|
||||
{
|
||||
V[i][j] = H[i][j];
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
for (int j = nn - 1; j >= low; j--)
|
||||
{
|
||||
for (int i = low; i <= high; i++)
|
||||
{
|
||||
z = 0.0m;
|
||||
for (int k = low; k <= System.Math.Min(j, high); k++)
|
||||
{
|
||||
z = z + V[i][k] * H[k][j];
|
||||
}
|
||||
V[i][j] = z;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public EigenvalueDecomposition(GeneralMatrix Arg)
|
||||
{
|
||||
decimal[][] A = Arg.Array;
|
||||
n = Arg.ColumnDimension;
|
||||
V = new decimal[n][];
|
||||
for (int i = 0; i < n; i++)
|
||||
{
|
||||
V[i] = new decimal[n];
|
||||
}
|
||||
d = new decimal[n];
|
||||
e = new decimal[n];
|
||||
|
||||
issymmetric = true;
|
||||
for (int j = 0; (j < n) & issymmetric; j++)
|
||||
{
|
||||
for (int i = 0; (i < n) & issymmetric; i++)
|
||||
{
|
||||
issymmetric = (A[i][j] == A[j][i]);
|
||||
}
|
||||
}
|
||||
|
||||
if (issymmetric)
|
||||
{
|
||||
for (int i = 0; i < n; i++)
|
||||
{
|
||||
for (int j = 0; j < n; j++)
|
||||
{
|
||||
V[i][j] = A[i][j];
|
||||
}
|
||||
}
|
||||
|
||||
tred2();
|
||||
tql2();
|
||||
}
|
||||
else
|
||||
{
|
||||
H = new decimal[n][];
|
||||
for (int i2 = 0; i2 < n; i2++)
|
||||
{
|
||||
H[i2] = new decimal[n];
|
||||
}
|
||||
ort = new decimal[n];
|
||||
|
||||
for (int j = 0; j < n; j++)
|
||||
{
|
||||
for (int i = 0; i < n; i++)
|
||||
{
|
||||
H[i][j] = A[i][j];
|
||||
}
|
||||
}
|
||||
|
||||
orthes();
|
||||
hqr2();
|
||||
}
|
||||
}
|
||||
|
||||
virtual public decimal[] RealEigenvalues
|
||||
{
|
||||
get
|
||||
{
|
||||
return d;
|
||||
}
|
||||
}
|
||||
virtual public decimal[] ImagEigenvalues
|
||||
{
|
||||
get
|
||||
{
|
||||
return e;
|
||||
}
|
||||
}
|
||||
virtual public GeneralMatrix D
|
||||
{
|
||||
get
|
||||
{
|
||||
GeneralMatrix X = new GeneralMatrix(n, n);
|
||||
decimal[][] D = X.Array;
|
||||
for (int i = 0; i < n; i++)
|
||||
{
|
||||
for (int j = 0; j < n; j++)
|
||||
{
|
||||
D[i][j] = 0.0m;
|
||||
}
|
||||
D[i][i] = d[i];
|
||||
if (e[i] > 0)
|
||||
{
|
||||
D[i][i + 1] = e[i];
|
||||
}
|
||||
else if (e[i] < 0)
|
||||
{
|
||||
D[i][i - 1] = e[i];
|
||||
}
|
||||
}
|
||||
return X;
|
||||
}
|
||||
}
|
||||
|
||||
public virtual GeneralMatrix GetV()
|
||||
{
|
||||
return new GeneralMatrix(V, n, n);
|
||||
}
|
||||
|
||||
void ISerializable.GetObjectData(SerializationInfo info, StreamingContext context)
|
||||
{
|
||||
}
|
||||
}
|
||||
}
|
||||
721
MathApp/MathApp/MathApp.Tests/Function.cs
Normal file
721
MathApp/MathApp/MathApp.Tests/Function.cs
Normal file
|
|
@ -0,0 +1,721 @@
|
|||
using System;
|
||||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// </summary>
|
||||
///
|
||||
public enum Type { Variable, Value, UnaryOperator, Operator, Function, Result, Bracket, Comma, Error }
|
||||
public struct Symbol
|
||||
{
|
||||
public string m_name;
|
||||
public decimal m_value;
|
||||
public Type m_type;
|
||||
public override string ToString()
|
||||
{
|
||||
return m_name;
|
||||
}
|
||||
}
|
||||
public delegate Symbol EvaluateFunctionDelegate(string name, params Object[] args);
|
||||
public class Function
|
||||
{
|
||||
public decimal Result
|
||||
{
|
||||
get
|
||||
{
|
||||
return m_result;
|
||||
}
|
||||
}
|
||||
|
||||
public ArrayList Equation
|
||||
{
|
||||
get
|
||||
{
|
||||
return (ArrayList)m_equation.Clone();
|
||||
}
|
||||
}
|
||||
public ArrayList Postfix
|
||||
{
|
||||
get
|
||||
{
|
||||
return (ArrayList)m_postfix.Clone();
|
||||
}
|
||||
}
|
||||
|
||||
public EvaluateFunctionDelegate DefaultFunctionEvaluation
|
||||
{
|
||||
set
|
||||
{
|
||||
m_defaultFunctionEvaluation = value;
|
||||
}
|
||||
}
|
||||
|
||||
public bool Error
|
||||
{
|
||||
get
|
||||
{
|
||||
return m_bError;
|
||||
}
|
||||
}
|
||||
|
||||
public string ErrorDescription
|
||||
{
|
||||
get
|
||||
{
|
||||
return m_sErrorDescription;
|
||||
}
|
||||
}
|
||||
|
||||
public ArrayList Variables
|
||||
{
|
||||
get
|
||||
{
|
||||
ArrayList var = new ArrayList();
|
||||
foreach (Symbol sym in m_equation)
|
||||
{
|
||||
if ((sym.m_type == Type.Variable) && (!var.Contains(sym)))
|
||||
var.Add(sym);
|
||||
}
|
||||
return var;
|
||||
}
|
||||
set
|
||||
{
|
||||
foreach (Symbol sym in value)
|
||||
{
|
||||
for (int i = 0; i < m_postfix.Count; i++)
|
||||
{
|
||||
if ((sym.m_name == ((Symbol)m_postfix[i]).m_name) && (((Symbol)m_postfix[i]).m_type == Type.Variable))
|
||||
{
|
||||
Symbol sym1 = (Symbol)m_postfix[i];
|
||||
sym1.m_value = sym.m_value;
|
||||
m_postfix[i] = sym1;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public Function()
|
||||
{ }
|
||||
|
||||
public void Parse(string equation)
|
||||
{
|
||||
int state = 1;
|
||||
string temp = "";
|
||||
Symbol ctSymbol = new Symbol(); // Initialized to avoid error
|
||||
|
||||
m_bError = false;
|
||||
m_sErrorDescription = "None";
|
||||
|
||||
m_equation.Clear();
|
||||
m_postfix.Clear();
|
||||
|
||||
int nPos = 0;
|
||||
//-- Remove all white spaces from the equation string --
|
||||
equation = equation.Trim();
|
||||
while ((nPos = equation.IndexOf(' ')) != -1)
|
||||
equation = equation.Remove(nPos, 1);
|
||||
|
||||
for (int i = 0; i < equation.Length; i++)
|
||||
{
|
||||
switch (state)
|
||||
{
|
||||
case 1:
|
||||
if (Char.IsNumber(equation[i]))
|
||||
{
|
||||
state = 2;
|
||||
temp += equation[i];
|
||||
}
|
||||
else if (Char.IsLetter(equation[i]))
|
||||
{
|
||||
state = 3;
|
||||
temp += equation[i];
|
||||
}
|
||||
else
|
||||
{
|
||||
ctSymbol.m_name = equation[i].ToString();
|
||||
ctSymbol.m_value = 0;
|
||||
switch (ctSymbol.m_name)
|
||||
{
|
||||
case ",":
|
||||
case ".":
|
||||
ctSymbol.m_type = Type.Comma;
|
||||
break;
|
||||
case "(":
|
||||
case ")":
|
||||
case "[":
|
||||
case "]":
|
||||
case "{":
|
||||
case "}":
|
||||
ctSymbol.m_type = Type.Bracket;
|
||||
break;
|
||||
default:
|
||||
if (m_equation.Count == 0)
|
||||
ctSymbol.m_type = Type.UnaryOperator;
|
||||
else if ((((Symbol)m_equation[m_equation.Count - 1]).m_type == Type.Value) || (((Symbol)m_equation[m_equation.Count - 1]).m_type == Type.Variable)
|
||||
|| (((Symbol)m_equation[m_equation.Count - 1]).m_name == ")") || (((Symbol)m_equation[m_equation.Count - 1]).m_name == "]") || (((Symbol)m_equation[m_equation.Count - 1]).m_name == "}"))
|
||||
ctSymbol.m_type = Type.Operator;
|
||||
else
|
||||
ctSymbol.m_type = Type.UnaryOperator;
|
||||
break;
|
||||
}
|
||||
m_equation.Add(ctSymbol);
|
||||
}
|
||||
break;
|
||||
case 2:
|
||||
if ((Char.IsNumber(equation[i])) || (equation[i] == '.') || (equation[i] == ','))
|
||||
temp += equation[i];
|
||||
else if (!Char.IsLetter(equation[i]))
|
||||
{
|
||||
state = 1;
|
||||
ctSymbol.m_name = temp;
|
||||
ctSymbol.m_value = Decimal.Parse(temp, System.Globalization.CultureInfo.InvariantCulture);
|
||||
ctSymbol.m_type = Type.Value;
|
||||
m_equation.Add(ctSymbol);
|
||||
ctSymbol.m_name = equation[i].ToString();
|
||||
ctSymbol.m_value = 0;
|
||||
switch (ctSymbol.m_name)
|
||||
{
|
||||
case ",":
|
||||
ctSymbol.m_type = Type.Comma;
|
||||
break;
|
||||
case "(":
|
||||
case ")":
|
||||
case "[":
|
||||
case "]":
|
||||
case "{":
|
||||
case "}":
|
||||
ctSymbol.m_type = Type.Bracket;
|
||||
break;
|
||||
default:
|
||||
if (m_equation.Count == 0)
|
||||
ctSymbol.m_type = Type.UnaryOperator;
|
||||
else if ((((Symbol)m_equation[m_equation.Count - 1]).m_type == Type.Value) || (((Symbol)m_equation[m_equation.Count - 1]).m_type == Type.Variable)
|
||||
|| (((Symbol)m_equation[m_equation.Count - 1]).m_name == ")") || (((Symbol)m_equation[m_equation.Count - 1]).m_name == "]") || (((Symbol)m_equation[m_equation.Count - 1]).m_name == "}"))
|
||||
ctSymbol.m_type = Type.Operator;
|
||||
else
|
||||
ctSymbol.m_type = Type.UnaryOperator;
|
||||
break;
|
||||
}
|
||||
m_equation.Add(ctSymbol);
|
||||
temp = "";
|
||||
}
|
||||
break;
|
||||
case 3:
|
||||
if (Char.IsLetterOrDigit(equation[i]))
|
||||
temp += equation[i];
|
||||
else
|
||||
{
|
||||
state = 1;
|
||||
ctSymbol.m_name = temp;
|
||||
ctSymbol.m_value = 0;
|
||||
if (equation[i] == '(')
|
||||
ctSymbol.m_type = Type.Function;
|
||||
else
|
||||
{
|
||||
if (ctSymbol.m_name == "pi")
|
||||
ctSymbol.m_value = DecimalMath.PI;
|
||||
else if (ctSymbol.m_name == "e")
|
||||
ctSymbol.m_value = DecimalMath.E;
|
||||
ctSymbol.m_type = Type.Variable;
|
||||
}
|
||||
m_equation.Add(ctSymbol);
|
||||
ctSymbol.m_name = equation[i].ToString();
|
||||
ctSymbol.m_value = 0;
|
||||
switch (ctSymbol.m_name)
|
||||
{
|
||||
case ",":
|
||||
ctSymbol.m_type = Type.Comma;
|
||||
break;
|
||||
case "(":
|
||||
case ")":
|
||||
case "[":
|
||||
case "]":
|
||||
case "{":
|
||||
case "}":
|
||||
ctSymbol.m_type = Type.Bracket;
|
||||
break;
|
||||
default:
|
||||
if (m_equation.Count == 0)
|
||||
ctSymbol.m_type = Type.UnaryOperator;
|
||||
else if ((((Symbol)m_equation[m_equation.Count - 1]).m_type == Type.Value) || (((Symbol)m_equation[m_equation.Count - 1]).m_type == Type.Variable)
|
||||
|| (((Symbol)m_equation[m_equation.Count - 1]).m_name == ")") || (((Symbol)m_equation[m_equation.Count - 1]).m_name == "]") || (((Symbol)m_equation[m_equation.Count - 1]).m_name == "}"))
|
||||
ctSymbol.m_type = Type.Operator;
|
||||
else
|
||||
ctSymbol.m_type = Type.UnaryOperator;
|
||||
break;
|
||||
}
|
||||
m_equation.Add(ctSymbol);
|
||||
temp = "";
|
||||
}
|
||||
break;
|
||||
}
|
||||
}
|
||||
if (temp != "")
|
||||
{
|
||||
ctSymbol.m_name = temp;
|
||||
if (state == 2)
|
||||
{
|
||||
ctSymbol.m_value = Decimal.Parse(temp, System.Globalization.CultureInfo.InvariantCulture);
|
||||
ctSymbol.m_type = Type.Value;
|
||||
}
|
||||
else
|
||||
{
|
||||
if (ctSymbol.m_name == "pi")
|
||||
ctSymbol.m_value = DecimalMath.PI;
|
||||
else if (ctSymbol.m_name == "e")
|
||||
ctSymbol.m_value = DecimalMath.E;
|
||||
else
|
||||
ctSymbol.m_value = 0;
|
||||
ctSymbol.m_type = Type.Variable;
|
||||
}
|
||||
m_equation.Add(ctSymbol);
|
||||
}
|
||||
}
|
||||
|
||||
public void Infix2Postfix()
|
||||
{
|
||||
Symbol tpSym;
|
||||
Stack tpStack = new Stack();
|
||||
foreach (Symbol sym in m_equation)
|
||||
{
|
||||
if ((sym.m_type == Type.Value) || (sym.m_type == Type.Variable))
|
||||
m_postfix.Add(sym);
|
||||
else if ((sym.m_name == "(") || (sym.m_name == "[") || (sym.m_name == "{"))
|
||||
tpStack.Push(sym);
|
||||
else if ((sym.m_name == ")") || (sym.m_name == "]") || (sym.m_name == "}"))
|
||||
{
|
||||
if (tpStack.Count > 0)
|
||||
{
|
||||
tpSym = (Symbol)tpStack.Pop();
|
||||
while ((tpSym.m_name != "(") && (tpSym.m_name != "[") && (tpSym.m_name != "{"))
|
||||
{
|
||||
m_postfix.Add(tpSym);
|
||||
tpSym = (Symbol)tpStack.Pop();
|
||||
}
|
||||
}
|
||||
}
|
||||
else if (sym.m_type == Type.UnaryOperator)
|
||||
{
|
||||
tpStack.Push(sym);
|
||||
}
|
||||
else
|
||||
{
|
||||
if (tpStack.Count > 0)
|
||||
{
|
||||
tpSym = (Symbol)tpStack.Pop();
|
||||
while ((tpStack.Count != 0) && ((tpSym.m_type == Type.UnaryOperator) || (tpSym.m_type == Type.Operator) || (tpSym.m_type == Type.Function) || (tpSym.m_type == Type.Comma)) && (Precedence(tpSym) >= Precedence(sym)))
|
||||
{
|
||||
m_postfix.Add(tpSym);
|
||||
tpSym = (Symbol)tpStack.Pop();
|
||||
}
|
||||
if (((tpSym.m_type == Type.UnaryOperator) || (tpSym.m_type == Type.Operator) || (tpSym.m_type == Type.Function) || (tpSym.m_type == Type.Comma)) && (Precedence(tpSym) >= Precedence(sym)))
|
||||
m_postfix.Add(tpSym);
|
||||
else
|
||||
tpStack.Push(tpSym);
|
||||
}
|
||||
tpStack.Push(sym);
|
||||
}
|
||||
}
|
||||
while (tpStack.Count > 0)
|
||||
{
|
||||
tpSym = (Symbol)tpStack.Pop();
|
||||
m_postfix.Add(tpSym);
|
||||
}
|
||||
}
|
||||
|
||||
public void EvaluatePostfix()
|
||||
{
|
||||
Symbol tpSym1, tpSym2, tpResult;
|
||||
Stack tpStack = new Stack();
|
||||
ArrayList fnParam = new ArrayList();
|
||||
m_bError = false;
|
||||
foreach (Symbol sym in m_postfix)
|
||||
{
|
||||
if ((sym.m_type == Type.Value) || (sym.m_type == Type.Variable) || (sym.m_type == Type.Result))
|
||||
tpStack.Push(sym);
|
||||
else if (sym.m_type == Type.UnaryOperator)
|
||||
{
|
||||
tpSym1 = (Symbol)tpStack.Pop();
|
||||
tpResult = Evaluate(sym, tpSym1);
|
||||
if (tpResult.m_type == Type.Error)
|
||||
{
|
||||
m_bError = true;
|
||||
m_sErrorDescription = tpResult.m_name;
|
||||
return;
|
||||
}
|
||||
tpStack.Push(tpResult);
|
||||
}
|
||||
else if (sym.m_type == Type.Operator)
|
||||
{
|
||||
tpSym1 = (Symbol)tpStack.Pop();
|
||||
tpSym2 = (Symbol)tpStack.Pop();
|
||||
tpResult = Evaluate(tpSym2, sym, tpSym1);
|
||||
if (tpResult.m_type == Type.Error)
|
||||
{
|
||||
m_bError = true;
|
||||
m_sErrorDescription = tpResult.m_name;
|
||||
return;
|
||||
}
|
||||
tpStack.Push(tpResult);
|
||||
}
|
||||
else if (sym.m_type == Type.Comma)
|
||||
{
|
||||
tpStack.Push(sym);
|
||||
}
|
||||
else if (sym.m_type == Type.Function)
|
||||
{
|
||||
fnParam.Clear();
|
||||
tpSym1 = (Symbol)tpStack.Pop();
|
||||
if ((tpSym1.m_type == Type.Value) || (tpSym1.m_type == Type.Variable) || (tpSym1.m_type == Type.Result))
|
||||
{
|
||||
tpResult = EvaluateFunction(sym.m_name, tpSym1);
|
||||
if (tpResult.m_type == Type.Error)
|
||||
{
|
||||
m_bError = true;
|
||||
m_sErrorDescription = tpResult.m_name;
|
||||
return;
|
||||
}
|
||||
tpStack.Push(tpResult);
|
||||
}
|
||||
else if (tpSym1.m_type == Type.Comma)
|
||||
{
|
||||
while (tpSym1.m_type == Type.Comma)
|
||||
{
|
||||
tpSym1 = (Symbol)tpStack.Pop();
|
||||
fnParam.Add(tpSym1);
|
||||
tpSym1 = (Symbol)tpStack.Pop();
|
||||
}
|
||||
fnParam.Add(tpSym1);
|
||||
tpResult = EvaluateFunction(sym.m_name, fnParam.ToArray());
|
||||
if (tpResult.m_type == Type.Error)
|
||||
{
|
||||
m_bError = true;
|
||||
m_sErrorDescription = tpResult.m_name;
|
||||
return;
|
||||
}
|
||||
tpStack.Push(tpResult);
|
||||
}
|
||||
else
|
||||
{
|
||||
tpStack.Push(tpSym1);
|
||||
tpResult = EvaluateFunction(sym.m_name);
|
||||
if (tpResult.m_type == Type.Error)
|
||||
{
|
||||
m_bError = true;
|
||||
m_sErrorDescription = tpResult.m_name;
|
||||
return;
|
||||
}
|
||||
tpStack.Push(tpResult);
|
||||
}
|
||||
}
|
||||
}
|
||||
if (tpStack.Count == 1)
|
||||
{
|
||||
tpResult = (Symbol)tpStack.Pop();
|
||||
m_result = tpResult.m_value;
|
||||
}
|
||||
}
|
||||
|
||||
protected int Precedence(Symbol sym)
|
||||
{
|
||||
switch (sym.m_type)
|
||||
{
|
||||
case Type.Bracket:
|
||||
return 7;
|
||||
case Type.Function:
|
||||
return 5;
|
||||
case Type.UnaryOperator:
|
||||
return 4;
|
||||
case Type.Comma:
|
||||
return 0;
|
||||
}
|
||||
switch (sym.m_name)
|
||||
{
|
||||
case "^":
|
||||
return 3;
|
||||
case "/":
|
||||
case "*":
|
||||
case "%":
|
||||
return 2;
|
||||
case "+":
|
||||
case "-":
|
||||
return 1;
|
||||
}
|
||||
return -1;
|
||||
}
|
||||
|
||||
protected Symbol Evaluate(Symbol opr, Symbol sym1)
|
||||
{
|
||||
Symbol result;
|
||||
result.m_name = opr.m_name + sym1.m_name;
|
||||
result.m_type = Type.Result;
|
||||
result.m_value = 0;
|
||||
switch (opr.m_name)
|
||||
{
|
||||
case "+":
|
||||
result.m_value = sym1.m_value; ;
|
||||
break;
|
||||
case "-":
|
||||
result.m_value = -1 * sym1.m_value;
|
||||
break;
|
||||
default:
|
||||
result.m_type = Type.Error;
|
||||
result.m_name = "Undefine unary operator: " + opr.m_name + ".";
|
||||
break;
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
protected Symbol Evaluate(Symbol sym1, Symbol opr, Symbol sym2)
|
||||
{
|
||||
Symbol result;
|
||||
result.m_name = sym1.m_name + opr.m_name + sym2.m_name;
|
||||
result.m_type = Type.Result;
|
||||
result.m_value = 0;
|
||||
switch (opr.m_name)
|
||||
{
|
||||
case "^":
|
||||
result.m_value = DecimalMath.Pow(sym1.m_value, sym2.m_value);
|
||||
break;
|
||||
case "/":
|
||||
{
|
||||
if (sym2.m_value != 0)
|
||||
result.m_value = sym1.m_value / sym2.m_value;
|
||||
else
|
||||
{
|
||||
result.m_name = "Divide by Zero.";
|
||||
result.m_type = Type.Error;
|
||||
}
|
||||
break;
|
||||
}
|
||||
case "*":
|
||||
result.m_value = sym1.m_value * sym2.m_value;
|
||||
break;
|
||||
case "%":
|
||||
result.m_value = sym1.m_value % sym2.m_value;
|
||||
break;
|
||||
case "+":
|
||||
result.m_value = sym1.m_value + sym2.m_value;
|
||||
break;
|
||||
case "-":
|
||||
result.m_value = sym1.m_value - sym2.m_value;
|
||||
break;
|
||||
default:
|
||||
result.m_type = Type.Error;
|
||||
result.m_name = "Undefine operator: " + opr.m_name + ".";
|
||||
break;
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
protected Symbol EvaluateFunction(string name, params Object[] args)
|
||||
{
|
||||
Symbol result;
|
||||
result.m_name = "";
|
||||
result.m_type = Type.Result;
|
||||
result.m_value = 0;
|
||||
switch (name)
|
||||
{
|
||||
case "cos":
|
||||
if (args.Length == 1)
|
||||
{
|
||||
result.m_name = name + "(" + ((Symbol)args[0]).m_value.ToString() + ")";
|
||||
result.m_value = DecimalMath.Cos(((Symbol)args[0]).m_value * DecimalMath.PI / 180);
|
||||
}
|
||||
else
|
||||
{
|
||||
result.m_name = "Invalid number of parameters in: " + name + ".";
|
||||
result.m_type = Type.Error;
|
||||
}
|
||||
break;
|
||||
case "sin":
|
||||
if (args.Length == 1)
|
||||
{
|
||||
result.m_name = name + "(" + ((Symbol)args[0]).m_value.ToString() + ")";
|
||||
result.m_value = DecimalMath.Sin(((Symbol)args[0]).m_value * DecimalMath.PI / 180);
|
||||
}
|
||||
else
|
||||
{
|
||||
result.m_name = "Invalid number of parameters in: " + name + ".";
|
||||
result.m_type = Type.Error;
|
||||
}
|
||||
break;
|
||||
case "tan":
|
||||
if (args.Length == 1)
|
||||
{
|
||||
result.m_name = name + "(" + ((Symbol)args[0]).m_value.ToString() + ")";
|
||||
result.m_value = DecimalMath.Tan(((Symbol)args[0]).m_value * DecimalMath.PI / 180);
|
||||
}
|
||||
else
|
||||
{
|
||||
result.m_name = "Invalid number of parameters in: " + name + ".";
|
||||
result.m_type = Type.Error;
|
||||
}
|
||||
break;
|
||||
case "cosh":
|
||||
if (args.Length == 1)
|
||||
{
|
||||
result.m_name = name + "(" + ((Symbol)args[0]).m_value.ToString() + ")";
|
||||
result.m_value = DecimalMath.Cosh(((Symbol)args[0]).m_value);
|
||||
}
|
||||
else
|
||||
{
|
||||
result.m_name = "Invalid number of parameters in: " + name + ".";
|
||||
result.m_type = Type.Error;
|
||||
}
|
||||
break;
|
||||
case "sinh":
|
||||
if (args.Length == 1)
|
||||
{
|
||||
result.m_name = name + "(" + ((Symbol)args[0]).m_value.ToString() + ")";
|
||||
result.m_value = DecimalMath.Sinh(((Symbol)args[0]).m_value);
|
||||
}
|
||||
else
|
||||
{
|
||||
result.m_name = "Invalid number of parameters in: " + name + ".";
|
||||
result.m_type = Type.Error;
|
||||
}
|
||||
break;
|
||||
case "tanh":
|
||||
if (args.Length == 1)
|
||||
{
|
||||
result.m_name = name + "(" + ((Symbol)args[0]).m_value.ToString() + ")";
|
||||
result.m_value = DecimalMath.Tanh(((Symbol)args[0]).m_value);
|
||||
}
|
||||
else
|
||||
{
|
||||
result.m_name = "Invalid number of parameters in: " + name + ".";
|
||||
result.m_type = Type.Error;
|
||||
}
|
||||
break;
|
||||
case "log":
|
||||
if (args.Length == 1)
|
||||
{
|
||||
result.m_name = name + "(" + ((Symbol)args[0]).m_value.ToString() + ")";
|
||||
result.m_value = DecimalMath.Log10(((Symbol)args[0]).m_value);
|
||||
}
|
||||
else
|
||||
{
|
||||
result.m_name = "Invalid number of parameters in: " + name + ".";
|
||||
result.m_type = Type.Error;
|
||||
}
|
||||
break;
|
||||
case "ln":
|
||||
if (args.Length == 1)
|
||||
{
|
||||
result.m_name = name + "(" + ((Symbol)args[0]).m_value.ToString() + ")";
|
||||
result.m_value = DecimalMath.Log(((Symbol)args[0]).m_value);
|
||||
}
|
||||
else
|
||||
{
|
||||
result.m_name = "Invalid number of parameters in: " + name + ".";
|
||||
result.m_type = Type.Error;
|
||||
}
|
||||
break;
|
||||
case "logn":
|
||||
if (args.Length == 2)
|
||||
{
|
||||
result.m_name = name + "(" + ((Symbol)args[0]).m_value.ToString() + "'" + ((Symbol)args[1]).m_value.ToString() + ")";
|
||||
// log_b(x) = ln(x) / ln(b)
|
||||
result.m_value = DecimalMath.Log(((Symbol)args[0]).m_value) / DecimalMath.Log(((Symbol)args[1]).m_value);
|
||||
}
|
||||
else
|
||||
{
|
||||
result.m_name = "Invalid number of parameters in: " + name + ".";
|
||||
result.m_type = Type.Error;
|
||||
}
|
||||
break;
|
||||
case "sqrt":
|
||||
if (args.Length == 1)
|
||||
{
|
||||
result.m_name = name + "(" + ((Symbol)args[0]).m_value.ToString() + ")";
|
||||
result.m_value = DecimalMath.Sqrt(((Symbol)args[0]).m_value);
|
||||
}
|
||||
else
|
||||
{
|
||||
result.m_name = "Invalid number of parameters in: " + name + ".";
|
||||
result.m_type = Type.Error;
|
||||
}
|
||||
break;
|
||||
case "abs":
|
||||
if (args.Length == 1)
|
||||
{
|
||||
result.m_name = name + "(" + ((Symbol)args[0]).m_value.ToString() + ")";
|
||||
result.m_value = DecimalMath.Abs(((Symbol)args[0]).m_value);
|
||||
}
|
||||
else
|
||||
{
|
||||
result.m_name = "Invalid number of parameters in: " + name + ".";
|
||||
result.m_type = Type.Error;
|
||||
}
|
||||
break;
|
||||
case "acos":
|
||||
if (args.Length == 1)
|
||||
{
|
||||
result.m_name = name + "(" + ((Symbol)args[0]).m_value.ToString() + ")";
|
||||
result.m_value = DecimalMath.Acos(((Symbol)args[0]).m_value) * 180 / DecimalMath.PI;
|
||||
}
|
||||
else
|
||||
{
|
||||
result.m_name = "Invalid number of parameters in: " + name + ".";
|
||||
result.m_type = Type.Error;
|
||||
}
|
||||
break;
|
||||
case "asin":
|
||||
if (args.Length == 1)
|
||||
{
|
||||
result.m_name = name + "(" + ((Symbol)args[0]).m_value.ToString() + ")";
|
||||
result.m_value = DecimalMath.Asin(((Symbol)args[0]).m_value);
|
||||
}
|
||||
else
|
||||
{
|
||||
result.m_name = "Invalid number of parameters in: " + name + ".";
|
||||
result.m_type = Type.Error;
|
||||
}
|
||||
break;
|
||||
case "atan":
|
||||
if (args.Length == 1)
|
||||
{
|
||||
result.m_name = name + "(" + ((Symbol)args[0]).m_value.ToString() + ")";
|
||||
result.m_value = DecimalMath.Atan(((Symbol)args[0]).m_value) * 180 / DecimalMath.PI;
|
||||
}
|
||||
else
|
||||
{
|
||||
result.m_name = "Invalid number of parameters in: " + name + ".";
|
||||
result.m_type = Type.Error;
|
||||
}
|
||||
break;
|
||||
case "exp":
|
||||
if (args.Length == 1)
|
||||
{
|
||||
result.m_name = name + "(" + ((Symbol)args[0]).m_value.ToString() + ")";
|
||||
result.m_value = DecimalMath.Exp(((Symbol)args[0]).m_value);
|
||||
}
|
||||
else
|
||||
{
|
||||
result.m_name = "Invalid number of parameters in: " + name + ".";
|
||||
result.m_type = Type.Error;
|
||||
}
|
||||
break;
|
||||
default:
|
||||
if (m_defaultFunctionEvaluation != null)
|
||||
result = m_defaultFunctionEvaluation(name, args);
|
||||
else
|
||||
{
|
||||
result.m_name = "Function: " + name + ", not found.";
|
||||
result.m_type = Type.Error;
|
||||
}
|
||||
break;
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
protected bool m_bError = false;
|
||||
protected string m_sErrorDescription = "None";
|
||||
public decimal m_result = 0;
|
||||
public ArrayList m_equation = new ArrayList();
|
||||
public ArrayList m_postfix = new ArrayList();
|
||||
protected EvaluateFunctionDelegate m_defaultFunctionEvaluation;
|
||||
}
|
||||
767
MathApp/MathApp/MathApp.Tests/GeneralMatrix.cs
Normal file
767
MathApp/MathApp/MathApp.Tests/GeneralMatrix.cs
Normal file
|
|
@ -0,0 +1,767 @@
|
|||
using System;
|
||||
using System.Runtime.Serialization;
|
||||
|
||||
namespace DotNetMatrix
|
||||
{
|
||||
internal class Maths
|
||||
{
|
||||
public static decimal Hypot(decimal a, decimal b)
|
||||
{
|
||||
decimal r;
|
||||
if (Math.Abs(a) > Math.Abs(b))
|
||||
{
|
||||
r = b/a;
|
||||
r = Math.Abs(a) * (decimal)DecimalMath.Sqrt(1 + r * r);
|
||||
}
|
||||
else if (b != 0)
|
||||
{
|
||||
r = a/b;
|
||||
r = Math.Abs(b) * (decimal)DecimalMath.Sqrt(1 + r * r);
|
||||
}
|
||||
else
|
||||
{
|
||||
r = 0.0m;
|
||||
}
|
||||
return r;
|
||||
}
|
||||
}
|
||||
|
||||
[Serializable]
|
||||
public class GeneralMatrix : System.ICloneable, System.Runtime.Serialization.ISerializable, System.IDisposable
|
||||
{
|
||||
private decimal[][] A;
|
||||
private int m, n;
|
||||
|
||||
public GeneralMatrix(int m, int n)
|
||||
{
|
||||
this.m = m;
|
||||
this.n = n;
|
||||
A = new decimal[m][];
|
||||
for (int i = 0; i < m; i++)
|
||||
{
|
||||
A[i] = new decimal[n];
|
||||
}
|
||||
}
|
||||
|
||||
public GeneralMatrix(int m, int n, decimal s)
|
||||
{
|
||||
this.m = m;
|
||||
this.n = n;
|
||||
A = new decimal[m][];
|
||||
for (int i = 0; i < m; i++)
|
||||
{
|
||||
A[i] = new decimal[n];
|
||||
for (int j = 0; j < n; j++)
|
||||
{
|
||||
A[i][j] = s;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public GeneralMatrix(decimal[][] A)
|
||||
{
|
||||
m = A.Length;
|
||||
n = A[0].Length;
|
||||
for (int i = 0; i < m; i++)
|
||||
{
|
||||
if (A[i].Length != n)
|
||||
{
|
||||
throw new System.ArgumentException("All rows must have the same length.");
|
||||
}
|
||||
}
|
||||
this.A = A;
|
||||
}
|
||||
|
||||
public GeneralMatrix(decimal[][] A, int m, int n)
|
||||
{
|
||||
this.A = A;
|
||||
this.m = m;
|
||||
this.n = n;
|
||||
}
|
||||
|
||||
public GeneralMatrix(decimal[] vals, int m)
|
||||
{
|
||||
this.m = m;
|
||||
n = (m != 0?vals.Length / m:0);
|
||||
if (m * n != vals.Length)
|
||||
{
|
||||
throw new System.ArgumentException("Array length must be a multiple of m.");
|
||||
}
|
||||
A = new decimal[m][];
|
||||
for (int i = 0; i < m; i++)
|
||||
{
|
||||
A[i] = new decimal[n];
|
||||
for (int j = 0; j < n; j++)
|
||||
{
|
||||
A[i][j] = vals[i + j * m];
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public virtual decimal[][] Array
|
||||
{
|
||||
get
|
||||
{
|
||||
return A;
|
||||
}
|
||||
}
|
||||
|
||||
public virtual decimal[][] ArrayCopy
|
||||
{
|
||||
get
|
||||
{
|
||||
decimal[][] C = new decimal[m][];
|
||||
for (int i = 0; i < m; i++)
|
||||
{
|
||||
C[i] = new decimal[n];
|
||||
for (int j = 0; j < n; j++)
|
||||
{
|
||||
C[i][j] = A[i][j];
|
||||
}
|
||||
}
|
||||
return C;
|
||||
}
|
||||
}
|
||||
|
||||
public virtual decimal[] ColumnPackedCopy
|
||||
{
|
||||
get
|
||||
{
|
||||
decimal[] vals = new decimal[m * n];
|
||||
for (int i = 0; i < m; i++)
|
||||
{
|
||||
for (int j = 0; j < n; j++)
|
||||
{
|
||||
vals[i + j * m] = A[i][j];
|
||||
}
|
||||
}
|
||||
return vals;
|
||||
}
|
||||
}
|
||||
|
||||
public virtual decimal[] RowPackedCopy
|
||||
{
|
||||
get
|
||||
{
|
||||
decimal[] vals = new decimal[m * n];
|
||||
for (int i = 0; i < m; i++)
|
||||
{
|
||||
for (int j = 0; j < n; j++)
|
||||
{
|
||||
vals[i * n + j] = A[i][j];
|
||||
}
|
||||
}
|
||||
return vals;
|
||||
}
|
||||
}
|
||||
|
||||
public virtual int RowDimension
|
||||
{
|
||||
get
|
||||
{
|
||||
return m;
|
||||
}
|
||||
}
|
||||
|
||||
public virtual int ColumnDimension
|
||||
{
|
||||
get
|
||||
{
|
||||
return n;
|
||||
}
|
||||
}
|
||||
|
||||
public static GeneralMatrix ConstructWithCopy(decimal[][] A)
|
||||
{
|
||||
int m = A.Length;
|
||||
int n = A[0].Length;
|
||||
GeneralMatrix X = new GeneralMatrix(m, n);
|
||||
decimal[][] C = X.Array;
|
||||
for (int i = 0; i < m; i++)
|
||||
{
|
||||
if (A[i].Length != n)
|
||||
{
|
||||
throw new System.ArgumentException("All rows must have the same length.");
|
||||
}
|
||||
for (int j = 0; j < n; j++)
|
||||
{
|
||||
C[i][j] = A[i][j];
|
||||
}
|
||||
}
|
||||
return X;
|
||||
}
|
||||
|
||||
public virtual GeneralMatrix Copy()
|
||||
{
|
||||
GeneralMatrix X = new GeneralMatrix(m, n);
|
||||
decimal[][] C = X.Array;
|
||||
for (int i = 0; i < m; i++)
|
||||
{
|
||||
for (int j = 0; j < n; j++)
|
||||
{
|
||||
C[i][j] = A[i][j];
|
||||
}
|
||||
}
|
||||
return X;
|
||||
}
|
||||
|
||||
public virtual decimal GetElement(int i, int j)
|
||||
{
|
||||
return A[i][j];
|
||||
}
|
||||
|
||||
public virtual GeneralMatrix GetMatrix(int i0, int i1, int j0, int j1)
|
||||
{
|
||||
GeneralMatrix X = new GeneralMatrix(i1 - i0 + 1, j1 - j0 + 1);
|
||||
decimal[][] B = X.Array;
|
||||
try
|
||||
{
|
||||
for (int i = i0; i <= i1; i++)
|
||||
{
|
||||
for (int j = j0; j <= j1; j++)
|
||||
{
|
||||
B[i - i0][j - j0] = A[i][j];
|
||||
}
|
||||
}
|
||||
}
|
||||
catch (System.IndexOutOfRangeException e)
|
||||
{
|
||||
throw new System.IndexOutOfRangeException("Submatrix indices", e);
|
||||
}
|
||||
return X;
|
||||
}
|
||||
|
||||
public virtual GeneralMatrix GetMatrix(int[] r, int[] c)
|
||||
{
|
||||
GeneralMatrix X = new GeneralMatrix(r.Length, c.Length);
|
||||
decimal[][] B = X.Array;
|
||||
try
|
||||
{
|
||||
for (int i = 0; i < r.Length; i++)
|
||||
{
|
||||
for (int j = 0; j < c.Length; j++)
|
||||
{
|
||||
B[i][j] = A[r[i]][c[j]];
|
||||
}
|
||||
}
|
||||
}
|
||||
catch (System.IndexOutOfRangeException e)
|
||||
{
|
||||
throw new System.IndexOutOfRangeException("Submatrix indices", e);
|
||||
}
|
||||
return X;
|
||||
}
|
||||
|
||||
public virtual GeneralMatrix GetMatrix(int i0, int i1, int[] c)
|
||||
{
|
||||
GeneralMatrix X = new GeneralMatrix(i1 - i0 + 1, c.Length);
|
||||
decimal[][] B = X.Array;
|
||||
try
|
||||
{
|
||||
for (int i = i0; i <= i1; i++)
|
||||
{
|
||||
for (int j = 0; j < c.Length; j++)
|
||||
{
|
||||
B[i - i0][j] = A[i][c[j]];
|
||||
}
|
||||
}
|
||||
}
|
||||
catch (System.IndexOutOfRangeException e)
|
||||
{
|
||||
throw new System.IndexOutOfRangeException("Submatrix indices", e);
|
||||
}
|
||||
return X;
|
||||
}
|
||||
|
||||
public virtual GeneralMatrix GetMatrix(int[] r, int j0, int j1)
|
||||
{
|
||||
GeneralMatrix X = new GeneralMatrix(r.Length, j1 - j0 + 1);
|
||||
decimal[][] B = X.Array;
|
||||
try
|
||||
{
|
||||
for (int i = 0; i < r.Length; i++)
|
||||
{
|
||||
for (int j = j0; j <= j1; j++)
|
||||
{
|
||||
B[i][j - j0] = A[r[i]][j];
|
||||
}
|
||||
}
|
||||
}
|
||||
catch (System.IndexOutOfRangeException e)
|
||||
{
|
||||
throw new System.IndexOutOfRangeException("Submatrix indices", e);
|
||||
}
|
||||
return X;
|
||||
}
|
||||
|
||||
public virtual void SetElement(int i, int j, decimal s)
|
||||
{
|
||||
A[i][j] = s;
|
||||
}
|
||||
|
||||
public virtual void SetMatrix(int i0, int i1, int j0, int j1, GeneralMatrix X)
|
||||
{
|
||||
try
|
||||
{
|
||||
for (int i = i0; i <= i1; i++)
|
||||
{
|
||||
for (int j = j0; j <= j1; j++)
|
||||
{
|
||||
A[i][j] = X.GetElement(i - i0, j - j0);
|
||||
}
|
||||
}
|
||||
}
|
||||
catch (System.IndexOutOfRangeException e)
|
||||
{
|
||||
throw new System.IndexOutOfRangeException("Submatrix indices", e);
|
||||
}
|
||||
}
|
||||
|
||||
public virtual void SetMatrix(int[] r, int[] c, GeneralMatrix X)
|
||||
{
|
||||
try
|
||||
{
|
||||
for (int i = 0; i < r.Length; i++)
|
||||
{
|
||||
for (int j = 0; j < c.Length; j++)
|
||||
{
|
||||
A[r[i]][c[j]] = X.GetElement(i, j);
|
||||
}
|
||||
}
|
||||
}
|
||||
catch (System.IndexOutOfRangeException e)
|
||||
{
|
||||
throw new System.IndexOutOfRangeException("Submatrix indices", e);
|
||||
}
|
||||
}
|
||||
|
||||
public virtual void SetMatrix(int[] r, int j0, int j1, GeneralMatrix X)
|
||||
{
|
||||
try
|
||||
{
|
||||
for (int i = 0; i < r.Length; i++)
|
||||
{
|
||||
for (int j = j0; j <= j1; j++)
|
||||
{
|
||||
A[r[i]][j] = X.GetElement(i, j - j0);
|
||||
}
|
||||
}
|
||||
}
|
||||
catch (System.IndexOutOfRangeException e)
|
||||
{
|
||||
throw new System.IndexOutOfRangeException("Submatrix indices", e);
|
||||
}
|
||||
}
|
||||
|
||||
public virtual void SetMatrix(int i0, int i1, int[] c, GeneralMatrix X)
|
||||
{
|
||||
try
|
||||
{
|
||||
for (int i = i0; i <= i1; i++)
|
||||
{
|
||||
for (int j = 0; j < c.Length; j++)
|
||||
{
|
||||
A[i][c[j]] = X.GetElement(i - i0, j);
|
||||
}
|
||||
}
|
||||
}
|
||||
catch (System.IndexOutOfRangeException e)
|
||||
{
|
||||
throw new System.IndexOutOfRangeException("Submatrix indices", e);
|
||||
}
|
||||
}
|
||||
|
||||
public virtual GeneralMatrix Transpose()
|
||||
{
|
||||
GeneralMatrix X = new GeneralMatrix(n, m);
|
||||
decimal[][] C = X.Array;
|
||||
for (int i = 0; i < m; i++)
|
||||
{
|
||||
for (int j = 0; j < n; j++)
|
||||
{
|
||||
C[j][i] = A[i][j];
|
||||
}
|
||||
}
|
||||
return X;
|
||||
}
|
||||
|
||||
public virtual decimal Norm1()
|
||||
{
|
||||
decimal f = 0;
|
||||
for (int j = 0; j < n; j++)
|
||||
{
|
||||
decimal s = 0;
|
||||
for (int i = 0; i < m; i++)
|
||||
{
|
||||
s += System.Math.Abs(A[i][j]);
|
||||
}
|
||||
f = System.Math.Max(f, s);
|
||||
}
|
||||
return f;
|
||||
}
|
||||
|
||||
public virtual decimal NormInf()
|
||||
{
|
||||
decimal f = 0;
|
||||
for (int i = 0; i < m; i++)
|
||||
{
|
||||
decimal s = 0;
|
||||
for (int j = 0; j < n; j++)
|
||||
{
|
||||
s += System.Math.Abs(A[i][j]);
|
||||
}
|
||||
f = System.Math.Max(f, s);
|
||||
}
|
||||
return f;
|
||||
}
|
||||
|
||||
public virtual decimal NormF()
|
||||
{
|
||||
decimal f = 0;
|
||||
for (int i = 0; i < m; i++)
|
||||
{
|
||||
for (int j = 0; j < n; j++)
|
||||
{
|
||||
f = Maths.Hypot(f, A[i][j]);
|
||||
}
|
||||
}
|
||||
return f;
|
||||
}
|
||||
|
||||
public virtual GeneralMatrix UnaryMinus()
|
||||
{
|
||||
GeneralMatrix X = new GeneralMatrix(m, n);
|
||||
decimal[][] C = X.Array;
|
||||
for (int i = 0; i < m; i++)
|
||||
{
|
||||
for (int j = 0; j < n; j++)
|
||||
{
|
||||
C[i][j] = - A[i][j];
|
||||
}
|
||||
}
|
||||
return X;
|
||||
}
|
||||
|
||||
public virtual GeneralMatrix Add(GeneralMatrix B)
|
||||
{
|
||||
CheckMatrixDimensions(B);
|
||||
GeneralMatrix X = new GeneralMatrix(m, n);
|
||||
decimal[][] C = X.Array;
|
||||
for (int i = 0; i < m; i++)
|
||||
{
|
||||
for (int j = 0; j < n; j++)
|
||||
{
|
||||
C[i][j] = A[i][j] + B.A[i][j];
|
||||
}
|
||||
}
|
||||
return X;
|
||||
}
|
||||
|
||||
public virtual GeneralMatrix AddEquals(GeneralMatrix B)
|
||||
{
|
||||
CheckMatrixDimensions(B);
|
||||
for (int i = 0; i < m; i++)
|
||||
{
|
||||
for (int j = 0; j < n; j++)
|
||||
{
|
||||
A[i][j] = A[i][j] + B.A[i][j];
|
||||
}
|
||||
}
|
||||
return this;
|
||||
}
|
||||
|
||||
public virtual GeneralMatrix Subtract(GeneralMatrix B)
|
||||
{
|
||||
CheckMatrixDimensions(B);
|
||||
GeneralMatrix X = new GeneralMatrix(m, n);
|
||||
decimal[][] C = X.Array;
|
||||
for (int i = 0; i < m; i++)
|
||||
{
|
||||
for (int j = 0; j < n; j++)
|
||||
{
|
||||
C[i][j] = A[i][j] - B.A[i][j];
|
||||
}
|
||||
}
|
||||
return X;
|
||||
}
|
||||
|
||||
public virtual GeneralMatrix SubtractEquals(GeneralMatrix B)
|
||||
{
|
||||
CheckMatrixDimensions(B);
|
||||
for (int i = 0; i < m; i++)
|
||||
{
|
||||
for (int j = 0; j < n; j++)
|
||||
{
|
||||
A[i][j] = A[i][j] - B.A[i][j];
|
||||
}
|
||||
}
|
||||
return this;
|
||||
}
|
||||
|
||||
public virtual GeneralMatrix ArrayMultiply(GeneralMatrix B)
|
||||
{
|
||||
CheckMatrixDimensions(B);
|
||||
GeneralMatrix X = new GeneralMatrix(m, n);
|
||||
decimal[][] C = X.Array;
|
||||
for (int i = 0; i < m; i++)
|
||||
{
|
||||
for (int j = 0; j < n; j++)
|
||||
{
|
||||
C[i][j] = A[i][j] * B.A[i][j];
|
||||
}
|
||||
}
|
||||
return X;
|
||||
}
|
||||
|
||||
public virtual GeneralMatrix ArrayMultiplyEquals(GeneralMatrix B)
|
||||
{
|
||||
CheckMatrixDimensions(B);
|
||||
for (int i = 0; i < m; i++)
|
||||
{
|
||||
for (int j = 0; j < n; j++)
|
||||
{
|
||||
A[i][j] = A[i][j] * B.A[i][j];
|
||||
}
|
||||
}
|
||||
return this;
|
||||
}
|
||||
|
||||
public virtual GeneralMatrix ArrayRightDivide(GeneralMatrix B)
|
||||
{
|
||||
CheckMatrixDimensions(B);
|
||||
GeneralMatrix X = new GeneralMatrix(m, n);
|
||||
decimal[][] C = X.Array;
|
||||
for (int i = 0; i < m; i++)
|
||||
{
|
||||
for (int j = 0; j < n; j++)
|
||||
{
|
||||
C[i][j] = A[i][j] / B.A[i][j];
|
||||
}
|
||||
}
|
||||
return X;
|
||||
}
|
||||
|
||||
public virtual GeneralMatrix ArrayRightDivideEquals(GeneralMatrix B)
|
||||
{
|
||||
CheckMatrixDimensions(B);
|
||||
for (int i = 0; i < m; i++)
|
||||
{
|
||||
for (int j = 0; j < n; j++)
|
||||
{
|
||||
A[i][j] = A[i][j] / B.A[i][j];
|
||||
}
|
||||
}
|
||||
return this;
|
||||
}
|
||||
|
||||
public virtual GeneralMatrix ArrayLeftDivide(GeneralMatrix B)
|
||||
{
|
||||
CheckMatrixDimensions(B);
|
||||
GeneralMatrix X = new GeneralMatrix(m, n);
|
||||
decimal[][] C = X.Array;
|
||||
for (int i = 0; i < m; i++)
|
||||
{
|
||||
for (int j = 0; j < n; j++)
|
||||
{
|
||||
C[i][j] = B.A[i][j] / A[i][j];
|
||||
}
|
||||
}
|
||||
return X;
|
||||
}
|
||||
|
||||
public virtual GeneralMatrix ArrayLeftDivideEquals(GeneralMatrix B)
|
||||
{
|
||||
CheckMatrixDimensions(B);
|
||||
for (int i = 0; i < m; i++)
|
||||
{
|
||||
for (int j = 0; j < n; j++)
|
||||
{
|
||||
A[i][j] = B.A[i][j] / A[i][j];
|
||||
}
|
||||
}
|
||||
return this;
|
||||
}
|
||||
|
||||
public virtual GeneralMatrix Multiply(decimal s)
|
||||
{
|
||||
GeneralMatrix X = new GeneralMatrix(m, n);
|
||||
decimal[][] C = X.Array;
|
||||
for (int i = 0; i < m; i++)
|
||||
{
|
||||
for (int j = 0; j < n; j++)
|
||||
{
|
||||
C[i][j] = s * A[i][j];
|
||||
}
|
||||
}
|
||||
return X;
|
||||
}
|
||||
|
||||
public virtual GeneralMatrix MultiplyEquals(decimal s)
|
||||
{
|
||||
for (int i = 0; i < m; i++)
|
||||
{
|
||||
for (int j = 0; j < n; j++)
|
||||
{
|
||||
A[i][j] = s * A[i][j];
|
||||
}
|
||||
}
|
||||
return this;
|
||||
}
|
||||
|
||||
public virtual GeneralMatrix Multiply(GeneralMatrix B)
|
||||
{
|
||||
if (B.m != n)
|
||||
{
|
||||
throw new System.ArgumentException("GeneralMatrix inner dimensions must agree.");
|
||||
}
|
||||
GeneralMatrix X = new GeneralMatrix(m, B.n);
|
||||
decimal[][] C = X.Array;
|
||||
decimal[] Bcolj = new decimal[n];
|
||||
for (int j = 0; j < B.n; j++)
|
||||
{
|
||||
for (int k = 0; k < n; k++)
|
||||
{
|
||||
Bcolj[k] = B.A[k][j];
|
||||
}
|
||||
for (int i = 0; i < m; i++)
|
||||
{
|
||||
decimal[] Arowi = A[i];
|
||||
decimal s = 0;
|
||||
for (int k = 0; k < n; k++)
|
||||
{
|
||||
s += Arowi[k] * Bcolj[k];
|
||||
}
|
||||
C[i][j] = s;
|
||||
}
|
||||
}
|
||||
return X;
|
||||
}
|
||||
|
||||
public static GeneralMatrix operator +(GeneralMatrix m1, GeneralMatrix m2)
|
||||
{
|
||||
return m1.Add(m2);
|
||||
}
|
||||
|
||||
public static GeneralMatrix operator -(GeneralMatrix m1, GeneralMatrix m2)
|
||||
{
|
||||
return m1.Subtract(m2);
|
||||
}
|
||||
|
||||
public static GeneralMatrix operator *(GeneralMatrix m1, GeneralMatrix m2)
|
||||
{
|
||||
return m1.Multiply(m2);
|
||||
}
|
||||
|
||||
public virtual LUDecomposition LUD()
|
||||
{
|
||||
return new LUDecomposition(this);
|
||||
}
|
||||
|
||||
public virtual QRDecomposition QRD()
|
||||
{
|
||||
return new QRDecomposition(this);
|
||||
}
|
||||
|
||||
public virtual CholeskyDecomposition chol()
|
||||
{
|
||||
return new CholeskyDecomposition(this);
|
||||
}
|
||||
|
||||
public virtual SingularValueDecomposition SVD()
|
||||
{
|
||||
return new SingularValueDecomposition(this);
|
||||
}
|
||||
|
||||
public virtual EigenvalueDecomposition Eigen()
|
||||
{
|
||||
return new EigenvalueDecomposition(this);
|
||||
}
|
||||
|
||||
public virtual GeneralMatrix Solve(GeneralMatrix B)
|
||||
{
|
||||
return (m == n ? (new LUDecomposition(this)).Solve(B):(new QRDecomposition(this)).Solve(B));
|
||||
}
|
||||
|
||||
public virtual GeneralMatrix SolveTranspose(GeneralMatrix B)
|
||||
{
|
||||
return Transpose().Solve(B.Transpose());
|
||||
}
|
||||
|
||||
public virtual GeneralMatrix Inverse()
|
||||
{
|
||||
return Solve(Identity(m, m));
|
||||
}
|
||||
|
||||
public virtual decimal Determinant()
|
||||
{
|
||||
return new LUDecomposition(this).Determinant();
|
||||
}
|
||||
|
||||
public virtual int Rank()
|
||||
{
|
||||
return new SingularValueDecomposition(this).Rank();
|
||||
}
|
||||
|
||||
public virtual decimal Condition()
|
||||
{
|
||||
return new SingularValueDecomposition(this).Condition();
|
||||
}
|
||||
|
||||
public virtual decimal Trace()
|
||||
{
|
||||
decimal t = 0;
|
||||
for (int i = 0; i < System.Math.Min(m, n); i++)
|
||||
{
|
||||
t += A[i][i];
|
||||
}
|
||||
return t;
|
||||
}
|
||||
|
||||
public static GeneralMatrix Identity(int m, int n)
|
||||
{
|
||||
GeneralMatrix A = new GeneralMatrix(m, n);
|
||||
decimal[][] X = A.Array;
|
||||
for (int i = 0; i < m; i++)
|
||||
{
|
||||
for (int j = 0; j < n; j++)
|
||||
{
|
||||
X[i][j] = (i == j ? 1.0m : 0.0m);
|
||||
}
|
||||
}
|
||||
return A;
|
||||
}
|
||||
|
||||
private void CheckMatrixDimensions(GeneralMatrix B)
|
||||
{
|
||||
if (B.m != m || B.n != n)
|
||||
{
|
||||
throw new System.ArgumentException("GeneralMatrix dimensions must agree.");
|
||||
}
|
||||
}
|
||||
|
||||
public void Dispose()
|
||||
{
|
||||
Dispose(true);
|
||||
}
|
||||
|
||||
private void Dispose(bool disposing)
|
||||
{
|
||||
if (disposing)
|
||||
GC.SuppressFinalize(this);
|
||||
}
|
||||
|
||||
~GeneralMatrix()
|
||||
{
|
||||
Dispose(false);
|
||||
}
|
||||
|
||||
public System.Object Clone()
|
||||
{
|
||||
return this.Copy();
|
||||
}
|
||||
|
||||
void ISerializable.GetObjectData(SerializationInfo info, StreamingContext context)
|
||||
{
|
||||
}
|
||||
}
|
||||
}
|
||||
322
MathApp/MathApp/MathApp.Tests/LUDecomposition.cs
Normal file
322
MathApp/MathApp/MathApp.Tests/LUDecomposition.cs
Normal file
|
|
@ -0,0 +1,322 @@
|
|||
using System;
|
||||
using System.Runtime.Serialization;
|
||||
|
||||
namespace DotNetMatrix
|
||||
{
|
||||
|
||||
/// <summary>LU Decomposition.
|
||||
/// For an m-by-n matrix A with m >= n, the LU decomposition is an m-by-n
|
||||
/// unit lower triangular matrix L, an n-by-n upper triangular matrix U,
|
||||
/// and a permutation vector piv of length m so that A(piv,:) = L*U.
|
||||
/// <code> If m < n, then L is m-by-m and U is m-by-n. </code>
|
||||
/// The LU decompostion with pivoting always exists, even if the matrix is
|
||||
/// singular, so the constructor will never fail. The primary use of the
|
||||
/// LU decomposition is in the solution of square systems of simultaneous
|
||||
/// linear equations. This will fail if IsNonSingular() returns false.
|
||||
/// </summary>
|
||||
|
||||
[Serializable]
|
||||
public class LUDecomposition : System.Runtime.Serialization.ISerializable
|
||||
{
|
||||
#region Class variables
|
||||
|
||||
/// <summary>Array for internal storage of decomposition.
|
||||
/// @serial internal array storage.
|
||||
/// </summary>
|
||||
private decimal[][] LU;
|
||||
|
||||
/// <summary>Row and column dimensions, and pivot sign.
|
||||
/// @serial column dimension.
|
||||
/// @serial row dimension.
|
||||
/// @serial pivot sign.
|
||||
/// </summary>
|
||||
private int m, n, pivsign;
|
||||
|
||||
/// <summary>Internal storage of pivot vector.
|
||||
/// @serial pivot vector.
|
||||
/// </summary>
|
||||
private int[] piv;
|
||||
|
||||
#endregion // Class variables
|
||||
|
||||
#region Constructor
|
||||
|
||||
/// <summary>LU Decomposition</summary>
|
||||
/// <param name="A"> Rectangular matrix
|
||||
/// </param>
|
||||
/// <returns> Structure to access L, U and piv.
|
||||
/// </returns>
|
||||
|
||||
public LUDecomposition(GeneralMatrix A)
|
||||
{
|
||||
// Use a "left-looking", dot-product, Crout/Doolittle algorithm.
|
||||
|
||||
LU = A.ArrayCopy;
|
||||
m = A.RowDimension;
|
||||
n = A.ColumnDimension;
|
||||
piv = new int[m];
|
||||
for (int i = 0; i < m; i++)
|
||||
{
|
||||
piv[i] = i;
|
||||
}
|
||||
pivsign = 1;
|
||||
decimal[] LUrowi;
|
||||
decimal[] LUcolj = new decimal[m];
|
||||
|
||||
// Outer loop.
|
||||
|
||||
for (int j = 0; j < n; j++)
|
||||
{
|
||||
|
||||
// Make a copy of the j-th column to localize references.
|
||||
|
||||
for (int i = 0; i < m; i++)
|
||||
{
|
||||
LUcolj[i] = LU[i][j];
|
||||
}
|
||||
|
||||
// Apply previous transformations.
|
||||
|
||||
for (int i = 0; i < m; i++)
|
||||
{
|
||||
LUrowi = LU[i];
|
||||
|
||||
// Most of the time is spent in the following dot product.
|
||||
|
||||
int kmax = System.Math.Min(i, j);
|
||||
decimal s = 0.0m;
|
||||
for (int k = 0; k < kmax; k++)
|
||||
{
|
||||
s += LUrowi[k] * LUcolj[k];
|
||||
}
|
||||
|
||||
LUrowi[j] = LUcolj[i] -= s;
|
||||
}
|
||||
|
||||
// Find pivot and exchange if necessary.
|
||||
|
||||
int p = j;
|
||||
for (int i = j + 1; i < m; i++)
|
||||
{
|
||||
if (System.Math.Abs(LUcolj[i]) > System.Math.Abs(LUcolj[p]))
|
||||
{
|
||||
p = i;
|
||||
}
|
||||
}
|
||||
if (p != j)
|
||||
{
|
||||
for (int k = 0; k < n; k++)
|
||||
{
|
||||
decimal t = LU[p][k]; LU[p][k] = LU[j][k]; LU[j][k] = t;
|
||||
}
|
||||
int k2 = piv[p]; piv[p] = piv[j]; piv[j] = k2;
|
||||
pivsign = - pivsign;
|
||||
}
|
||||
|
||||
// Compute multipliers.
|
||||
|
||||
if (j < m & LU[j][j] != 0.0m)
|
||||
{
|
||||
for (int i = j + 1; i < m; i++)
|
||||
{
|
||||
LU[i][j] /= LU[j][j];
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
#endregion // Constructor
|
||||
|
||||
#region Public Properties
|
||||
/// <summary>Is the matrix nonsingular?</summary>
|
||||
/// <returns> true if U, and hence A, is nonsingular.
|
||||
/// </returns>
|
||||
virtual public bool IsNonSingular
|
||||
{
|
||||
get
|
||||
{
|
||||
for (int j = 0; j < n; j++)
|
||||
{
|
||||
if (LU[j][j] == 0)
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>Return lower triangular factor</summary>
|
||||
/// <returns> L
|
||||
/// </returns>
|
||||
virtual public GeneralMatrix L
|
||||
{
|
||||
get
|
||||
{
|
||||
GeneralMatrix X = new GeneralMatrix(m, n);
|
||||
decimal[][] L = X.Array;
|
||||
for (int i = 0; i < m; i++)
|
||||
{
|
||||
for (int j = 0; j < n; j++)
|
||||
{
|
||||
if (i > j)
|
||||
{
|
||||
L[i][j] = LU[i][j];
|
||||
}
|
||||
else if (i == j)
|
||||
{
|
||||
L[i][j] = 1.0m;
|
||||
}
|
||||
else
|
||||
{
|
||||
L[i][j] = 0.0m;
|
||||
}
|
||||
}
|
||||
}
|
||||
return X;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>Return upper triangular factor</summary>
|
||||
/// <returns> U
|
||||
/// </returns>
|
||||
virtual public GeneralMatrix U
|
||||
{
|
||||
get
|
||||
{
|
||||
GeneralMatrix X = new GeneralMatrix(n, n);
|
||||
decimal[][] U = X.Array;
|
||||
for (int i = 0; i < n; i++)
|
||||
{
|
||||
for (int j = 0; j < n; j++)
|
||||
{
|
||||
if (i <= j)
|
||||
{
|
||||
U[i][j] = LU[i][j];
|
||||
}
|
||||
else
|
||||
{
|
||||
U[i][j] = 0.0m;
|
||||
}
|
||||
}
|
||||
}
|
||||
return X;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>Return pivot permutation vector</summary>
|
||||
/// <returns> piv
|
||||
/// </returns>
|
||||
virtual public int[] Pivot
|
||||
{
|
||||
get
|
||||
{
|
||||
int[] p = new int[m];
|
||||
for (int i = 0; i < m; i++)
|
||||
{
|
||||
p[i] = piv[i];
|
||||
}
|
||||
return p;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>Return pivot permutation vector as a one-dimensional decimal array</summary>
|
||||
/// <returns> (decimal) piv
|
||||
/// </returns>
|
||||
virtual public decimal[] decimalPivot
|
||||
{
|
||||
get
|
||||
{
|
||||
decimal[] vals = new decimal[m];
|
||||
for (int i = 0; i < m; i++)
|
||||
{
|
||||
vals[i] = (decimal) piv[i];
|
||||
}
|
||||
return vals;
|
||||
}
|
||||
}
|
||||
|
||||
#endregion // Public Properties
|
||||
|
||||
#region Public Methods
|
||||
|
||||
/// <summary>Determinant</summary>
|
||||
/// <returns> det(A)
|
||||
/// </returns>
|
||||
/// <exception cref="System.ArgumentException"> Matrix must be square
|
||||
/// </exception>
|
||||
|
||||
public virtual decimal Determinant()
|
||||
{
|
||||
if (m != n)
|
||||
{
|
||||
throw new System.ArgumentException("Matrix must be square.");
|
||||
}
|
||||
decimal d = (decimal) pivsign;
|
||||
for (int j = 0; j < n; j++)
|
||||
{
|
||||
d *= LU[j][j];
|
||||
}
|
||||
return d;
|
||||
}
|
||||
|
||||
/// <summary>Solve A*X = B</summary>
|
||||
/// <param name="B"> A Matrix with as many rows as A and any number of columns.
|
||||
/// </param>
|
||||
/// <returns> X so that L*U*X = B(piv,:)
|
||||
/// </returns>
|
||||
/// <exception cref="System.ArgumentException"> Matrix row dimensions must agree.
|
||||
/// </exception>
|
||||
/// <exception cref="System.SystemException"> Matrix is singular.
|
||||
/// </exception>
|
||||
|
||||
public virtual GeneralMatrix Solve(GeneralMatrix B)
|
||||
{
|
||||
if (B.RowDimension != m)
|
||||
{
|
||||
throw new System.ArgumentException("Matrix row dimensions must agree.");
|
||||
}
|
||||
if (!this.IsNonSingular)
|
||||
{
|
||||
throw new System.SystemException("Matrix is singular.");
|
||||
}
|
||||
|
||||
// Copy right hand side with pivoting
|
||||
int nx = B.ColumnDimension;
|
||||
GeneralMatrix Xmat = B.GetMatrix(piv, 0, nx - 1);
|
||||
decimal[][] X = Xmat.Array;
|
||||
|
||||
// Solve L*Y = B(piv,:)
|
||||
for (int k = 0; k < n; k++)
|
||||
{
|
||||
for (int i = k + 1; i < n; i++)
|
||||
{
|
||||
for (int j = 0; j < nx; j++)
|
||||
{
|
||||
X[i][j] -= X[k][j] * LU[i][k];
|
||||
}
|
||||
}
|
||||
}
|
||||
// Solve U*X = Y;
|
||||
for (int k = n - 1; k >= 0; k--)
|
||||
{
|
||||
for (int j = 0; j < nx; j++)
|
||||
{
|
||||
X[k][j] /= LU[k][k];
|
||||
}
|
||||
for (int i = 0; i < k; i++)
|
||||
{
|
||||
for (int j = 0; j < nx; j++)
|
||||
{
|
||||
X[i][j] -= X[k][j] * LU[i][k];
|
||||
}
|
||||
}
|
||||
}
|
||||
return Xmat;
|
||||
}
|
||||
|
||||
#endregion // Public Methods
|
||||
|
||||
// A method called when serializing this class.
|
||||
void ISerializable.GetObjectData(SerializationInfo info, StreamingContext context)
|
||||
{
|
||||
}
|
||||
}
|
||||
}
|
||||
290
MathApp/MathApp/MathApp.Tests/Matrix.cs
Normal file
290
MathApp/MathApp/MathApp.Tests/Matrix.cs
Normal file
|
|
@ -0,0 +1,290 @@
|
|||
using System;
|
||||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
using System.Globalization;
|
||||
|
||||
public class Matrix
|
||||
{
|
||||
private static Random random = new Random();
|
||||
|
||||
public static decimal[][] GenerateRandomMatrix(int rows, int columns, decimal min, decimal max)
|
||||
{
|
||||
decimal[][] res = new decimal[rows][];
|
||||
for (int i = 0; i < rows; i++)
|
||||
{
|
||||
res[i] = new decimal[columns];
|
||||
for (int j = 0; j < columns; j++)
|
||||
{
|
||||
// Replaced UnityEngine.Random with System.Random
|
||||
double range = (double)(max - min);
|
||||
double sample = random.NextDouble();
|
||||
double scaled = (sample * range) + (double)min;
|
||||
decimal value = (decimal)System.Math.Round(scaled, 2);
|
||||
res[i][j] = value;
|
||||
}
|
||||
}
|
||||
return res;
|
||||
}
|
||||
|
||||
public static decimal[][] Transpoze(decimal[][] _m)
|
||||
{
|
||||
decimal[][] res = new decimal[_m[0].Length][];
|
||||
for (int i = 0; i < _m[0].Length; i++)
|
||||
{
|
||||
res[i] = new decimal[_m.Length];
|
||||
}
|
||||
for (int i = 0; i < _m.Length; i++)
|
||||
{
|
||||
for (int j = 0; j < _m[0].Length; j++)
|
||||
{
|
||||
res[j][i] = _m[i][j];
|
||||
}
|
||||
}
|
||||
return res;
|
||||
}
|
||||
|
||||
public static decimal MatrixDeterminant(decimal[][] _m)
|
||||
{
|
||||
if (_m.Length == _m[0].Length)
|
||||
{
|
||||
int c = _m.Length;
|
||||
if (c == 2)
|
||||
{
|
||||
return (decimal)Math.Round(_m[0][0] * _m[1][1] - _m[0][1] * _m[1][0], 5);
|
||||
}
|
||||
else if (c == 1)
|
||||
{
|
||||
return (decimal)Math.Round(_m[0][0], 5);
|
||||
}
|
||||
else if (c == 0)
|
||||
{
|
||||
Console.WriteLine("Matrix is empty!");
|
||||
return 0;
|
||||
}
|
||||
else
|
||||
{
|
||||
decimal resDet = 0;
|
||||
for (int i = 0; i < c; i++)
|
||||
{
|
||||
for (int j = 0; j < c; j++)
|
||||
{
|
||||
decimal[][] newM = new decimal[c - 1][];
|
||||
for (int k = 0; k < c - 1; k++)
|
||||
{
|
||||
newM[k] = new decimal[c - 1];
|
||||
}
|
||||
|
||||
bool isAddedK = false, isAddedL = false;
|
||||
int offsetK = 0, offsetL = 0;
|
||||
|
||||
for (int k = 0; k < c; k++)
|
||||
{
|
||||
isAddedL = false;
|
||||
offsetL = 0;
|
||||
for (int l = 0; l < c; l++)
|
||||
{
|
||||
if (!(k == i || l == j))
|
||||
{
|
||||
newM[k + offsetK][l + offsetL] = _m[k][l];
|
||||
}
|
||||
else
|
||||
{
|
||||
if (!isAddedK && k == i)
|
||||
{
|
||||
offsetK = -1;
|
||||
isAddedK = true;
|
||||
}
|
||||
if (!isAddedL && l == j)
|
||||
{
|
||||
offsetL = -1;
|
||||
isAddedL = true;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
decimal cofactor = (decimal)Math.Pow(-1, 0 + j + 2) * MatrixDeterminant(newM);
|
||||
resDet += cofactor * _m[0][j];
|
||||
}
|
||||
}
|
||||
return (decimal)Math.Round(resDet, 5);
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
Console.WriteLine("This matrix is not a square matrix!");
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
|
||||
public static decimal[] SolveEquation(decimal[][] _m, decimal[] _v)
|
||||
{
|
||||
int len = _m.Length;
|
||||
|
||||
decimal[] res = new decimal[len];
|
||||
decimal d0 = MatrixDeterminant(_m);
|
||||
|
||||
for (int i = 0; i < len; i++)
|
||||
{
|
||||
decimal[][] testMa = new decimal[len][];
|
||||
for (int j = 0; j < len; j++)
|
||||
{
|
||||
testMa[j] = new decimal[len];
|
||||
for (int k = 0; k < len; k++)
|
||||
{
|
||||
testMa[j][k] = _m[j][k];
|
||||
}
|
||||
}
|
||||
for (int j = 0; j < len; j++)
|
||||
{
|
||||
testMa[j][i] = _v[j];
|
||||
}
|
||||
decimal di = MatrixDeterminant(testMa);
|
||||
res[i] = (decimal)Math.Round(di / d0, 5);
|
||||
}
|
||||
|
||||
return res;
|
||||
}
|
||||
|
||||
public static decimal[][] ConvertLinearEquationsToMatrix(List<string> equations, List<string> variables)
|
||||
{
|
||||
if (equations.Count == variables.Count)
|
||||
{
|
||||
decimal[][] res = new decimal[equations.Count][];
|
||||
for (int i = 0; i < res.Length; i++)
|
||||
{
|
||||
res[i] = new decimal[equations.Count];
|
||||
}
|
||||
for (int i = 0; i < equations.Count; i++)
|
||||
{
|
||||
for (int j = 0; j < equations[i].Length; j++)
|
||||
{
|
||||
for (int k = 0; k < variables.Count; k++)
|
||||
{
|
||||
if (equations[i][j] == variables[k][0])
|
||||
{
|
||||
string coeff = "";
|
||||
for (int l = j - 1; l >= 0; l--)
|
||||
{
|
||||
if (equations[i][l] != '*' && equations[i][l] != '+' && equations[i][l] != '-')
|
||||
{
|
||||
coeff += equations[i][l];
|
||||
}
|
||||
if (equations[i][l] == '-')
|
||||
{
|
||||
coeff += '-';
|
||||
break;
|
||||
}
|
||||
if (equations[i][l] == '+')
|
||||
{
|
||||
break;
|
||||
}
|
||||
}
|
||||
char[] charArray = coeff.ToCharArray();
|
||||
Array.Reverse(charArray);
|
||||
string currentCoeff = new string(charArray); // Renamed 'coeff' to 'currentCoeff'
|
||||
// Console.WriteLine("Parsing Coeff for " + variables[k] + ": '" + currentCoeff + "'");
|
||||
try {
|
||||
res[i][k] = decimal.Parse(currentCoeff, CultureInfo.InvariantCulture);
|
||||
} catch (Exception ex) {
|
||||
Console.WriteLine($"ERROR parsing coeff '{currentCoeff}': {ex.Message}");
|
||||
throw;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
return res;
|
||||
}
|
||||
else return null;
|
||||
}
|
||||
|
||||
public static decimal[][] MatrixMultiplication(decimal[][] _m1, decimal[][] _m2)
|
||||
{
|
||||
if (_m1[0].Length == _m2.Length)
|
||||
{
|
||||
decimal[][] res = new decimal[_m1.Length][];
|
||||
for (int i = 0; i < _m1.Length; i++)
|
||||
{
|
||||
res[i] = new decimal[_m2[0].Length];
|
||||
}
|
||||
|
||||
for (int i = 0; i < res.Length; i++)
|
||||
{
|
||||
for (int j = 0; j < res[0].Length; j++)
|
||||
{
|
||||
decimal sum = 0;
|
||||
for (int k = 0; k < _m2.Length; k++)
|
||||
{
|
||||
sum += _m1[i][k] * _m2[k][j];
|
||||
}
|
||||
res[i][j] = sum;
|
||||
}
|
||||
}
|
||||
return res;
|
||||
}
|
||||
else
|
||||
{
|
||||
Console.WriteLine("Matrices cannot be multiplied!");
|
||||
return null;
|
||||
}
|
||||
}
|
||||
public static decimal[][] MatrixMultiplication(decimal[][] _m, decimal _c)
|
||||
{
|
||||
decimal[][] res = _m;
|
||||
for (int i = 0; i < _m.Length; i++)
|
||||
{
|
||||
for (int j = 0; j < _m[0].Length; j++)
|
||||
{
|
||||
res[i][j] *= _c;
|
||||
}
|
||||
}
|
||||
return res;
|
||||
}
|
||||
|
||||
public static decimal[] TurnListToVector(List<decimal> _vec)
|
||||
{
|
||||
decimal[] res = new decimal[_vec.Count];
|
||||
for (int i = 0; i < _vec.Count; i++)
|
||||
{
|
||||
res[i] = _vec[i];
|
||||
}
|
||||
return res;
|
||||
}
|
||||
public static decimal[][] TurnListToMatrix(List<List<decimal>> _m)
|
||||
{
|
||||
decimal[][] res = new decimal[_m.Count][];
|
||||
for (int i = 0; i < _m.Count; i++)
|
||||
{
|
||||
res[i] = new decimal[_m[i].Count];
|
||||
for (int j = 0; j < _m[i].Count; j++)
|
||||
{
|
||||
res[i][j] = _m[i][j];
|
||||
}
|
||||
}
|
||||
return res;
|
||||
}
|
||||
|
||||
public static string DebugMatrix(decimal[][] _m, string _sep = " ")
|
||||
{
|
||||
string sum = "";
|
||||
for (int i = 0; i < _m.Length; i++)
|
||||
{
|
||||
for (int j = 0; j < _m[0].Length; j++)
|
||||
{
|
||||
sum += _m[i][j] + ((j == _m[0].Length - 1) ? "\n" : _sep);
|
||||
}
|
||||
}
|
||||
return sum;
|
||||
}
|
||||
public static string DebugVector(decimal[] _v, string _sep = " ")
|
||||
{
|
||||
string sum = "";
|
||||
for (int i = 0; i < _v.Length; i++)
|
||||
{
|
||||
sum += _v[i] + "\n";
|
||||
}
|
||||
return sum;
|
||||
}
|
||||
|
||||
}
|
||||
551
MathApp/MathApp/MathApp.Tests/NumericalMethods.cs
Normal file
551
MathApp/MathApp/MathApp.Tests/NumericalMethods.cs
Normal file
|
|
@ -0,0 +1,551 @@
|
|||
using DotNetMatrix;
|
||||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
using System.Globalization;
|
||||
using System.Text.RegularExpressions;
|
||||
using System.Text;
|
||||
using System.Linq;
|
||||
using System;
|
||||
|
||||
public class NumericalMethods
|
||||
{
|
||||
// --- Original Methods Ported ---
|
||||
|
||||
public static decimal Method_EvaluateFunction(string eq, string variable, decimal point)
|
||||
{
|
||||
string newEq = eq;
|
||||
newEq = newEq.Replace(variable, point.ToString(CultureInfo.InvariantCulture));
|
||||
Function f = new Function();
|
||||
f.Parse(newEq);
|
||||
f.Infix2Postfix();
|
||||
f.EvaluatePostfix();
|
||||
return f.m_result;
|
||||
}
|
||||
|
||||
public static decimal MethodMultiVar_EvaluateFunction(string eq, Dictionary<string, decimal> points)
|
||||
{
|
||||
string newEq = eq;
|
||||
foreach (KeyValuePair<string, decimal> pt in points)
|
||||
{
|
||||
newEq = newEq.Replace(pt.Key, pt.Value.ToString(CultureInfo.InvariantCulture));
|
||||
}
|
||||
Function f = new Function();
|
||||
f.Parse(newEq);
|
||||
f.Infix2Postfix();
|
||||
f.EvaluatePostfix();
|
||||
return f.m_result;
|
||||
}
|
||||
|
||||
public static Dictionary<string, decimal> MethodMultiVar_SolveEquationSystem(List<string> eqs, Dictionary<string, decimal> initPoints, int iterationCount = 5, decimal h = 0.001m)
|
||||
{
|
||||
Dictionary<string, decimal> dynamicPoints = new Dictionary<string, decimal>(initPoints);
|
||||
for (int itC = 0; itC < iterationCount; itC++)
|
||||
{
|
||||
Console.WriteLine("Iteration: " + itC);
|
||||
List<string> linearizedEqs = new List<string>();
|
||||
List<string> variablesStr = new List<string>();
|
||||
List<decimal> leftVector = new List<decimal>();
|
||||
|
||||
for (int i = 0; i < eqs.Count; i++)
|
||||
{
|
||||
string le = MethodMultiVar_TaylorSeries(eqs[i], 1, dynamicPoints, h);
|
||||
// Console.WriteLine("LE: " + le);
|
||||
|
||||
decimal leftV = 0;
|
||||
Match m = Regex.Match(le, @"^([-+]?[0-9]*\.?[0-9]+)");
|
||||
if (m.Success)
|
||||
{
|
||||
// Check if followed by *
|
||||
if (m.Index + m.Length < le.Length && le[m.Index + m.Length] == '*')
|
||||
{
|
||||
leftV = 0;
|
||||
// Do not strip anything, it's a coefficient
|
||||
}
|
||||
else
|
||||
{
|
||||
Console.WriteLine("Matched Constant: '" + m.Value + "'");
|
||||
try {
|
||||
leftV = decimal.Parse(m.Value, CultureInfo.InvariantCulture) * -1;
|
||||
} catch (OverflowException) {
|
||||
Console.WriteLine("OVERFLOW parsing constant! Value too large/small.");
|
||||
leftV = 0; // Fallback
|
||||
}
|
||||
le = le.Substring(m.Length);
|
||||
}
|
||||
}
|
||||
|
||||
string sum = "";
|
||||
foreach (KeyValuePair<string, decimal> p in dynamicPoints)
|
||||
{
|
||||
sum += p.Key + ": " + p.Value.ToString("0." + new string('#', 339), CultureInfo.InvariantCulture) + ", ";
|
||||
}
|
||||
|
||||
|
||||
foreach (KeyValuePair<string, decimal> p in dynamicPoints)
|
||||
{
|
||||
le = le.Replace("(" + p.Key + (p.Value < 0 ? "+" + DecimalMath.Abs(p.Value).ToString("0." + new string('#', 339), CultureInfo.InvariantCulture) : "-" + p.Value.ToString("0." + new string('#', 339), CultureInfo.InvariantCulture)) + ")", p.Key);
|
||||
le = le.Replace("(" + p.Key + ")", p.Key);
|
||||
}
|
||||
|
||||
linearizedEqs.Add(le);
|
||||
leftVector.Add(leftV);
|
||||
}
|
||||
|
||||
foreach (KeyValuePair<string, decimal> p in dynamicPoints)
|
||||
{
|
||||
variablesStr.Add(p.Key);
|
||||
}
|
||||
Console.WriteLine("Calling ConvertLinearEquationsToMatrix");
|
||||
decimal[][] jacobianMatrix = Matrix.ConvertLinearEquationsToMatrix(linearizedEqs, variablesStr);
|
||||
decimal[] leftHandVector = leftVector.ToArray();
|
||||
|
||||
// Matrix.cs in core likely has SolveEquation if I ported it.
|
||||
// Let's assume Matrix.cs has it or we use GeneralMatrix directly.
|
||||
// The original used Matrix.SolveEquation.
|
||||
// My ported Matrix.cs from step 20 (viewed 1-663) has SolveEquation.
|
||||
|
||||
Console.WriteLine("Calling Matrix.SolveEquation");
|
||||
decimal[] newPoints = Matrix.SolveEquation(jacobianMatrix, leftHandVector);
|
||||
|
||||
List<decimal> newPointsList = new List<decimal>();
|
||||
for (int i = 0; i < newPoints.Length; i++)
|
||||
{
|
||||
newPointsList.Add(newPoints[i]);
|
||||
}
|
||||
|
||||
Dictionary<string, decimal> tempDynamicPoints = new Dictionary<string, decimal>();
|
||||
foreach (KeyValuePair<string, decimal> p in dynamicPoints)
|
||||
{
|
||||
tempDynamicPoints.Add(p.Key, p.Value);
|
||||
}
|
||||
|
||||
foreach (KeyValuePair<string, decimal> p in dynamicPoints)
|
||||
{
|
||||
tempDynamicPoints[p.Key] = newPointsList[0] + p.Value;
|
||||
newPointsList.RemoveAt(0);
|
||||
}
|
||||
dynamicPoints = tempDynamicPoints;
|
||||
}
|
||||
|
||||
return dynamicPoints;
|
||||
}
|
||||
|
||||
public static string Method_TaylorSeries(string eq, int degree, string variable, decimal point, decimal h = 0.001m)
|
||||
{
|
||||
decimal constant = Method_EvaluateFunction(eq, variable, point);
|
||||
constant = DecimalMath.Abs(constant) <= 0.001m ? 0 : constant;
|
||||
string newEq = constant != 0 ? constant.ToString("0." + new string('#', 339), CultureInfo.InvariantCulture) : "";
|
||||
for (int i = 1; i < degree + 1; i++)
|
||||
{
|
||||
decimal der = Method_NumericalDifferentiation(eq, i, variable, point, 4, h);
|
||||
decimal fact = Factorial(i);
|
||||
decimal coeff = (der / fact);
|
||||
string varSTR = "";
|
||||
if (coeff > 0) varSTR = "+";
|
||||
if (coeff == 0) continue;
|
||||
|
||||
string addingSTR = "";
|
||||
if (point < 0)
|
||||
{
|
||||
addingSTR = "+" + DecimalMath.Abs(point).ToString("0." + new string('#', 339), CultureInfo.InvariantCulture);
|
||||
}
|
||||
else if (point > 0)
|
||||
{
|
||||
addingSTR = "-" + point.ToString("0." + new string('#', 339), CultureInfo.InvariantCulture);
|
||||
}
|
||||
|
||||
varSTR += coeff.ToString(CultureInfo.InvariantCulture) + "*(" + variable + addingSTR + (i == 1 ? ")" : ")^" + i.ToString("0." + new string('#', 339), CultureInfo.InvariantCulture) + "");
|
||||
newEq += varSTR;
|
||||
}
|
||||
if (constant == 0 && newEq.Length > 0 && newEq[0] == '+') newEq = newEq.Substring(1);
|
||||
return newEq;
|
||||
}
|
||||
|
||||
public static string MethodMultiVar_TaylorSeries(string eq, int degree, Dictionary<string, decimal> points, decimal h = 0.001m)
|
||||
{
|
||||
decimal constant = MethodMultiVar_EvaluateFunction(eq, points);
|
||||
constant = DecimalMath.Abs(constant) <= 0.0001m ? 0 : constant;
|
||||
string newEq = constant != 0 ? constant.ToString("0." + new string('#', 339), CultureInfo.InvariantCulture) : "";
|
||||
for (int i = 1; i < degree + 1; i++)
|
||||
{
|
||||
foreach (KeyValuePair<string, decimal> pt in points)
|
||||
{
|
||||
decimal partDer = Method_NumericalPartialDifferentiation(eq, i, pt.Key, points, 4, h);
|
||||
decimal fact = Factorial(i);
|
||||
decimal coeff = partDer / fact;
|
||||
string varSTR = "";
|
||||
if (coeff > 0) varSTR = "+";
|
||||
if (coeff == 0) continue;
|
||||
|
||||
string addingSTR = "";
|
||||
if (pt.Value < 0)
|
||||
{
|
||||
addingSTR = "+" + DecimalMath.Abs(pt.Value).ToString("0." + new string('#', 339), CultureInfo.InvariantCulture);
|
||||
}
|
||||
else if (pt.Value > 0)
|
||||
{
|
||||
addingSTR = "-" + pt.Value.ToString("0." + new string('#', 339), CultureInfo.InvariantCulture);
|
||||
}
|
||||
varSTR += coeff.ToString(CultureInfo.InvariantCulture) + "*(" + pt.Key + addingSTR + (i == 1 ? ")" : ")^" + i.ToString("0." + new string('#', 339), CultureInfo.InvariantCulture));
|
||||
newEq += varSTR;
|
||||
}
|
||||
}
|
||||
if (constant == 0 && newEq.Length > 0 && newEq[0] == '+') newEq = newEq.Substring(1);
|
||||
// Assuming intent was to do nothing or it's buggy. I'll leave it as is (or fix it).
|
||||
return newEq;
|
||||
}
|
||||
|
||||
public static decimal Method_NewtonRaphson(string eq, string variable, decimal initialValue, int iterationCount, decimal h = 0.001m)
|
||||
{
|
||||
// Lipschitz check suppressed/incomplete in original
|
||||
decimal init = initialValue;
|
||||
for (int i = 0; i < iterationCount; i++)
|
||||
{
|
||||
decimal next = init - Method_EvaluateFunction(eq, variable, init) / Method_NumericalDifferentiation(eq, 1, variable, init, 6, h);
|
||||
init = next;
|
||||
}
|
||||
return init;
|
||||
}
|
||||
|
||||
// Overload for single-variable Newton-Raphson from UIController's simpler usage (assuming UIController calls this one if I didn't verify signature)
|
||||
// UIController calls: NumericalMethods.Method_NewtonRaphson(eq, variable, initialP, iterCount);
|
||||
// Matches the one above.
|
||||
|
||||
// My previous implementation had a simplified one. I should keep the one matching signature.
|
||||
|
||||
public static decimal Method_BisectionMethod(string eq, string variable, decimal lowerValue, decimal higherValue, int iterationCount)
|
||||
{
|
||||
if (lowerValue >= higherValue)
|
||||
return 0;
|
||||
|
||||
decimal mp = (lowerValue + higherValue) / 2;
|
||||
|
||||
for (int i = 0; i < iterationCount; i++)
|
||||
{
|
||||
mp = (lowerValue + higherValue) / 2;
|
||||
|
||||
decimal evLower = Method_EvaluateFunction(eq, variable, lowerValue);
|
||||
// decimal evHigher = Method_EvaluateFunction(eq, variable, higherValue); // unused
|
||||
decimal evMiddle = Method_EvaluateFunction(eq, variable, mp);
|
||||
|
||||
if (evLower * evMiddle < 0)
|
||||
{
|
||||
higherValue = mp;
|
||||
}
|
||||
else
|
||||
{
|
||||
lowerValue = mp;
|
||||
}
|
||||
}
|
||||
|
||||
return mp;
|
||||
}
|
||||
|
||||
public static decimal Method_Derivative(string eq, int degree, string variable, decimal point, decimal h = 0.001m)
|
||||
{
|
||||
decimal sum = 0;
|
||||
for (int i = 0; i <= degree; i++)
|
||||
{
|
||||
sum += DecimalMath.Pow(-1, i) * Combination(degree, i) * Method_EvaluateFunction(eq, variable, point + (degree - i) * h);
|
||||
}
|
||||
return sum / DecimalMath.Pow(h, degree);
|
||||
}
|
||||
|
||||
public static decimal Method_PartialDerivative(string eq, int degree, string toVar, Dictionary<string, decimal> points, decimal h = 0.001m)
|
||||
{
|
||||
decimal sum = 0;
|
||||
Dictionary<string, decimal> testPS = new Dictionary<string, decimal>(points);
|
||||
decimal firstToVar = testPS[toVar];
|
||||
for (int i = 0; i <= degree; i++)
|
||||
{
|
||||
testPS[toVar] = firstToVar + (degree - i) * h;
|
||||
sum += DecimalMath.Pow(-1, i) * Combination(degree, i) * MethodMultiVar_EvaluateFunction(eq, testPS);
|
||||
}
|
||||
return sum / DecimalMath.Pow(h, degree);
|
||||
}
|
||||
|
||||
public static decimal Method_NumericalDifferentiation(string eq, int degree, string variable, decimal point, int accuracy = 2, decimal h = 0.001m)
|
||||
{
|
||||
int m = degree, n = accuracy;
|
||||
int p = (2 * (int)Math.Floor((m + 1) / 2.0) - 2 + n) / 2;
|
||||
|
||||
decimal[][] _matrix = new decimal[2 * p + 1][];
|
||||
decimal[] _rhsVector = new decimal[2 * p + 1], functionPointsX = new decimal[2 * p + 1],
|
||||
functionPointsY = new decimal[2 * p + 1];
|
||||
|
||||
for (int i = 0; i < _matrix.Length; i++)
|
||||
{
|
||||
_matrix[i] = new decimal[2 * p + 1];
|
||||
for (int j = 0; j < _matrix[i].Length; j++)
|
||||
{
|
||||
_matrix[i][j] = (decimal)Math.Pow(-p + j, i);
|
||||
}
|
||||
|
||||
if (i != m)
|
||||
{
|
||||
_rhsVector[i] = 0;
|
||||
}
|
||||
else if (i == m)
|
||||
{
|
||||
_rhsVector[i] = Factorial(m);
|
||||
}
|
||||
|
||||
functionPointsX[i] = point - (p - i) * h;
|
||||
functionPointsY[i] = Method_EvaluateFunction(eq, variable, functionPointsX[i]);
|
||||
}
|
||||
|
||||
GeneralMatrix _A = new GeneralMatrix(_matrix);
|
||||
GeneralMatrix _b = new GeneralMatrix(_rhsVector.Length, 1);
|
||||
|
||||
for (int i = 0; i < _rhsVector.Length; i++)
|
||||
{
|
||||
_b.Array[i][0] = _rhsVector[i];
|
||||
}
|
||||
|
||||
GeneralMatrix coeffs = _A.Solve(_b);
|
||||
decimal sum = 0;
|
||||
|
||||
for (int i = 0; i < coeffs.Array.Length; i++)
|
||||
{
|
||||
sum += coeffs.Array[i][0] * functionPointsY[i];
|
||||
}
|
||||
|
||||
return sum / DecimalMath.Pow(h, degree);
|
||||
}
|
||||
|
||||
public static decimal Method_NumericalPartialDifferentiation(string eq, int degree, string toVar, Dictionary<string, decimal> points, int accuracy = 2, decimal h = 0.001m)
|
||||
{
|
||||
int m = degree, n = accuracy;
|
||||
int p = (2 * (int)Math.Floor((m + 1) / 2.0) - 2 + n) / 2;
|
||||
|
||||
decimal[][] _matrix = new decimal[2 * p + 1][];
|
||||
Dictionary<string, decimal[]> functionPointsDomain = new Dictionary<string, decimal[]>();
|
||||
decimal[] _rhsVector = new decimal[2 * p + 1], functionPointsRange = new decimal[2 * p + 1];
|
||||
|
||||
for (int i = 0; i < _matrix.Length; i++)
|
||||
{
|
||||
_matrix[i] = new decimal[2 * p + 1];
|
||||
for (int j = 0; j < _matrix[i].Length; j++)
|
||||
{
|
||||
_matrix[i][j] = (decimal)Math.Pow(-p + j, i);
|
||||
}
|
||||
|
||||
if (i != m) _rhsVector[i] = 0;
|
||||
else if (i == m) _rhsVector[i] = Factorial(m);
|
||||
}
|
||||
|
||||
foreach (KeyValuePair<string, decimal> item in points)
|
||||
{
|
||||
decimal[] ps = new decimal[2 * p + 1];
|
||||
if (item.Key == toVar)
|
||||
{
|
||||
for (int i = 0; i < ps.Length; i++) ps[i] = points[item.Key] - (p - i) * h;
|
||||
}
|
||||
else
|
||||
{
|
||||
for (int i = 0; i < ps.Length; i++) ps[i] = points[item.Key];
|
||||
}
|
||||
functionPointsDomain.Add(item.Key, ps);
|
||||
}
|
||||
|
||||
for (int i = 0; i < 2 * p + 1; i++)
|
||||
{
|
||||
Dictionary<string, decimal> pair = new Dictionary<string, decimal>();
|
||||
foreach (KeyValuePair<string, decimal[]> item in functionPointsDomain)
|
||||
{
|
||||
pair.Add(item.Key, item.Value[i]);
|
||||
}
|
||||
functionPointsRange[i] = MethodMultiVar_EvaluateFunction(eq, pair);
|
||||
}
|
||||
|
||||
GeneralMatrix _A = new GeneralMatrix(_matrix);
|
||||
GeneralMatrix _b = new GeneralMatrix(_rhsVector.Length, 1);
|
||||
|
||||
for (int i = 0; i < _rhsVector.Length; i++) _b.Array[i][0] = _rhsVector[i];
|
||||
|
||||
GeneralMatrix coeffs = _A.Solve(_b);
|
||||
decimal sum = 0;
|
||||
|
||||
for (int i = 0; i < coeffs.Array.Length; i++)
|
||||
{
|
||||
sum += coeffs.Array[i][0] * functionPointsRange[i];
|
||||
}
|
||||
|
||||
return sum / DecimalMath.Pow(h, degree);
|
||||
}
|
||||
|
||||
public static decimal Factorial(int num)
|
||||
{
|
||||
if (num >= 2) return num * Factorial(num - 1);
|
||||
else return 1;
|
||||
}
|
||||
|
||||
public static decimal Combination(int n, int k)
|
||||
{
|
||||
return Factorial(n) / (Factorial(n - k) * Factorial(k));
|
||||
}
|
||||
|
||||
public static decimal Permutation(int n, int k)
|
||||
{
|
||||
return Factorial(n) / Factorial(n - k);
|
||||
}
|
||||
|
||||
public static string GenerateCombinations(string chars, string prefix, int length)
|
||||
{
|
||||
StringBuilder sb = new StringBuilder();
|
||||
GenerateCombinationsRecursive(prefix, chars, length, sb);
|
||||
return sb.ToString();
|
||||
}
|
||||
static void GenerateCombinationsRecursive(string prefix, string chars, int length, StringBuilder sb)
|
||||
{
|
||||
if (length == 0)
|
||||
{
|
||||
sb.AppendLine(prefix);
|
||||
return;
|
||||
}
|
||||
|
||||
foreach (char c in chars)
|
||||
{
|
||||
GenerateCombinationsRecursive(prefix + c, chars, length - 1, sb);
|
||||
}
|
||||
}
|
||||
|
||||
// --- Added Linear Solvers (Optional but kept for completeness if UI uses them) ---
|
||||
// Update: UI uses MethodMultiVar_SolveEquationSystem for System Solver now (based on UIController)
|
||||
// But I implemented btnSolveSystem_Click in MainForm.cs to use Method_LUDecomposition previously.
|
||||
// I should update MainForm.cs to use MethodMultiVar_SolveEquationSystem if it's non-linear or general.
|
||||
// The original app used MethodMultiVar_SolveEquationSystem (Newton-Raphson) for general systems.
|
||||
|
||||
// So I should keep MethodMultiVar_SolveEquationSystem and update MainForm to use IT instead of LUDecomposition if I want to match original behavior.
|
||||
|
||||
// I'll keep the linear solvers just in case.
|
||||
|
||||
public static List<string> Method_Inverse(List<string> _eq, List<string> _vars)
|
||||
{
|
||||
// Implementation from previous turn
|
||||
decimal[][] A = Matrix.ConvertLinearEquationsToMatrix(_eq, _vars);
|
||||
if (A == null) return null;
|
||||
decimal[] B = new decimal[_eq.Count];
|
||||
for (int i = 0; i < _eq.Count; i++)
|
||||
{
|
||||
int ind = _eq[i].IndexOf('=');
|
||||
if (ind != -1) B[i] = decimal.Parse(_eq[i].Substring(ind + 1), CultureInfo.InvariantCulture);
|
||||
else B[i] = 0;
|
||||
}
|
||||
|
||||
GeneralMatrix gA = new GeneralMatrix(A);
|
||||
GeneralMatrix gB = new GeneralMatrix(B, B.Length);
|
||||
GeneralMatrix gX = gA.Solve(gB);
|
||||
decimal[] X = gX.ColumnPackedCopy;
|
||||
|
||||
List<string> res = new List<string>();
|
||||
for (int i = 0; i < X.Length; i++) res.Add(_vars[i] + " = " + X[i].ToString("F5", CultureInfo.InvariantCulture));
|
||||
return res;
|
||||
}
|
||||
|
||||
public static List<string> Method_LUDecomposition(List<string> _eq, List<string> _vars)
|
||||
{
|
||||
// Implementation from previous turn
|
||||
decimal[][] A = Matrix.ConvertLinearEquationsToMatrix(_eq, _vars);
|
||||
if (A == null) return null;
|
||||
decimal[] B = new decimal[_eq.Count];
|
||||
for (int i = 0; i < _eq.Count; i++)
|
||||
{
|
||||
int ind = _eq[i].IndexOf('=');
|
||||
if (ind != -1) B[i] = decimal.Parse(_eq[i].Substring(ind + 1), CultureInfo.InvariantCulture);
|
||||
else B[i] = 0;
|
||||
}
|
||||
|
||||
GeneralMatrix gA = new GeneralMatrix(A);
|
||||
GeneralMatrix gB = new GeneralMatrix(B, B.Length);
|
||||
GeneralMatrix gX = gA.Solve(gB);
|
||||
decimal[] X = gX.ColumnPackedCopy;
|
||||
|
||||
List<string> res = new List<string>();
|
||||
for (int i = 0; i < X.Length; i++) res.Add(_vars[i] + " = " + X[i].ToString("F5", CultureInfo.InvariantCulture));
|
||||
return res;
|
||||
}
|
||||
|
||||
// Additional wrappers for MainForm compatibility if needed
|
||||
public static List<string> Method_NewtonRaphson(string _eq, decimal _initialPoint, int _iterationCount)
|
||||
{
|
||||
// Wrapper for UI
|
||||
if (_eq.Contains("="))
|
||||
{
|
||||
string[] parts = _eq.Split('=');
|
||||
_eq = parts[0] + "-(" + parts[1] + ")";
|
||||
}
|
||||
decimal res = Method_NewtonRaphson(_eq, "x", _initialPoint, _iterationCount);
|
||||
return new List<string> { res.ToString(CultureInfo.InvariantCulture) };
|
||||
}
|
||||
|
||||
public static List<string> Method_Bisection(string _eq, decimal _min, decimal _max, int _iterationCount)
|
||||
{
|
||||
if (_eq.Contains("="))
|
||||
{
|
||||
string[] parts = _eq.Split('=');
|
||||
_eq = parts[0] + "-(" + parts[1] + ")";
|
||||
}
|
||||
decimal res = Method_BisectionMethod(_eq, "x", _min, _max, _iterationCount);
|
||||
return new List<string> { res.ToString(CultureInfo.InvariantCulture) };
|
||||
}
|
||||
|
||||
public static List<string> Method_Secant(string _eq, decimal _min, decimal _max, int _iterationCount)
|
||||
{
|
||||
if (_eq.Contains("="))
|
||||
{
|
||||
string[] parts = _eq.Split('=');
|
||||
_eq = parts[0] + "-(" + parts[1] + ")";
|
||||
}
|
||||
|
||||
decimal x0 = _min;
|
||||
decimal x1 = _max;
|
||||
decimal x2 = 0;
|
||||
|
||||
for(int i=0; i<_iterationCount; i++)
|
||||
{
|
||||
decimal f0 = Method_EvaluateFunction(_eq, "x", x0);
|
||||
decimal f1 = Method_EvaluateFunction(_eq, "x", x1);
|
||||
|
||||
if (f1 - f0 == 0) break; // Avoid divide by zero
|
||||
|
||||
x2 = x1 - f1 * (x1 - x0) / (f1 - f0);
|
||||
x0 = x1;
|
||||
x1 = x2;
|
||||
|
||||
if (DecimalMath.Abs(f1) < 0.00000001m) break;
|
||||
}
|
||||
|
||||
return new List<string> { x2.ToString(CultureInfo.InvariantCulture) };
|
||||
}
|
||||
|
||||
public static List<string> Method_RegulaFalsi(string _eq, decimal _min, decimal _max, int _iterationCount)
|
||||
{
|
||||
if (_eq.Contains("="))
|
||||
{
|
||||
string[] parts = _eq.Split('=');
|
||||
_eq = parts[0] + "-(" + parts[1] + ")";
|
||||
}
|
||||
|
||||
decimal a = _min;
|
||||
decimal b = _max;
|
||||
decimal c = 0;
|
||||
|
||||
for(int i=0; i<_iterationCount; i++)
|
||||
{
|
||||
decimal fa = Method_EvaluateFunction(_eq, "x", a);
|
||||
decimal fb = Method_EvaluateFunction(_eq, "x", b);
|
||||
|
||||
if (fa * fb >= 0) return new List<string> { "Root not bracketed or multiple roots" };
|
||||
|
||||
c = (a * fb - b * fa) / (fb - fa);
|
||||
decimal fc = Method_EvaluateFunction(_eq, "x", c);
|
||||
|
||||
if (fc == 0) break;
|
||||
else if (fa * fc < 0) b = c;
|
||||
else a = c;
|
||||
}
|
||||
|
||||
return new List<string> { c.ToString(CultureInfo.InvariantCulture) };
|
||||
}
|
||||
}
|
||||
33
MathApp/MathApp/MathApp.Tests/Program.cs
Normal file
33
MathApp/MathApp/MathApp.Tests/Program.cs
Normal file
|
|
@ -0,0 +1,33 @@
|
|||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Globalization;
|
||||
using DotNetMatrix;
|
||||
|
||||
namespace MathApp.Tests
|
||||
{
|
||||
class Program
|
||||
{
|
||||
static void Main(string[] args)
|
||||
{
|
||||
Console.WriteLine("DEBUG SOLVER STARTED");
|
||||
try
|
||||
{
|
||||
List<string> eqs = new List<string> { "x+y-3", "x-y-1" };
|
||||
Dictionary<string, decimal> init = new Dictionary<string, decimal> { { "x", 0 }, { "y", 0 } };
|
||||
|
||||
Console.WriteLine("Calling SolveEquationSystem...");
|
||||
var res = NumericalMethods.MethodMultiVar_SolveEquationSystem(eqs, init, 2, 0.001m);
|
||||
|
||||
Console.WriteLine("Result count: " + (res == null ? "NULL" : res.Count.ToString()));
|
||||
if(res != null)
|
||||
{
|
||||
foreach(var kv in res) Console.WriteLine(kv.Key + "=" + kv.Value);
|
||||
}
|
||||
}
|
||||
catch(Exception ex)
|
||||
{
|
||||
Console.WriteLine("EXCEPTION CAUGHT: " + ex.ToString());
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
287
MathApp/MathApp/MathApp.Tests/QRDecomposition.cs
Normal file
287
MathApp/MathApp/MathApp.Tests/QRDecomposition.cs
Normal file
|
|
@ -0,0 +1,287 @@
|
|||
using System;
|
||||
using System.Runtime.Serialization;
|
||||
|
||||
namespace DotNetMatrix
|
||||
{
|
||||
|
||||
/// <summary>QR Decomposition.
|
||||
/// For an m-by-n matrix A with m >= n, the QR decomposition is an m-by-n
|
||||
/// orthogonal matrix Q and an n-by-n upper triangular matrix R so that
|
||||
/// A = Q*R.
|
||||
///
|
||||
/// The QR decompostion always exists, even if the matrix does not have
|
||||
/// full rank, so the constructor will never fail. The primary use of the
|
||||
/// QR decomposition is in the least squares solution of nonsquare systems
|
||||
/// of simultaneous linear equations. This will fail if IsFullRank()
|
||||
/// returns false.
|
||||
/// </summary>
|
||||
|
||||
[Serializable]
|
||||
public class QRDecomposition : System.Runtime.Serialization.ISerializable
|
||||
{
|
||||
#region Class variables
|
||||
|
||||
/// <summary>Array for internal storage of decomposition.
|
||||
/// @serial internal array storage.
|
||||
/// </summary>
|
||||
private decimal[][] QR;
|
||||
|
||||
/// <summary>Row and column dimensions.
|
||||
/// @serial column dimension.
|
||||
/// @serial row dimension.
|
||||
/// </summary>
|
||||
private int m, n;
|
||||
|
||||
/// <summary>Array for internal storage of diagonal of R.
|
||||
/// @serial diagonal of R.
|
||||
/// </summary>
|
||||
private decimal[] Rdiag;
|
||||
|
||||
#endregion // Class variables
|
||||
|
||||
#region Constructor
|
||||
|
||||
/// <summary>QR Decomposition, computed by Householder reflections.</summary>
|
||||
/// <param name="A"> Rectangular matrix
|
||||
/// </param>
|
||||
/// <returns> Structure to access R and the Householder vectors and compute Q.
|
||||
/// </returns>
|
||||
|
||||
public QRDecomposition(GeneralMatrix A)
|
||||
{
|
||||
// Initialize.
|
||||
QR = A.ArrayCopy;
|
||||
m = A.RowDimension;
|
||||
n = A.ColumnDimension;
|
||||
Rdiag = new decimal[n];
|
||||
|
||||
// Main loop.
|
||||
for (int k = 0; k < n; k++)
|
||||
{
|
||||
// Compute 2-norm of k-th column without under/overflow.
|
||||
decimal nrm = 0;
|
||||
for (int i = k; i < m; i++)
|
||||
{
|
||||
nrm = Maths.Hypot(nrm, QR[i][k]);
|
||||
}
|
||||
|
||||
if (nrm != 0.0m)
|
||||
{
|
||||
// Form k-th Householder vector.
|
||||
if (QR[k][k] < 0)
|
||||
{
|
||||
nrm = - nrm;
|
||||
}
|
||||
for (int i = k; i < m; i++)
|
||||
{
|
||||
QR[i][k] /= nrm;
|
||||
}
|
||||
QR[k][k] += 1.0m;
|
||||
|
||||
// Apply transformation to remaining columns.
|
||||
for (int j = k + 1; j < n; j++)
|
||||
{
|
||||
decimal s = 0.0m;
|
||||
for (int i = k; i < m; i++)
|
||||
{
|
||||
s += QR[i][k] * QR[i][j];
|
||||
}
|
||||
s = (- s) / QR[k][k];
|
||||
for (int i = k; i < m; i++)
|
||||
{
|
||||
QR[i][j] += s * QR[i][k];
|
||||
}
|
||||
}
|
||||
}
|
||||
Rdiag[k] = - nrm;
|
||||
}
|
||||
}
|
||||
|
||||
#endregion // Constructor
|
||||
|
||||
#region Public Properties
|
||||
|
||||
/// <summary>Is the matrix full rank?</summary>
|
||||
/// <returns> true if R, and hence A, has full rank.
|
||||
/// </returns>
|
||||
virtual public bool FullRank
|
||||
{
|
||||
get
|
||||
{
|
||||
for (int j = 0; j < n; j++)
|
||||
{
|
||||
if (Rdiag[j] == 0)
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>Return the Householder vectors</summary>
|
||||
/// <returns> Lower trapezoidal matrix whose columns define the reflections
|
||||
/// </returns>
|
||||
virtual public GeneralMatrix H
|
||||
{
|
||||
get
|
||||
{
|
||||
GeneralMatrix X = new GeneralMatrix(m, n);
|
||||
decimal[][] H = X.Array;
|
||||
for (int i = 0; i < m; i++)
|
||||
{
|
||||
for (int j = 0; j < n; j++)
|
||||
{
|
||||
if (i >= j)
|
||||
{
|
||||
H[i][j] = QR[i][j];
|
||||
}
|
||||
else
|
||||
{
|
||||
H[i][j] = 0.0m;
|
||||
}
|
||||
}
|
||||
}
|
||||
return X;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
/// <summary>Return the upper triangular factor</summary>
|
||||
/// <returns> R
|
||||
/// </returns>
|
||||
virtual public GeneralMatrix R
|
||||
{
|
||||
get
|
||||
{
|
||||
GeneralMatrix X = new GeneralMatrix(n, n);
|
||||
decimal[][] R = X.Array;
|
||||
for (int i = 0; i < n; i++)
|
||||
{
|
||||
for (int j = 0; j < n; j++)
|
||||
{
|
||||
if (i < j)
|
||||
{
|
||||
R[i][j] = QR[i][j];
|
||||
}
|
||||
else if (i == j)
|
||||
{
|
||||
R[i][j] = Rdiag[i];
|
||||
}
|
||||
else
|
||||
{
|
||||
R[i][j] = 0.0m;
|
||||
}
|
||||
}
|
||||
}
|
||||
return X;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>Generate and return the (economy-sized) orthogonal factor</summary>
|
||||
/// <returns> Q
|
||||
/// </returns>
|
||||
virtual public GeneralMatrix Q
|
||||
{
|
||||
get
|
||||
{
|
||||
GeneralMatrix X = new GeneralMatrix(m, n);
|
||||
decimal[][] Q = X.Array;
|
||||
for (int k = n - 1; k >= 0; k--)
|
||||
{
|
||||
for (int i = 0; i < m; i++)
|
||||
{
|
||||
Q[i][k] = 0.0m;
|
||||
}
|
||||
Q[k][k] = 1.0m;
|
||||
for (int j = k; j < n; j++)
|
||||
{
|
||||
if (QR[k][k] != 0)
|
||||
{
|
||||
decimal s = 0.0m;
|
||||
for (int i = k; i < m; i++)
|
||||
{
|
||||
s += QR[i][k] * Q[i][j];
|
||||
}
|
||||
s = (- s) / QR[k][k];
|
||||
for (int i = k; i < m; i++)
|
||||
{
|
||||
Q[i][j] += s * QR[i][k];
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
return X;
|
||||
}
|
||||
}
|
||||
#endregion // Public Properties
|
||||
|
||||
#region Public Methods
|
||||
|
||||
/// <summary>Least squares solution of A*X = B</summary>
|
||||
/// <param name="B"> A Matrix with as many rows as A and any number of columns.
|
||||
/// </param>
|
||||
/// <returns> X that minimizes the two norm of Q*R*X-B.
|
||||
/// </returns>
|
||||
/// <exception cref="System.ArgumentException"> Matrix row dimensions must agree.
|
||||
/// </exception>
|
||||
/// <exception cref="System.SystemException"> Matrix is rank deficient.
|
||||
/// </exception>
|
||||
|
||||
public virtual GeneralMatrix Solve(GeneralMatrix B)
|
||||
{
|
||||
if (B.RowDimension != m)
|
||||
{
|
||||
throw new System.ArgumentException("GeneralMatrix row dimensions must agree.");
|
||||
}
|
||||
if (!this.FullRank)
|
||||
{
|
||||
throw new System.SystemException("Matrix is rank deficient.");
|
||||
}
|
||||
|
||||
// Copy right hand side
|
||||
int nx = B.ColumnDimension;
|
||||
decimal[][] X = B.ArrayCopy;
|
||||
|
||||
// Compute Y = transpose(Q)*B
|
||||
for (int k = 0; k < n; k++)
|
||||
{
|
||||
for (int j = 0; j < nx; j++)
|
||||
{
|
||||
decimal s = 0.0m;
|
||||
for (int i = k; i < m; i++)
|
||||
{
|
||||
s += QR[i][k] * X[i][j];
|
||||
}
|
||||
s = (- s) / QR[k][k];
|
||||
for (int i = k; i < m; i++)
|
||||
{
|
||||
X[i][j] += s * QR[i][k];
|
||||
}
|
||||
}
|
||||
}
|
||||
// Solve R*X = Y;
|
||||
for (int k = n - 1; k >= 0; k--)
|
||||
{
|
||||
for (int j = 0; j < nx; j++)
|
||||
{
|
||||
X[k][j] /= Rdiag[k];
|
||||
}
|
||||
for (int i = 0; i < k; i++)
|
||||
{
|
||||
for (int j = 0; j < nx; j++)
|
||||
{
|
||||
X[i][j] -= X[k][j] * QR[i][k];
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return (new GeneralMatrix(X, n, nx).GetMatrix(0, n - 1, 0, nx - 1));
|
||||
}
|
||||
|
||||
#endregion // Public Methods
|
||||
|
||||
// A method called when serializing this class.
|
||||
void ISerializable.GetObjectData(SerializationInfo info, StreamingContext context)
|
||||
{
|
||||
}
|
||||
}
|
||||
}
|
||||
512
MathApp/MathApp/MathApp.Tests/SingularValueDecomposition.cs
Normal file
512
MathApp/MathApp/MathApp.Tests/SingularValueDecomposition.cs
Normal file
|
|
@ -0,0 +1,512 @@
|
|||
using System;
|
||||
using System.Runtime.Serialization;
|
||||
|
||||
namespace DotNetMatrix
|
||||
{
|
||||
[Serializable]
|
||||
public class SingularValueDecomposition : System.Runtime.Serialization.ISerializable
|
||||
{
|
||||
private decimal[][] U, V;
|
||||
private decimal[] s;
|
||||
private int m, n;
|
||||
|
||||
public SingularValueDecomposition(GeneralMatrix Arg)
|
||||
{
|
||||
decimal[][] A = Arg.ArrayCopy;
|
||||
m = Arg.RowDimension;
|
||||
n = Arg.ColumnDimension;
|
||||
int nu = System.Math.Min(m, n);
|
||||
s = new decimal[System.Math.Min(m + 1, n)];
|
||||
U = new decimal[m][];
|
||||
for (int i = 0; i < m; i++)
|
||||
{
|
||||
U[i] = new decimal[nu];
|
||||
}
|
||||
V = new decimal[n][];
|
||||
for (int i2 = 0; i2 < n; i2++)
|
||||
{
|
||||
V[i2] = new decimal[n];
|
||||
}
|
||||
decimal[] e = new decimal[n];
|
||||
decimal[] work = new decimal[m];
|
||||
bool wantu = true;
|
||||
bool wantv = true;
|
||||
|
||||
int nct = System.Math.Min(m - 1, n);
|
||||
int nrt = System.Math.Max(0, System.Math.Min(n - 2, m));
|
||||
for (int k = 0; k < System.Math.Max(nct, nrt); k++)
|
||||
{
|
||||
if (k < nct)
|
||||
{
|
||||
s[k] = 0;
|
||||
for (int i = k; i < m; i++)
|
||||
{
|
||||
s[k] = Maths.Hypot(s[k], A[i][k]);
|
||||
}
|
||||
if (s[k] != 0.0m)
|
||||
{
|
||||
if (A[k][k] < 0.0m)
|
||||
{
|
||||
s[k] = - s[k];
|
||||
}
|
||||
for (int i = k; i < m; i++)
|
||||
{
|
||||
A[i][k] /= s[k];
|
||||
}
|
||||
A[k][k] += 1.0m;
|
||||
}
|
||||
s[k] = - s[k];
|
||||
}
|
||||
for (int j = k + 1; j < n; j++)
|
||||
{
|
||||
if ((k < nct) & (s[k] != 0.0m))
|
||||
{
|
||||
decimal t = 0;
|
||||
for (int i = k; i < m; i++)
|
||||
{
|
||||
t += A[i][k] * A[i][j];
|
||||
}
|
||||
t = (- t) / A[k][k];
|
||||
for (int i = k; i < m; i++)
|
||||
{
|
||||
A[i][j] += t * A[i][k];
|
||||
}
|
||||
}
|
||||
e[j] = A[k][j];
|
||||
}
|
||||
if (wantu & (k < nct))
|
||||
{
|
||||
for (int i = k; i < m; i++)
|
||||
{
|
||||
U[i][k] = A[i][k];
|
||||
}
|
||||
}
|
||||
if (k < nrt)
|
||||
{
|
||||
e[k] = 0;
|
||||
for (int i = k + 1; i < n; i++)
|
||||
{
|
||||
e[k] = Maths.Hypot(e[k], e[i]);
|
||||
}
|
||||
if (e[k] != 0.0m)
|
||||
{
|
||||
if (e[k + 1] < 0.0m)
|
||||
{
|
||||
e[k] = - e[k];
|
||||
}
|
||||
for (int i = k + 1; i < n; i++)
|
||||
{
|
||||
e[i] /= e[k];
|
||||
}
|
||||
e[k + 1] += 1.0m;
|
||||
}
|
||||
e[k] = - e[k];
|
||||
if ((k + 1 < m) & (e[k] != 0.0m))
|
||||
{
|
||||
for (int i = k + 1; i < m; i++)
|
||||
{
|
||||
work[i] = 0.0m;
|
||||
}
|
||||
for (int j = k + 1; j < n; j++)
|
||||
{
|
||||
for (int i = k + 1; i < m; i++)
|
||||
{
|
||||
work[i] += e[j] * A[i][j];
|
||||
}
|
||||
}
|
||||
for (int j = k + 1; j < n; j++)
|
||||
{
|
||||
decimal t = (- e[j]) / e[k + 1];
|
||||
for (int i = k + 1; i < m; i++)
|
||||
{
|
||||
A[i][j] += t * work[i];
|
||||
}
|
||||
}
|
||||
}
|
||||
if (wantv)
|
||||
{
|
||||
for (int i = k + 1; i < n; i++)
|
||||
{
|
||||
V[i][k] = e[i];
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
int p = System.Math.Min(n, m + 1);
|
||||
if (nct < n)
|
||||
{
|
||||
s[nct] = A[nct][nct];
|
||||
}
|
||||
if (m < p)
|
||||
{
|
||||
s[p - 1] = 0.0m;
|
||||
}
|
||||
if (nrt + 1 < p)
|
||||
{
|
||||
e[nrt] = A[nrt][p - 1];
|
||||
}
|
||||
e[p - 1] = 0.0m;
|
||||
|
||||
if (wantu)
|
||||
{
|
||||
for (int j = nct; j < nu; j++)
|
||||
{
|
||||
for (int i = 0; i < m; i++)
|
||||
{
|
||||
U[i][j] = 0.0m;
|
||||
}
|
||||
U[j][j] = 1.0m;
|
||||
}
|
||||
for (int k = nct - 1; k >= 0; k--)
|
||||
{
|
||||
if (s[k] != 0.0m)
|
||||
{
|
||||
for (int j = k + 1; j < nu; j++)
|
||||
{
|
||||
decimal t = 0;
|
||||
for (int i = k; i < m; i++)
|
||||
{
|
||||
t += U[i][k] * U[i][j];
|
||||
}
|
||||
t = (- t) / U[k][k];
|
||||
for (int i = k; i < m; i++)
|
||||
{
|
||||
U[i][j] += t * U[i][k];
|
||||
}
|
||||
}
|
||||
for (int i = k; i < m; i++)
|
||||
{
|
||||
U[i][k] = - U[i][k];
|
||||
}
|
||||
U[k][k] = 1.0m + U[k][k];
|
||||
for (int i = 0; i < k - 1; i++)
|
||||
{
|
||||
U[i][k] = 0.0m;
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
for (int i = 0; i < m; i++)
|
||||
{
|
||||
U[i][k] = 0.0m;
|
||||
}
|
||||
U[k][k] = 1.0m;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (wantv)
|
||||
{
|
||||
for (int k = n - 1; k >= 0; k--)
|
||||
{
|
||||
if ((k < nrt) & (e[k] != 0.0m))
|
||||
{
|
||||
for (int j = k + 1; j < nu; j++)
|
||||
{
|
||||
decimal t = 0;
|
||||
for (int i = k + 1; i < n; i++)
|
||||
{
|
||||
t += V[i][k] * V[i][j];
|
||||
}
|
||||
t = (- t) / V[k + 1][k];
|
||||
for (int i = k + 1; i < n; i++)
|
||||
{
|
||||
V[i][j] += t * V[i][k];
|
||||
}
|
||||
}
|
||||
}
|
||||
for (int i = 0; i < n; i++)
|
||||
{
|
||||
V[i][k] = 0.0m;
|
||||
}
|
||||
V[k][k] = 1.0m;
|
||||
}
|
||||
}
|
||||
|
||||
int pp = p - 1;
|
||||
int iter = 0;
|
||||
decimal eps = (decimal)Math.Pow(2.0f, - 52.0f);
|
||||
while (p > 0)
|
||||
{
|
||||
int k, kase;
|
||||
for (k = p - 2; k >= - 1; k--)
|
||||
{
|
||||
if (k == - 1)
|
||||
{
|
||||
break;
|
||||
}
|
||||
if (System.Math.Abs(e[k]) <= eps * (System.Math.Abs(s[k]) + System.Math.Abs(s[k + 1])))
|
||||
{
|
||||
e[k] = 0.0m;
|
||||
break;
|
||||
}
|
||||
}
|
||||
if (k == p - 2)
|
||||
{
|
||||
kase = 4;
|
||||
}
|
||||
else
|
||||
{
|
||||
int ks;
|
||||
for (ks = p - 1; ks >= k; ks--)
|
||||
{
|
||||
if (ks == k)
|
||||
{
|
||||
break;
|
||||
}
|
||||
decimal t = (ks != p?System.Math.Abs(e[ks]):0.0m) + (ks != k + 1?System.Math.Abs(e[ks - 1]):0.0m);
|
||||
if (System.Math.Abs(s[ks]) <= eps * t)
|
||||
{
|
||||
s[ks] = 0.0m;
|
||||
break;
|
||||
}
|
||||
}
|
||||
if (ks == k)
|
||||
{
|
||||
kase = 3;
|
||||
}
|
||||
else if (ks == p - 1)
|
||||
{
|
||||
kase = 1;
|
||||
}
|
||||
else
|
||||
{
|
||||
kase = 2;
|
||||
k = ks;
|
||||
}
|
||||
}
|
||||
k++;
|
||||
|
||||
switch (kase)
|
||||
{
|
||||
case 1:
|
||||
{
|
||||
decimal f = e[p - 2];
|
||||
e[p - 2] = 0.0m;
|
||||
for (int j = p - 2; j >= k; j--)
|
||||
{
|
||||
decimal t = Maths.Hypot(s[j], f);
|
||||
decimal cs = s[j] / t;
|
||||
decimal sn = f / t;
|
||||
s[j] = t;
|
||||
if (j != k)
|
||||
{
|
||||
f = (- sn) * e[j - 1];
|
||||
e[j - 1] = cs * e[j - 1];
|
||||
}
|
||||
if (wantv)
|
||||
{
|
||||
for (int i = 0; i < n; i++)
|
||||
{
|
||||
t = cs * V[i][j] + sn * V[i][p - 1];
|
||||
V[i][p - 1] = (- sn) * V[i][j] + cs * V[i][p - 1];
|
||||
V[i][j] = t;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
break;
|
||||
case 2:
|
||||
{
|
||||
decimal f = e[k - 1];
|
||||
e[k - 1] = 0.0m;
|
||||
for (int j = k; j < p; j++)
|
||||
{
|
||||
decimal t = Maths.Hypot(s[j], f);
|
||||
decimal cs = s[j] / t;
|
||||
decimal sn = f / t;
|
||||
s[j] = t;
|
||||
f = (- sn) * e[j];
|
||||
e[j] = cs * e[j];
|
||||
if (wantu)
|
||||
{
|
||||
for (int i = 0; i < m; i++)
|
||||
{
|
||||
t = cs * U[i][j] + sn * U[i][k - 1];
|
||||
U[i][k - 1] = (- sn) * U[i][j] + cs * U[i][k - 1];
|
||||
U[i][j] = t;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
break;
|
||||
case 3:
|
||||
{
|
||||
decimal scale = System.Math.Max(System.Math.Max(System.Math.Max(System.Math.Max(System.Math.Abs(s[p - 1]), System.Math.Abs(s[p - 2])), System.Math.Abs(e[p - 2])), System.Math.Abs(s[k])), System.Math.Abs(e[k]));
|
||||
decimal sp = s[p - 1] / scale;
|
||||
decimal spm1 = s[p - 2] / scale;
|
||||
decimal epm1 = e[p - 2] / scale;
|
||||
decimal sk = s[k] / scale;
|
||||
decimal ek = e[k] / scale;
|
||||
decimal b = ((spm1 + sp) * (spm1 - sp) + epm1 * epm1) / 2.0m;
|
||||
decimal c = (sp * epm1) * (sp * epm1);
|
||||
decimal shift = 0.0m;
|
||||
if ((b != 0.0m) | (c != 0.0m))
|
||||
{
|
||||
shift = (decimal)DecimalMath.Sqrt(b * b + c);
|
||||
if (b < 0.0m)
|
||||
{
|
||||
shift = - shift;
|
||||
}
|
||||
shift = c / (b + shift);
|
||||
}
|
||||
decimal f = (sk + sp) * (sk - sp) + shift;
|
||||
decimal g = sk * ek;
|
||||
|
||||
for (int j = k; j < p - 1; j++)
|
||||
{
|
||||
decimal t = Maths.Hypot(f, g);
|
||||
decimal cs = f / t;
|
||||
decimal sn = g / t;
|
||||
if (j != k)
|
||||
{
|
||||
e[j - 1] = t;
|
||||
}
|
||||
f = cs * s[j] + sn * e[j];
|
||||
e[j] = cs * e[j] - sn * s[j];
|
||||
g = sn * s[j + 1];
|
||||
s[j + 1] = cs * s[j + 1];
|
||||
if (wantv)
|
||||
{
|
||||
for (int i = 0; i < n; i++)
|
||||
{
|
||||
t = cs * V[i][j] + sn * V[i][j + 1];
|
||||
V[i][j + 1] = (- sn) * V[i][j] + cs * V[i][j + 1];
|
||||
V[i][j] = t;
|
||||
}
|
||||
}
|
||||
t = Maths.Hypot(f, g);
|
||||
cs = f / t;
|
||||
sn = g / t;
|
||||
s[j] = t;
|
||||
f = cs * e[j] + sn * s[j + 1];
|
||||
s[j + 1] = (- sn) * e[j] + cs * s[j + 1];
|
||||
g = sn * e[j + 1];
|
||||
e[j + 1] = cs * e[j + 1];
|
||||
if (wantu && (j < m - 1))
|
||||
{
|
||||
for (int i = 0; i < m; i++)
|
||||
{
|
||||
t = cs * U[i][j] + sn * U[i][j + 1];
|
||||
U[i][j + 1] = (- sn) * U[i][j] + cs * U[i][j + 1];
|
||||
U[i][j] = t;
|
||||
}
|
||||
}
|
||||
}
|
||||
e[p - 2] = f;
|
||||
iter = iter + 1;
|
||||
}
|
||||
break;
|
||||
case 4:
|
||||
{
|
||||
if (s[k] <= 0.0m)
|
||||
{
|
||||
s[k] = (s[k] < 0.0m?- s[k]:0.0m);
|
||||
if (wantv)
|
||||
{
|
||||
for (int i = 0; i <= pp; i++)
|
||||
{
|
||||
V[i][k] = - V[i][k];
|
||||
}
|
||||
}
|
||||
}
|
||||
while (k < pp)
|
||||
{
|
||||
if (s[k] >= s[k + 1])
|
||||
{
|
||||
break;
|
||||
}
|
||||
decimal t = s[k];
|
||||
s[k] = s[k + 1];
|
||||
s[k + 1] = t;
|
||||
if (wantv && (k < n - 1))
|
||||
{
|
||||
for (int i = 0; i < n; i++)
|
||||
{
|
||||
t = V[i][k + 1]; V[i][k + 1] = V[i][k]; V[i][k] = t;
|
||||
}
|
||||
}
|
||||
if (wantu && (k < m - 1))
|
||||
{
|
||||
for (int i = 0; i < m; i++)
|
||||
{
|
||||
t = U[i][k + 1]; U[i][k + 1] = U[i][k]; U[i][k] = t;
|
||||
}
|
||||
}
|
||||
k++;
|
||||
}
|
||||
iter = 0;
|
||||
p--;
|
||||
}
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
virtual public decimal[] SingularValues
|
||||
{
|
||||
get
|
||||
{
|
||||
return s;
|
||||
}
|
||||
}
|
||||
|
||||
virtual public GeneralMatrix S
|
||||
{
|
||||
get
|
||||
{
|
||||
GeneralMatrix X = new GeneralMatrix(n, n);
|
||||
decimal[][] S = X.Array;
|
||||
for (int i = 0; i < n; i++)
|
||||
{
|
||||
for (int j = 0; j < n; j++)
|
||||
{
|
||||
S[i][j] = 0.0m;
|
||||
}
|
||||
S[i][i] = this.s[i];
|
||||
}
|
||||
return X;
|
||||
}
|
||||
}
|
||||
|
||||
public virtual GeneralMatrix GetU()
|
||||
{
|
||||
return new GeneralMatrix(U, m, System.Math.Min(m + 1, n));
|
||||
}
|
||||
|
||||
public virtual GeneralMatrix GetV()
|
||||
{
|
||||
return new GeneralMatrix(V, n, n);
|
||||
}
|
||||
|
||||
public virtual decimal Norm2()
|
||||
{
|
||||
return s[0];
|
||||
}
|
||||
|
||||
public virtual decimal Condition()
|
||||
{
|
||||
return s[0] / s[System.Math.Min(m, n) - 1];
|
||||
}
|
||||
|
||||
public virtual int Rank()
|
||||
{
|
||||
decimal eps = (decimal)System.Math.Pow(2.0f, - 52.0f);
|
||||
decimal tol = System.Math.Max(m, n) * s[0] * eps;
|
||||
int r = 0;
|
||||
for (int i = 0; i < s.Length; i++)
|
||||
{
|
||||
if (s[i] > tol)
|
||||
{
|
||||
r++;
|
||||
}
|
||||
}
|
||||
return r;
|
||||
}
|
||||
|
||||
void ISerializable.GetObjectData(SerializationInfo info, StreamingContext context)
|
||||
{
|
||||
}
|
||||
}
|
||||
}
|
||||
88
MathApp/MathApp/MathApp.Tests/build_err.txt
Normal file
88
MathApp/MathApp/MathApp.Tests/build_err.txt
Normal file
|
|
@ -0,0 +1,88 @@
|
|||
MSBuild version 17.3.4+a400405ba for .NET
|
||||
Geri y<>klenecek projeler belirleniyor...
|
||||
Geri y<>kleme i‡in t<>m projeler g<>ncel.
|
||||
C:\Users\tolga\Desktop\Math\MathApp\MathApp\MathApp.Tests\Function.cs(88,41): warning CS8605: Olas<61> bir null de§eri kutudan ‡<>karma. [C:\Users\tolga\Desktop\Math\MathApp\MathApp\MathApp.Tests\MathApp.Tests.csproj]
|
||||
C:\Users\tolga\Desktop\Math\MathApp\MathApp\MathApp.Tests\Function.cs(88,76): warning CS8605: Olas<61> bir null de§eri kutudan ‡<>karma. [C:\Users\tolga\Desktop\Math\MathApp\MathApp\MathApp.Tests\MathApp.Tests.csproj]
|
||||
C:\Users\tolga\Desktop\Math\MathApp\MathApp\MathApp.Tests\Function.cs(90,39): warning CS8605: Olas<61> bir null de§eri kutudan ‡<>karma. [C:\Users\tolga\Desktop\Math\MathApp\MathApp\MathApp.Tests\MathApp.Tests.csproj]
|
||||
C:\Users\tolga\Desktop\Math\MathApp\MathApp\MathApp.Tests\Function.cs(99,12): warning CS8618: Null atanamaz alan 'm_defaultFunctionEvaluation', oluŸturucudan ‡<>k<EFBFBD>Ÿ yaparken null olmayan bir de§er i‡ermelidir. alan alan<61>n<EFBFBD> null atanabilir olarak bildirmeyi d<>Ÿ<EFBFBD>n<EFBFBD>n. [C:\Users\tolga\Desktop\Math\MathApp\MathApp\MathApp.Tests\MathApp.Tests.csproj]
|
||||
C:\Users\tolga\Desktop\Math\MathApp\MathApp\MathApp.Tests\Function.cs(156,44): warning CS8605: Olas<61> bir null de§eri kutudan ‡<>karma. [C:\Users\tolga\Desktop\Math\MathApp\MathApp\MathApp.Tests\MathApp.Tests.csproj]
|
||||
C:\Users\tolga\Desktop\Math\MathApp\MathApp\MathApp.Tests\Function.cs(156,113): warning CS8605: Olas<61> bir null de§eri kutudan ‡<>karma. [C:\Users\tolga\Desktop\Math\MathApp\MathApp\MathApp.Tests\MathApp.Tests.csproj]
|
||||
C:\Users\tolga\Desktop\Math\MathApp\MathApp\MathApp.Tests\Function.cs(157,40): warning CS8605: Olas<61> bir null de§eri kutudan ‡<>karma. [C:\Users\tolga\Desktop\Math\MathApp\MathApp\MathApp.Tests\MathApp.Tests.csproj]
|
||||
C:\Users\tolga\Desktop\Math\MathApp\MathApp\MathApp.Tests\Function.cs(157,102): warning CS8605: Olas<61> bir null de§eri kutudan ‡<>karma. [C:\Users\tolga\Desktop\Math\MathApp\MathApp\MathApp.Tests\MathApp.Tests.csproj]
|
||||
C:\Users\tolga\Desktop\Math\MathApp\MathApp\MathApp.Tests\Function.cs(157,164): warning CS8605: Olas<61> bir null de§eri kutudan ‡<>karma. [C:\Users\tolga\Desktop\Math\MathApp\MathApp\MathApp.Tests\MathApp.Tests.csproj]
|
||||
C:\Users\tolga\Desktop\Math\MathApp\MathApp\MathApp.Tests\Function.cs(194,44): warning CS8605: Olas<61> bir null de§eri kutudan ‡<>karma. [C:\Users\tolga\Desktop\Math\MathApp\MathApp\MathApp.Tests\MathApp.Tests.csproj]
|
||||
C:\Users\tolga\Desktop\Math\MathApp\MathApp\MathApp.Tests\Function.cs(194,113): warning CS8605: Olas<61> bir null de§eri kutudan ‡<>karma. [C:\Users\tolga\Desktop\Math\MathApp\MathApp\MathApp.Tests\MathApp.Tests.csproj]
|
||||
C:\Users\tolga\Desktop\Math\MathApp\MathApp\MathApp.Tests\Function.cs(195,40): warning CS8605: Olas<61> bir null de§eri kutudan ‡<>karma. [C:\Users\tolga\Desktop\Math\MathApp\MathApp\MathApp.Tests\MathApp.Tests.csproj]
|
||||
C:\Users\tolga\Desktop\Math\MathApp\MathApp\MathApp.Tests\Function.cs(195,102): warning CS8605: Olas<61> bir null de§eri kutudan ‡<>karma. [C:\Users\tolga\Desktop\Math\MathApp\MathApp\MathApp.Tests\MathApp.Tests.csproj]
|
||||
C:\Users\tolga\Desktop\Math\MathApp\MathApp\MathApp.Tests\Function.cs(195,164): warning CS8605: Olas<61> bir null de§eri kutudan ‡<>karma. [C:\Users\tolga\Desktop\Math\MathApp\MathApp\MathApp.Tests\MathApp.Tests.csproj]
|
||||
C:\Users\tolga\Desktop\Math\MathApp\MathApp\MathApp.Tests\Function.cs(242,44): warning CS8605: Olas<61> bir null de§eri kutudan ‡<>karma. [C:\Users\tolga\Desktop\Math\MathApp\MathApp\MathApp.Tests\MathApp.Tests.csproj]
|
||||
C:\Users\tolga\Desktop\Math\MathApp\MathApp\MathApp.Tests\Function.cs(242,113): warning CS8605: Olas<61> bir null de§eri kutudan ‡<>karma. [C:\Users\tolga\Desktop\Math\MathApp\MathApp\MathApp.Tests\MathApp.Tests.csproj]
|
||||
C:\Users\tolga\Desktop\Math\MathApp\MathApp\MathApp.Tests\Function.cs(243,40): warning CS8605: Olas<61> bir null de§eri kutudan ‡<>karma. [C:\Users\tolga\Desktop\Math\MathApp\MathApp\MathApp.Tests\MathApp.Tests.csproj]
|
||||
C:\Users\tolga\Desktop\Math\MathApp\MathApp\MathApp.Tests\Function.cs(243,102): warning CS8605: Olas<61> bir null de§eri kutudan ‡<>karma. [C:\Users\tolga\Desktop\Math\MathApp\MathApp\MathApp.Tests\MathApp.Tests.csproj]
|
||||
C:\Users\tolga\Desktop\Math\MathApp\MathApp\MathApp.Tests\Function.cs(243,164): warning CS8605: Olas<61> bir null de§eri kutudan ‡<>karma. [C:\Users\tolga\Desktop\Math\MathApp\MathApp\MathApp.Tests\MathApp.Tests.csproj]
|
||||
C:\Users\tolga\Desktop\Math\MathApp\MathApp\MathApp.Tests\Matrix.cs(187,73): error CS0103: 'CultureInfo' ad<61> ge‡erli ba§lamda yok [C:\Users\tolga\Desktop\Math\MathApp\MathApp\MathApp.Tests\MathApp.Tests.csproj]
|
||||
C:\Users\tolga\Desktop\Math\MathApp\MathApp\MathApp.Tests\Matrix.cs(198,21): warning CS8603: Olas<61> null baŸvuru d”n<E2809D>Ÿ<EFBFBD>. [C:\Users\tolga\Desktop\Math\MathApp\MathApp\MathApp.Tests\MathApp.Tests.csproj]
|
||||
C:\Users\tolga\Desktop\Math\MathApp\MathApp\MathApp.Tests\Matrix.cs(228,20): warning CS8603: Olas<61> null baŸvuru d”n<E2809D>Ÿ<EFBFBD>. [C:\Users\tolga\Desktop\Math\MathApp\MathApp\MathApp.Tests\MathApp.Tests.csproj]
|
||||
C:\Users\tolga\Desktop\Math\MathApp\MathApp\MathApp.Tests\Function.cs(291,29): warning CS8605: Olas<61> bir null de§eri kutudan ‡<>karma. [C:\Users\tolga\Desktop\Math\MathApp\MathApp\MathApp.Tests\MathApp.Tests.csproj]
|
||||
C:\Users\tolga\Desktop\Math\MathApp\MathApp\MathApp.Tests\Function.cs(295,33): warning CS8605: Olas<61> bir null de§eri kutudan ‡<>karma. [C:\Users\tolga\Desktop\Math\MathApp\MathApp\MathApp.Tests\MathApp.Tests.csproj]
|
||||
C:\Users\tolga\Desktop\Math\MathApp\MathApp\MathApp.Tests\Function.cs(307,29): warning CS8605: Olas<61> bir null de§eri kutudan ‡<>karma. [C:\Users\tolga\Desktop\Math\MathApp\MathApp\MathApp.Tests\MathApp.Tests.csproj]
|
||||
C:\Users\tolga\Desktop\Math\MathApp\MathApp\MathApp.Tests\Function.cs(311,33): warning CS8605: Olas<61> bir null de§eri kutudan ‡<>karma. [C:\Users\tolga\Desktop\Math\MathApp\MathApp\MathApp.Tests\MathApp.Tests.csproj]
|
||||
C:\Users\tolga\Desktop\Math\MathApp\MathApp\MathApp.Tests\Function.cs(323,21): warning CS8605: Olas<61> bir null de§eri kutudan ‡<>karma. [C:\Users\tolga\Desktop\Math\MathApp\MathApp\MathApp.Tests\MathApp.Tests.csproj]
|
||||
C:\Users\tolga\Desktop\Math\MathApp\MathApp\MathApp.Tests\Function.cs(340,26): warning CS8605: Olas<61> bir null de§eri kutudan ‡<>karma. [C:\Users\tolga\Desktop\Math\MathApp\MathApp\MathApp.Tests\MathApp.Tests.csproj]
|
||||
C:\Users\tolga\Desktop\Math\MathApp\MathApp\MathApp.Tests\Function.cs(352,26): warning CS8605: Olas<61> bir null de§eri kutudan ‡<>karma. [C:\Users\tolga\Desktop\Math\MathApp\MathApp\MathApp.Tests\MathApp.Tests.csproj]
|
||||
C:\Users\tolga\Desktop\Math\MathApp\MathApp\MathApp.Tests\Function.cs(353,26): warning CS8605: Olas<61> bir null de§eri kutudan ‡<>karma. [C:\Users\tolga\Desktop\Math\MathApp\MathApp\MathApp.Tests\MathApp.Tests.csproj]
|
||||
C:\Users\tolga\Desktop\Math\MathApp\MathApp\MathApp.Tests\Function.cs(370,26): warning CS8605: Olas<61> bir null de§eri kutudan ‡<>karma. [C:\Users\tolga\Desktop\Math\MathApp\MathApp\MathApp.Tests\MathApp.Tests.csproj]
|
||||
C:\Users\tolga\Desktop\Math\MathApp\MathApp\MathApp.Tests\Function.cs(386,34): warning CS8605: Olas<61> bir null de§eri kutudan ‡<>karma. [C:\Users\tolga\Desktop\Math\MathApp\MathApp\MathApp.Tests\MathApp.Tests.csproj]
|
||||
C:\Users\tolga\Desktop\Math\MathApp\MathApp\MathApp.Tests\Function.cs(388,34): warning CS8605: Olas<61> bir null de§eri kutudan ‡<>karma. [C:\Users\tolga\Desktop\Math\MathApp\MathApp\MathApp.Tests\MathApp.Tests.csproj]
|
||||
C:\Users\tolga\Desktop\Math\MathApp\MathApp\MathApp.Tests\Function.cs(391,61): warning CS8620: 'object?[]' t<>r<EFBFBD>ndeki ba§<61>ms<6D>z de§iŸken, baŸvuru t<>rlerinin null atanabilirlik farkl<6B>l<EFBFBD>klar<61> nedeniyle 'Symbol Function.EvaluateFunction(string name, params object[] args)' i‡inde 'object[]' t<>r<EFBFBD>ndeki 'args' parametresi i‡in kullan<61>lamaz. [C:\Users\tolga\Desktop\Math\MathApp\MathApp\MathApp.Tests\MathApp.Tests.csproj]
|
||||
C:\Users\tolga\Desktop\Math\MathApp\MathApp\MathApp.Tests\Function.cs(416,24): warning CS8605: Olas<61> bir null de§eri kutudan ‡<>karma. [C:\Users\tolga\Desktop\Math\MathApp\MathApp\MathApp.Tests\MathApp.Tests.csproj]
|
||||
C:\Users\tolga\Desktop\Math\MathApp\MathApp\MathApp.Tests\NumericalMethods.cs(427,31): warning CS8603: Olas<61> null baŸvuru d”n<E2809D>Ÿ<EFBFBD>. [C:\Users\tolga\Desktop\Math\MathApp\MathApp\MathApp.Tests\MathApp.Tests.csproj]
|
||||
C:\Users\tolga\Desktop\Math\MathApp\MathApp\MathApp.Tests\NumericalMethods.cs(450,32): warning CS8603: Olas<61> null baŸvuru d”n<E2809D>Ÿ<EFBFBD>. [C:\Users\tolga\Desktop\Math\MathApp\MathApp\MathApp.Tests\MathApp.Tests.csproj]
|
||||
C:\Users\tolga\Desktop\Math\MathApp\MathApp\MathApp.Tests\EigenvalueDecomposition.cs(823,10): warning CS8618: Null atanamaz alan 'H', oluŸturucudan ‡<>k<EFBFBD>Ÿ yaparken null olmayan bir de§er i‡ermelidir. alan alan<61>n<EFBFBD> null atanabilir olarak bildirmeyi d<>Ÿ<EFBFBD>n<EFBFBD>n. [C:\Users\tolga\Desktop\Math\MathApp\MathApp\MathApp.Tests\MathApp.Tests.csproj]
|
||||
C:\Users\tolga\Desktop\Math\MathApp\MathApp\MathApp.Tests\EigenvalueDecomposition.cs(823,10): warning CS8618: Null atanamaz alan 'ort', oluŸturucudan ‡<>k<EFBFBD>Ÿ yaparken null olmayan bir de§er i‡ermelidir. alan alan<61>n<EFBFBD> null atanabilir olarak bildirmeyi d<>Ÿ<EFBFBD>n<EFBFBD>n. [C:\Users\tolga\Desktop\Math\MathApp\MathApp\MathApp.Tests\MathApp.Tests.csproj]
|
||||
|
||||
OluŸturma BAžARISIZ OLDU.
|
||||
|
||||
C:\Users\tolga\Desktop\Math\MathApp\MathApp\MathApp.Tests\Function.cs(88,41): warning CS8605: Olas<61> bir null de§eri kutudan ‡<>karma. [C:\Users\tolga\Desktop\Math\MathApp\MathApp\MathApp.Tests\MathApp.Tests.csproj]
|
||||
C:\Users\tolga\Desktop\Math\MathApp\MathApp\MathApp.Tests\Function.cs(88,76): warning CS8605: Olas<61> bir null de§eri kutudan ‡<>karma. [C:\Users\tolga\Desktop\Math\MathApp\MathApp\MathApp.Tests\MathApp.Tests.csproj]
|
||||
C:\Users\tolga\Desktop\Math\MathApp\MathApp\MathApp.Tests\Function.cs(90,39): warning CS8605: Olas<61> bir null de§eri kutudan ‡<>karma. [C:\Users\tolga\Desktop\Math\MathApp\MathApp\MathApp.Tests\MathApp.Tests.csproj]
|
||||
C:\Users\tolga\Desktop\Math\MathApp\MathApp\MathApp.Tests\Function.cs(99,12): warning CS8618: Null atanamaz alan 'm_defaultFunctionEvaluation', oluŸturucudan ‡<>k<EFBFBD>Ÿ yaparken null olmayan bir de§er i‡ermelidir. alan alan<61>n<EFBFBD> null atanabilir olarak bildirmeyi d<>Ÿ<EFBFBD>n<EFBFBD>n. [C:\Users\tolga\Desktop\Math\MathApp\MathApp\MathApp.Tests\MathApp.Tests.csproj]
|
||||
C:\Users\tolga\Desktop\Math\MathApp\MathApp\MathApp.Tests\Function.cs(156,44): warning CS8605: Olas<61> bir null de§eri kutudan ‡<>karma. [C:\Users\tolga\Desktop\Math\MathApp\MathApp\MathApp.Tests\MathApp.Tests.csproj]
|
||||
C:\Users\tolga\Desktop\Math\MathApp\MathApp\MathApp.Tests\Function.cs(156,113): warning CS8605: Olas<61> bir null de§eri kutudan ‡<>karma. [C:\Users\tolga\Desktop\Math\MathApp\MathApp\MathApp.Tests\MathApp.Tests.csproj]
|
||||
C:\Users\tolga\Desktop\Math\MathApp\MathApp\MathApp.Tests\Function.cs(157,40): warning CS8605: Olas<61> bir null de§eri kutudan ‡<>karma. [C:\Users\tolga\Desktop\Math\MathApp\MathApp\MathApp.Tests\MathApp.Tests.csproj]
|
||||
C:\Users\tolga\Desktop\Math\MathApp\MathApp\MathApp.Tests\Function.cs(157,102): warning CS8605: Olas<61> bir null de§eri kutudan ‡<>karma. [C:\Users\tolga\Desktop\Math\MathApp\MathApp\MathApp.Tests\MathApp.Tests.csproj]
|
||||
C:\Users\tolga\Desktop\Math\MathApp\MathApp\MathApp.Tests\Function.cs(157,164): warning CS8605: Olas<61> bir null de§eri kutudan ‡<>karma. [C:\Users\tolga\Desktop\Math\MathApp\MathApp\MathApp.Tests\MathApp.Tests.csproj]
|
||||
C:\Users\tolga\Desktop\Math\MathApp\MathApp\MathApp.Tests\Function.cs(194,44): warning CS8605: Olas<61> bir null de§eri kutudan ‡<>karma. [C:\Users\tolga\Desktop\Math\MathApp\MathApp\MathApp.Tests\MathApp.Tests.csproj]
|
||||
C:\Users\tolga\Desktop\Math\MathApp\MathApp\MathApp.Tests\Function.cs(194,113): warning CS8605: Olas<61> bir null de§eri kutudan ‡<>karma. [C:\Users\tolga\Desktop\Math\MathApp\MathApp\MathApp.Tests\MathApp.Tests.csproj]
|
||||
C:\Users\tolga\Desktop\Math\MathApp\MathApp\MathApp.Tests\Function.cs(195,40): warning CS8605: Olas<61> bir null de§eri kutudan ‡<>karma. [C:\Users\tolga\Desktop\Math\MathApp\MathApp\MathApp.Tests\MathApp.Tests.csproj]
|
||||
C:\Users\tolga\Desktop\Math\MathApp\MathApp\MathApp.Tests\Function.cs(195,102): warning CS8605: Olas<61> bir null de§eri kutudan ‡<>karma. [C:\Users\tolga\Desktop\Math\MathApp\MathApp\MathApp.Tests\MathApp.Tests.csproj]
|
||||
C:\Users\tolga\Desktop\Math\MathApp\MathApp\MathApp.Tests\Function.cs(195,164): warning CS8605: Olas<61> bir null de§eri kutudan ‡<>karma. [C:\Users\tolga\Desktop\Math\MathApp\MathApp\MathApp.Tests\MathApp.Tests.csproj]
|
||||
C:\Users\tolga\Desktop\Math\MathApp\MathApp\MathApp.Tests\Function.cs(242,44): warning CS8605: Olas<61> bir null de§eri kutudan ‡<>karma. [C:\Users\tolga\Desktop\Math\MathApp\MathApp\MathApp.Tests\MathApp.Tests.csproj]
|
||||
C:\Users\tolga\Desktop\Math\MathApp\MathApp\MathApp.Tests\Function.cs(242,113): warning CS8605: Olas<61> bir null de§eri kutudan ‡<>karma. [C:\Users\tolga\Desktop\Math\MathApp\MathApp\MathApp.Tests\MathApp.Tests.csproj]
|
||||
C:\Users\tolga\Desktop\Math\MathApp\MathApp\MathApp.Tests\Function.cs(243,40): warning CS8605: Olas<61> bir null de§eri kutudan ‡<>karma. [C:\Users\tolga\Desktop\Math\MathApp\MathApp\MathApp.Tests\MathApp.Tests.csproj]
|
||||
C:\Users\tolga\Desktop\Math\MathApp\MathApp\MathApp.Tests\Function.cs(243,102): warning CS8605: Olas<61> bir null de§eri kutudan ‡<>karma. [C:\Users\tolga\Desktop\Math\MathApp\MathApp\MathApp.Tests\MathApp.Tests.csproj]
|
||||
C:\Users\tolga\Desktop\Math\MathApp\MathApp\MathApp.Tests\Function.cs(243,164): warning CS8605: Olas<61> bir null de§eri kutudan ‡<>karma. [C:\Users\tolga\Desktop\Math\MathApp\MathApp\MathApp.Tests\MathApp.Tests.csproj]
|
||||
C:\Users\tolga\Desktop\Math\MathApp\MathApp\MathApp.Tests\Matrix.cs(198,21): warning CS8603: Olas<61> null baŸvuru d”n<E2809D>Ÿ<EFBFBD>. [C:\Users\tolga\Desktop\Math\MathApp\MathApp\MathApp.Tests\MathApp.Tests.csproj]
|
||||
C:\Users\tolga\Desktop\Math\MathApp\MathApp\MathApp.Tests\Matrix.cs(228,20): warning CS8603: Olas<61> null baŸvuru d”n<E2809D>Ÿ<EFBFBD>. [C:\Users\tolga\Desktop\Math\MathApp\MathApp\MathApp.Tests\MathApp.Tests.csproj]
|
||||
C:\Users\tolga\Desktop\Math\MathApp\MathApp\MathApp.Tests\Function.cs(291,29): warning CS8605: Olas<61> bir null de§eri kutudan ‡<>karma. [C:\Users\tolga\Desktop\Math\MathApp\MathApp\MathApp.Tests\MathApp.Tests.csproj]
|
||||
C:\Users\tolga\Desktop\Math\MathApp\MathApp\MathApp.Tests\Function.cs(295,33): warning CS8605: Olas<61> bir null de§eri kutudan ‡<>karma. [C:\Users\tolga\Desktop\Math\MathApp\MathApp\MathApp.Tests\MathApp.Tests.csproj]
|
||||
C:\Users\tolga\Desktop\Math\MathApp\MathApp\MathApp.Tests\Function.cs(307,29): warning CS8605: Olas<61> bir null de§eri kutudan ‡<>karma. [C:\Users\tolga\Desktop\Math\MathApp\MathApp\MathApp.Tests\MathApp.Tests.csproj]
|
||||
C:\Users\tolga\Desktop\Math\MathApp\MathApp\MathApp.Tests\Function.cs(311,33): warning CS8605: Olas<61> bir null de§eri kutudan ‡<>karma. [C:\Users\tolga\Desktop\Math\MathApp\MathApp\MathApp.Tests\MathApp.Tests.csproj]
|
||||
C:\Users\tolga\Desktop\Math\MathApp\MathApp\MathApp.Tests\Function.cs(323,21): warning CS8605: Olas<61> bir null de§eri kutudan ‡<>karma. [C:\Users\tolga\Desktop\Math\MathApp\MathApp\MathApp.Tests\MathApp.Tests.csproj]
|
||||
C:\Users\tolga\Desktop\Math\MathApp\MathApp\MathApp.Tests\Function.cs(340,26): warning CS8605: Olas<61> bir null de§eri kutudan ‡<>karma. [C:\Users\tolga\Desktop\Math\MathApp\MathApp\MathApp.Tests\MathApp.Tests.csproj]
|
||||
C:\Users\tolga\Desktop\Math\MathApp\MathApp\MathApp.Tests\Function.cs(352,26): warning CS8605: Olas<61> bir null de§eri kutudan ‡<>karma. [C:\Users\tolga\Desktop\Math\MathApp\MathApp\MathApp.Tests\MathApp.Tests.csproj]
|
||||
C:\Users\tolga\Desktop\Math\MathApp\MathApp\MathApp.Tests\Function.cs(353,26): warning CS8605: Olas<61> bir null de§eri kutudan ‡<>karma. [C:\Users\tolga\Desktop\Math\MathApp\MathApp\MathApp.Tests\MathApp.Tests.csproj]
|
||||
C:\Users\tolga\Desktop\Math\MathApp\MathApp\MathApp.Tests\Function.cs(370,26): warning CS8605: Olas<61> bir null de§eri kutudan ‡<>karma. [C:\Users\tolga\Desktop\Math\MathApp\MathApp\MathApp.Tests\MathApp.Tests.csproj]
|
||||
C:\Users\tolga\Desktop\Math\MathApp\MathApp\MathApp.Tests\Function.cs(386,34): warning CS8605: Olas<61> bir null de§eri kutudan ‡<>karma. [C:\Users\tolga\Desktop\Math\MathApp\MathApp\MathApp.Tests\MathApp.Tests.csproj]
|
||||
C:\Users\tolga\Desktop\Math\MathApp\MathApp\MathApp.Tests\Function.cs(388,34): warning CS8605: Olas<61> bir null de§eri kutudan ‡<>karma. [C:\Users\tolga\Desktop\Math\MathApp\MathApp\MathApp.Tests\MathApp.Tests.csproj]
|
||||
C:\Users\tolga\Desktop\Math\MathApp\MathApp\MathApp.Tests\Function.cs(391,61): warning CS8620: 'object?[]' t<>r<EFBFBD>ndeki ba§<61>ms<6D>z de§iŸken, baŸvuru t<>rlerinin null atanabilirlik farkl<6B>l<EFBFBD>klar<61> nedeniyle 'Symbol Function.EvaluateFunction(string name, params object[] args)' i‡inde 'object[]' t<>r<EFBFBD>ndeki 'args' parametresi i‡in kullan<61>lamaz. [C:\Users\tolga\Desktop\Math\MathApp\MathApp\MathApp.Tests\MathApp.Tests.csproj]
|
||||
C:\Users\tolga\Desktop\Math\MathApp\MathApp\MathApp.Tests\Function.cs(416,24): warning CS8605: Olas<61> bir null de§eri kutudan ‡<>karma. [C:\Users\tolga\Desktop\Math\MathApp\MathApp\MathApp.Tests\MathApp.Tests.csproj]
|
||||
C:\Users\tolga\Desktop\Math\MathApp\MathApp\MathApp.Tests\NumericalMethods.cs(427,31): warning CS8603: Olas<61> null baŸvuru d”n<E2809D>Ÿ<EFBFBD>. [C:\Users\tolga\Desktop\Math\MathApp\MathApp\MathApp.Tests\MathApp.Tests.csproj]
|
||||
C:\Users\tolga\Desktop\Math\MathApp\MathApp\MathApp.Tests\NumericalMethods.cs(450,32): warning CS8603: Olas<61> null baŸvuru d”n<E2809D>Ÿ<EFBFBD>. [C:\Users\tolga\Desktop\Math\MathApp\MathApp\MathApp.Tests\MathApp.Tests.csproj]
|
||||
C:\Users\tolga\Desktop\Math\MathApp\MathApp\MathApp.Tests\EigenvalueDecomposition.cs(823,10): warning CS8618: Null atanamaz alan 'H', oluŸturucudan ‡<>k<EFBFBD>Ÿ yaparken null olmayan bir de§er i‡ermelidir. alan alan<61>n<EFBFBD> null atanabilir olarak bildirmeyi d<>Ÿ<EFBFBD>n<EFBFBD>n. [C:\Users\tolga\Desktop\Math\MathApp\MathApp\MathApp.Tests\MathApp.Tests.csproj]
|
||||
C:\Users\tolga\Desktop\Math\MathApp\MathApp\MathApp.Tests\EigenvalueDecomposition.cs(823,10): warning CS8618: Null atanamaz alan 'ort', oluŸturucudan ‡<>k<EFBFBD>Ÿ yaparken null olmayan bir de§er i‡ermelidir. alan alan<61>n<EFBFBD> null atanabilir olarak bildirmeyi d<>Ÿ<EFBFBD>n<EFBFBD>n. [C:\Users\tolga\Desktop\Math\MathApp\MathApp\MathApp.Tests\MathApp.Tests.csproj]
|
||||
C:\Users\tolga\Desktop\Math\MathApp\MathApp\MathApp.Tests\Matrix.cs(187,73): error CS0103: 'CultureInfo' ad<61> ge‡erli ba§lamda yok [C:\Users\tolga\Desktop\Math\MathApp\MathApp\MathApp.Tests\MathApp.Tests.csproj]
|
||||
38 Uyar<61>
|
||||
1 Hata
|
||||
|
||||
Ge‡en S<>re 00:00:02.21
|
||||
BIN
MathApp/MathApp/MathApp.Tests/out_ascii.txt
Normal file
BIN
MathApp/MathApp/MathApp.Tests/out_ascii.txt
Normal file
Binary file not shown.
BIN
MathApp/MathApp/MathApp.Tests/test_results.txt
Normal file
BIN
MathApp/MathApp/MathApp.Tests/test_results.txt
Normal file
Binary file not shown.
18
MathApp/MathApp/Program.cs
Normal file
18
MathApp/MathApp/Program.cs
Normal file
|
|
@ -0,0 +1,18 @@
|
|||
using System;
|
||||
using System.Windows.Forms;
|
||||
|
||||
namespace MathApp
|
||||
{
|
||||
internal static class Program
|
||||
{
|
||||
/// <summary>
|
||||
/// The main entry point for the application.
|
||||
/// </summary>
|
||||
[STAThread]
|
||||
static void Main()
|
||||
{
|
||||
ApplicationConfiguration.Initialize();
|
||||
Application.Run(new MainForm());
|
||||
}
|
||||
}
|
||||
}
|
||||
69
MathApp/MathApp/ThemeHelper.cs
Normal file
69
MathApp/MathApp/ThemeHelper.cs
Normal file
|
|
@ -0,0 +1,69 @@
|
|||
using System;
|
||||
using System.Drawing;
|
||||
using System.Windows.Forms;
|
||||
|
||||
namespace MathApp
|
||||
{
|
||||
public static class ThemeHelper
|
||||
{
|
||||
// Color Palette
|
||||
public static Color DarkBackground = Color.FromArgb(32, 32, 32);
|
||||
public static Color DarkPanel = Color.FromArgb(45, 45, 48);
|
||||
public static Color LightText = Color.FromArgb(241, 241, 241);
|
||||
public static Color AccentBlue = Color.FromArgb(0, 122, 204);
|
||||
public static Color AccentDarkBlue = Color.FromArgb(14, 99, 156);
|
||||
public static Color BorderColor = Color.FromArgb(63, 63, 70);
|
||||
|
||||
public static void ApplyTheme(Form form)
|
||||
{
|
||||
form.BackColor = DarkBackground;
|
||||
form.ForeColor = LightText;
|
||||
|
||||
ApplyRecursively(form.Controls);
|
||||
}
|
||||
|
||||
public static void ApplyRecursively(Control.ControlCollection controls)
|
||||
{
|
||||
foreach (Control c in controls)
|
||||
{
|
||||
if (c is Button btn)
|
||||
{
|
||||
btn.FlatStyle = FlatStyle.Flat;
|
||||
btn.FlatAppearance.BorderSize = 0;
|
||||
btn.BackColor = DarkPanel;
|
||||
btn.ForeColor = LightText;
|
||||
|
||||
// Simple hover effect hook if not already handled
|
||||
// But we don't want to override custom event handlers easily.
|
||||
// Just set initial properties.
|
||||
}
|
||||
else if (c is TextBox txt)
|
||||
{
|
||||
txt.BackColor = Color.FromArgb(30, 30, 30);
|
||||
txt.ForeColor = LightText;
|
||||
txt.BorderStyle = BorderStyle.FixedSingle;
|
||||
}
|
||||
else if (c is Panel pnl)
|
||||
{
|
||||
// pnl.BackColor = DarkBackground; // Don't override panel colors if they are structural
|
||||
// Apply to children
|
||||
ApplyRecursively(pnl.Controls);
|
||||
}
|
||||
else if (c is Label lbl)
|
||||
{
|
||||
lbl.ForeColor = LightText;
|
||||
}
|
||||
else if (c is GroupBox grp)
|
||||
{
|
||||
grp.ForeColor = LightText;
|
||||
ApplyRecursively(grp.Controls);
|
||||
}
|
||||
|
||||
if (c.HasChildren && !(c is Panel) && !(c is GroupBox))
|
||||
{
|
||||
ApplyRecursively(c.Controls);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
BIN
MathApp/MathApp/build_err_main.txt
Normal file
BIN
MathApp/MathApp/build_err_main.txt
Normal file
Binary file not shown.
BIN
MathApp/MathApp/build_log.txt
Normal file
BIN
MathApp/MathApp/build_log.txt
Normal file
Binary file not shown.
BIN
MathApp/MathApp/logo.ico
Normal file
BIN
MathApp/MathApp/logo.ico
Normal file
Binary file not shown.
|
After Width: | Height: | Size: 40 KiB |
43
Packages/manifest.json
Normal file
43
Packages/manifest.json
Normal file
|
|
@ -0,0 +1,43 @@
|
|||
{
|
||||
"dependencies": {
|
||||
"com.unity.collab-proxy": "1.14.18",
|
||||
"com.unity.ide.rider": "1.2.1",
|
||||
"com.unity.ide.visualstudio": "2.0.15",
|
||||
"com.unity.ide.vscode": "1.2.5",
|
||||
"com.unity.test-framework": "1.1.31",
|
||||
"com.unity.textmeshpro": "2.1.4",
|
||||
"com.unity.timeline": "1.2.18",
|
||||
"com.unity.ugui": "1.0.0",
|
||||
"com.unity.modules.ai": "1.0.0",
|
||||
"com.unity.modules.androidjni": "1.0.0",
|
||||
"com.unity.modules.animation": "1.0.0",
|
||||
"com.unity.modules.assetbundle": "1.0.0",
|
||||
"com.unity.modules.audio": "1.0.0",
|
||||
"com.unity.modules.cloth": "1.0.0",
|
||||
"com.unity.modules.director": "1.0.0",
|
||||
"com.unity.modules.imageconversion": "1.0.0",
|
||||
"com.unity.modules.imgui": "1.0.0",
|
||||
"com.unity.modules.jsonserialize": "1.0.0",
|
||||
"com.unity.modules.particlesystem": "1.0.0",
|
||||
"com.unity.modules.physics": "1.0.0",
|
||||
"com.unity.modules.physics2d": "1.0.0",
|
||||
"com.unity.modules.screencapture": "1.0.0",
|
||||
"com.unity.modules.terrain": "1.0.0",
|
||||
"com.unity.modules.terrainphysics": "1.0.0",
|
||||
"com.unity.modules.tilemap": "1.0.0",
|
||||
"com.unity.modules.ui": "1.0.0",
|
||||
"com.unity.modules.uielements": "1.0.0",
|
||||
"com.unity.modules.umbra": "1.0.0",
|
||||
"com.unity.modules.unityanalytics": "1.0.0",
|
||||
"com.unity.modules.unitywebrequest": "1.0.0",
|
||||
"com.unity.modules.unitywebrequestassetbundle": "1.0.0",
|
||||
"com.unity.modules.unitywebrequestaudio": "1.0.0",
|
||||
"com.unity.modules.unitywebrequesttexture": "1.0.0",
|
||||
"com.unity.modules.unitywebrequestwww": "1.0.0",
|
||||
"com.unity.modules.vehicles": "1.0.0",
|
||||
"com.unity.modules.video": "1.0.0",
|
||||
"com.unity.modules.vr": "1.0.0",
|
||||
"com.unity.modules.wind": "1.0.0",
|
||||
"com.unity.modules.xr": "1.0.0"
|
||||
}
|
||||
}
|
||||
326
Packages/packages-lock.json
Normal file
326
Packages/packages-lock.json
Normal file
|
|
@ -0,0 +1,326 @@
|
|||
{
|
||||
"dependencies": {
|
||||
"com.unity.collab-proxy": {
|
||||
"version": "1.14.18",
|
||||
"depth": 0,
|
||||
"source": "registry",
|
||||
"dependencies": {},
|
||||
"url": "https://packages.unity.com"
|
||||
},
|
||||
"com.unity.ext.nunit": {
|
||||
"version": "1.0.6",
|
||||
"depth": 1,
|
||||
"source": "registry",
|
||||
"dependencies": {},
|
||||
"url": "https://packages.unity.com"
|
||||
},
|
||||
"com.unity.ide.rider": {
|
||||
"version": "1.2.1",
|
||||
"depth": 0,
|
||||
"source": "registry",
|
||||
"dependencies": {
|
||||
"com.unity.test-framework": "1.1.1"
|
||||
},
|
||||
"url": "https://packages.unity.com"
|
||||
},
|
||||
"com.unity.ide.visualstudio": {
|
||||
"version": "2.0.15",
|
||||
"depth": 0,
|
||||
"source": "registry",
|
||||
"dependencies": {
|
||||
"com.unity.test-framework": "1.1.9"
|
||||
},
|
||||
"url": "https://packages.unity.com"
|
||||
},
|
||||
"com.unity.ide.vscode": {
|
||||
"version": "1.2.5",
|
||||
"depth": 0,
|
||||
"source": "registry",
|
||||
"dependencies": {},
|
||||
"url": "https://packages.unity.com"
|
||||
},
|
||||
"com.unity.test-framework": {
|
||||
"version": "1.1.31",
|
||||
"depth": 0,
|
||||
"source": "registry",
|
||||
"dependencies": {
|
||||
"com.unity.ext.nunit": "1.0.6",
|
||||
"com.unity.modules.imgui": "1.0.0",
|
||||
"com.unity.modules.jsonserialize": "1.0.0"
|
||||
},
|
||||
"url": "https://packages.unity.com"
|
||||
},
|
||||
"com.unity.textmeshpro": {
|
||||
"version": "2.1.4",
|
||||
"depth": 0,
|
||||
"source": "registry",
|
||||
"dependencies": {
|
||||
"com.unity.ugui": "1.0.0"
|
||||
},
|
||||
"url": "https://packages.unity.com"
|
||||
},
|
||||
"com.unity.timeline": {
|
||||
"version": "1.2.18",
|
||||
"depth": 0,
|
||||
"source": "registry",
|
||||
"dependencies": {
|
||||
"com.unity.modules.audio": "1.0.0",
|
||||
"com.unity.modules.director": "1.0.0",
|
||||
"com.unity.modules.animation": "1.0.0",
|
||||
"com.unity.modules.particlesystem": "1.0.0"
|
||||
},
|
||||
"url": "https://packages.unity.com"
|
||||
},
|
||||
"com.unity.ugui": {
|
||||
"version": "1.0.0",
|
||||
"depth": 0,
|
||||
"source": "builtin",
|
||||
"dependencies": {
|
||||
"com.unity.modules.ui": "1.0.0",
|
||||
"com.unity.modules.imgui": "1.0.0"
|
||||
}
|
||||
},
|
||||
"com.unity.modules.ai": {
|
||||
"version": "1.0.0",
|
||||
"depth": 0,
|
||||
"source": "builtin",
|
||||
"dependencies": {}
|
||||
},
|
||||
"com.unity.modules.androidjni": {
|
||||
"version": "1.0.0",
|
||||
"depth": 0,
|
||||
"source": "builtin",
|
||||
"dependencies": {}
|
||||
},
|
||||
"com.unity.modules.animation": {
|
||||
"version": "1.0.0",
|
||||
"depth": 0,
|
||||
"source": "builtin",
|
||||
"dependencies": {}
|
||||
},
|
||||
"com.unity.modules.assetbundle": {
|
||||
"version": "1.0.0",
|
||||
"depth": 0,
|
||||
"source": "builtin",
|
||||
"dependencies": {}
|
||||
},
|
||||
"com.unity.modules.audio": {
|
||||
"version": "1.0.0",
|
||||
"depth": 0,
|
||||
"source": "builtin",
|
||||
"dependencies": {}
|
||||
},
|
||||
"com.unity.modules.cloth": {
|
||||
"version": "1.0.0",
|
||||
"depth": 0,
|
||||
"source": "builtin",
|
||||
"dependencies": {
|
||||
"com.unity.modules.physics": "1.0.0"
|
||||
}
|
||||
},
|
||||
"com.unity.modules.director": {
|
||||
"version": "1.0.0",
|
||||
"depth": 0,
|
||||
"source": "builtin",
|
||||
"dependencies": {
|
||||
"com.unity.modules.audio": "1.0.0",
|
||||
"com.unity.modules.animation": "1.0.0"
|
||||
}
|
||||
},
|
||||
"com.unity.modules.imageconversion": {
|
||||
"version": "1.0.0",
|
||||
"depth": 0,
|
||||
"source": "builtin",
|
||||
"dependencies": {}
|
||||
},
|
||||
"com.unity.modules.imgui": {
|
||||
"version": "1.0.0",
|
||||
"depth": 0,
|
||||
"source": "builtin",
|
||||
"dependencies": {}
|
||||
},
|
||||
"com.unity.modules.jsonserialize": {
|
||||
"version": "1.0.0",
|
||||
"depth": 0,
|
||||
"source": "builtin",
|
||||
"dependencies": {}
|
||||
},
|
||||
"com.unity.modules.particlesystem": {
|
||||
"version": "1.0.0",
|
||||
"depth": 0,
|
||||
"source": "builtin",
|
||||
"dependencies": {}
|
||||
},
|
||||
"com.unity.modules.physics": {
|
||||
"version": "1.0.0",
|
||||
"depth": 0,
|
||||
"source": "builtin",
|
||||
"dependencies": {}
|
||||
},
|
||||
"com.unity.modules.physics2d": {
|
||||
"version": "1.0.0",
|
||||
"depth": 0,
|
||||
"source": "builtin",
|
||||
"dependencies": {}
|
||||
},
|
||||
"com.unity.modules.screencapture": {
|
||||
"version": "1.0.0",
|
||||
"depth": 0,
|
||||
"source": "builtin",
|
||||
"dependencies": {
|
||||
"com.unity.modules.imageconversion": "1.0.0"
|
||||
}
|
||||
},
|
||||
"com.unity.modules.subsystems": {
|
||||
"version": "1.0.0",
|
||||
"depth": 1,
|
||||
"source": "builtin",
|
||||
"dependencies": {
|
||||
"com.unity.modules.jsonserialize": "1.0.0"
|
||||
}
|
||||
},
|
||||
"com.unity.modules.terrain": {
|
||||
"version": "1.0.0",
|
||||
"depth": 0,
|
||||
"source": "builtin",
|
||||
"dependencies": {}
|
||||
},
|
||||
"com.unity.modules.terrainphysics": {
|
||||
"version": "1.0.0",
|
||||
"depth": 0,
|
||||
"source": "builtin",
|
||||
"dependencies": {
|
||||
"com.unity.modules.physics": "1.0.0",
|
||||
"com.unity.modules.terrain": "1.0.0"
|
||||
}
|
||||
},
|
||||
"com.unity.modules.tilemap": {
|
||||
"version": "1.0.0",
|
||||
"depth": 0,
|
||||
"source": "builtin",
|
||||
"dependencies": {
|
||||
"com.unity.modules.physics2d": "1.0.0"
|
||||
}
|
||||
},
|
||||
"com.unity.modules.ui": {
|
||||
"version": "1.0.0",
|
||||
"depth": 0,
|
||||
"source": "builtin",
|
||||
"dependencies": {}
|
||||
},
|
||||
"com.unity.modules.uielements": {
|
||||
"version": "1.0.0",
|
||||
"depth": 0,
|
||||
"source": "builtin",
|
||||
"dependencies": {
|
||||
"com.unity.modules.imgui": "1.0.0",
|
||||
"com.unity.modules.jsonserialize": "1.0.0"
|
||||
}
|
||||
},
|
||||
"com.unity.modules.umbra": {
|
||||
"version": "1.0.0",
|
||||
"depth": 0,
|
||||
"source": "builtin",
|
||||
"dependencies": {}
|
||||
},
|
||||
"com.unity.modules.unityanalytics": {
|
||||
"version": "1.0.0",
|
||||
"depth": 0,
|
||||
"source": "builtin",
|
||||
"dependencies": {
|
||||
"com.unity.modules.unitywebrequest": "1.0.0",
|
||||
"com.unity.modules.jsonserialize": "1.0.0"
|
||||
}
|
||||
},
|
||||
"com.unity.modules.unitywebrequest": {
|
||||
"version": "1.0.0",
|
||||
"depth": 0,
|
||||
"source": "builtin",
|
||||
"dependencies": {}
|
||||
},
|
||||
"com.unity.modules.unitywebrequestassetbundle": {
|
||||
"version": "1.0.0",
|
||||
"depth": 0,
|
||||
"source": "builtin",
|
||||
"dependencies": {
|
||||
"com.unity.modules.assetbundle": "1.0.0",
|
||||
"com.unity.modules.unitywebrequest": "1.0.0"
|
||||
}
|
||||
},
|
||||
"com.unity.modules.unitywebrequestaudio": {
|
||||
"version": "1.0.0",
|
||||
"depth": 0,
|
||||
"source": "builtin",
|
||||
"dependencies": {
|
||||
"com.unity.modules.unitywebrequest": "1.0.0",
|
||||
"com.unity.modules.audio": "1.0.0"
|
||||
}
|
||||
},
|
||||
"com.unity.modules.unitywebrequesttexture": {
|
||||
"version": "1.0.0",
|
||||
"depth": 0,
|
||||
"source": "builtin",
|
||||
"dependencies": {
|
||||
"com.unity.modules.unitywebrequest": "1.0.0",
|
||||
"com.unity.modules.imageconversion": "1.0.0"
|
||||
}
|
||||
},
|
||||
"com.unity.modules.unitywebrequestwww": {
|
||||
"version": "1.0.0",
|
||||
"depth": 0,
|
||||
"source": "builtin",
|
||||
"dependencies": {
|
||||
"com.unity.modules.unitywebrequest": "1.0.0",
|
||||
"com.unity.modules.unitywebrequestassetbundle": "1.0.0",
|
||||
"com.unity.modules.unitywebrequestaudio": "1.0.0",
|
||||
"com.unity.modules.audio": "1.0.0",
|
||||
"com.unity.modules.assetbundle": "1.0.0",
|
||||
"com.unity.modules.imageconversion": "1.0.0"
|
||||
}
|
||||
},
|
||||
"com.unity.modules.vehicles": {
|
||||
"version": "1.0.0",
|
||||
"depth": 0,
|
||||
"source": "builtin",
|
||||
"dependencies": {
|
||||
"com.unity.modules.physics": "1.0.0"
|
||||
}
|
||||
},
|
||||
"com.unity.modules.video": {
|
||||
"version": "1.0.0",
|
||||
"depth": 0,
|
||||
"source": "builtin",
|
||||
"dependencies": {
|
||||
"com.unity.modules.audio": "1.0.0",
|
||||
"com.unity.modules.ui": "1.0.0",
|
||||
"com.unity.modules.unitywebrequest": "1.0.0"
|
||||
}
|
||||
},
|
||||
"com.unity.modules.vr": {
|
||||
"version": "1.0.0",
|
||||
"depth": 0,
|
||||
"source": "builtin",
|
||||
"dependencies": {
|
||||
"com.unity.modules.jsonserialize": "1.0.0",
|
||||
"com.unity.modules.physics": "1.0.0",
|
||||
"com.unity.modules.xr": "1.0.0"
|
||||
}
|
||||
},
|
||||
"com.unity.modules.wind": {
|
||||
"version": "1.0.0",
|
||||
"depth": 0,
|
||||
"source": "builtin",
|
||||
"dependencies": {}
|
||||
},
|
||||
"com.unity.modules.xr": {
|
||||
"version": "1.0.0",
|
||||
"depth": 0,
|
||||
"source": "builtin",
|
||||
"dependencies": {
|
||||
"com.unity.modules.physics": "1.0.0",
|
||||
"com.unity.modules.jsonserialize": "1.0.0",
|
||||
"com.unity.modules.subsystems": "1.0.0"
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
19
ProjectSettings/AudioManager.asset
Normal file
19
ProjectSettings/AudioManager.asset
Normal file
|
|
@ -0,0 +1,19 @@
|
|||
%YAML 1.1
|
||||
%TAG !u! tag:unity3d.com,2011:
|
||||
--- !u!11 &1
|
||||
AudioManager:
|
||||
m_ObjectHideFlags: 0
|
||||
serializedVersion: 2
|
||||
m_Volume: 1
|
||||
Rolloff Scale: 1
|
||||
Doppler Factor: 1
|
||||
Default Speaker Mode: 2
|
||||
m_SampleRate: 0
|
||||
m_DSPBufferSize: 1024
|
||||
m_VirtualVoiceCount: 512
|
||||
m_RealVoiceCount: 32
|
||||
m_SpatializerPlugin:
|
||||
m_AmbisonicDecoderPlugin:
|
||||
m_DisableAudio: 0
|
||||
m_VirtualizeEffects: 1
|
||||
m_RequestedDSPBufferSize: 1024
|
||||
6
ProjectSettings/ClusterInputManager.asset
Normal file
6
ProjectSettings/ClusterInputManager.asset
Normal file
|
|
@ -0,0 +1,6 @@
|
|||
%YAML 1.1
|
||||
%TAG !u! tag:unity3d.com,2011:
|
||||
--- !u!236 &1
|
||||
ClusterInputManager:
|
||||
m_ObjectHideFlags: 0
|
||||
m_Inputs: []
|
||||
34
ProjectSettings/DynamicsManager.asset
Normal file
34
ProjectSettings/DynamicsManager.asset
Normal file
|
|
@ -0,0 +1,34 @@
|
|||
%YAML 1.1
|
||||
%TAG !u! tag:unity3d.com,2011:
|
||||
--- !u!55 &1
|
||||
PhysicsManager:
|
||||
m_ObjectHideFlags: 0
|
||||
serializedVersion: 11
|
||||
m_Gravity: {x: 0, y: -9.81, z: 0}
|
||||
m_DefaultMaterial: {fileID: 0}
|
||||
m_BounceThreshold: 2
|
||||
m_SleepThreshold: 0.005
|
||||
m_DefaultContactOffset: 0.01
|
||||
m_DefaultSolverIterations: 6
|
||||
m_DefaultSolverVelocityIterations: 1
|
||||
m_QueriesHitBackfaces: 0
|
||||
m_QueriesHitTriggers: 1
|
||||
m_EnableAdaptiveForce: 0
|
||||
m_ClothInterCollisionDistance: 0
|
||||
m_ClothInterCollisionStiffness: 0
|
||||
m_ContactsGeneration: 1
|
||||
m_LayerCollisionMatrix: ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff
|
||||
m_AutoSimulation: 1
|
||||
m_AutoSyncTransforms: 0
|
||||
m_ReuseCollisionCallbacks: 1
|
||||
m_ClothInterCollisionSettingsToggle: 0
|
||||
m_ContactPairsMode: 0
|
||||
m_BroadphaseType: 0
|
||||
m_WorldBounds:
|
||||
m_Center: {x: 0, y: 0, z: 0}
|
||||
m_Extent: {x: 250, y: 250, z: 250}
|
||||
m_WorldSubdivisions: 8
|
||||
m_FrictionType: 0
|
||||
m_EnableEnhancedDeterminism: 0
|
||||
m_EnableUnifiedHeightmaps: 1
|
||||
m_DefaultMaxAngluarSpeed: 7
|
||||
11
ProjectSettings/EditorBuildSettings.asset
Normal file
11
ProjectSettings/EditorBuildSettings.asset
Normal file
|
|
@ -0,0 +1,11 @@
|
|||
%YAML 1.1
|
||||
%TAG !u! tag:unity3d.com,2011:
|
||||
--- !u!1045 &1
|
||||
EditorBuildSettings:
|
||||
m_ObjectHideFlags: 0
|
||||
serializedVersion: 2
|
||||
m_Scenes:
|
||||
- enabled: 1
|
||||
path: Assets/Scenes/SampleScene.unity
|
||||
guid: 9fc0d4010bbf28b4594072e72b8655ab
|
||||
m_configObjects: {}
|
||||
35
ProjectSettings/EditorSettings.asset
Normal file
35
ProjectSettings/EditorSettings.asset
Normal file
|
|
@ -0,0 +1,35 @@
|
|||
%YAML 1.1
|
||||
%TAG !u! tag:unity3d.com,2011:
|
||||
--- !u!159 &1
|
||||
EditorSettings:
|
||||
m_ObjectHideFlags: 0
|
||||
serializedVersion: 9
|
||||
m_ExternalVersionControlSupport: Visible Meta Files
|
||||
m_SerializationMode: 2
|
||||
m_LineEndingsForNewScripts: 0
|
||||
m_DefaultBehaviorMode: 0
|
||||
m_PrefabRegularEnvironment: {fileID: 0}
|
||||
m_PrefabUIEnvironment: {fileID: 0}
|
||||
m_SpritePackerMode: 0
|
||||
m_SpritePackerPaddingPower: 1
|
||||
m_EtcTextureCompressorBehavior: 1
|
||||
m_EtcTextureFastCompressor: 1
|
||||
m_EtcTextureNormalCompressor: 2
|
||||
m_EtcTextureBestCompressor: 4
|
||||
m_ProjectGenerationIncludedExtensions: txt;xml;fnt;cd;asmdef;rsp;asmref
|
||||
m_ProjectGenerationRootNamespace:
|
||||
m_CollabEditorSettings:
|
||||
inProgressEnabled: 1
|
||||
m_EnableTextureStreamingInEditMode: 1
|
||||
m_EnableTextureStreamingInPlayMode: 1
|
||||
m_AsyncShaderCompilation: 1
|
||||
m_EnterPlayModeOptionsEnabled: 0
|
||||
m_EnterPlayModeOptions: 3
|
||||
m_ShowLightmapResolutionOverlay: 1
|
||||
m_UseLegacyProbeSampleCount: 0
|
||||
m_AssetPipelineMode: 1
|
||||
m_CacheServerMode: 0
|
||||
m_CacheServerEndpoint:
|
||||
m_CacheServerNamespacePrefix: default
|
||||
m_CacheServerEnableDownload: 1
|
||||
m_CacheServerEnableUpload: 1
|
||||
83
ProjectSettings/GraphicsSettings.asset
Normal file
83
ProjectSettings/GraphicsSettings.asset
Normal file
|
|
@ -0,0 +1,83 @@
|
|||
%YAML 1.1
|
||||
%TAG !u! tag:unity3d.com,2011:
|
||||
--- !u!30 &1
|
||||
GraphicsSettings:
|
||||
m_ObjectHideFlags: 0
|
||||
serializedVersion: 13
|
||||
m_Deferred:
|
||||
m_Mode: 1
|
||||
m_Shader: {fileID: 69, guid: 0000000000000000f000000000000000, type: 0}
|
||||
m_DeferredReflections:
|
||||
m_Mode: 1
|
||||
m_Shader: {fileID: 74, guid: 0000000000000000f000000000000000, type: 0}
|
||||
m_ScreenSpaceShadows:
|
||||
m_Mode: 1
|
||||
m_Shader: {fileID: 64, guid: 0000000000000000f000000000000000, type: 0}
|
||||
m_LegacyDeferred:
|
||||
m_Mode: 1
|
||||
m_Shader: {fileID: 63, guid: 0000000000000000f000000000000000, type: 0}
|
||||
m_DepthNormals:
|
||||
m_Mode: 1
|
||||
m_Shader: {fileID: 62, guid: 0000000000000000f000000000000000, type: 0}
|
||||
m_MotionVectors:
|
||||
m_Mode: 1
|
||||
m_Shader: {fileID: 75, guid: 0000000000000000f000000000000000, type: 0}
|
||||
m_LightHalo:
|
||||
m_Mode: 1
|
||||
m_Shader: {fileID: 105, guid: 0000000000000000f000000000000000, type: 0}
|
||||
m_LensFlare:
|
||||
m_Mode: 1
|
||||
m_Shader: {fileID: 102, guid: 0000000000000000f000000000000000, type: 0}
|
||||
m_AlwaysIncludedShaders:
|
||||
- {fileID: 7, guid: 0000000000000000f000000000000000, type: 0}
|
||||
- {fileID: 15104, guid: 0000000000000000f000000000000000, type: 0}
|
||||
- {fileID: 15105, guid: 0000000000000000f000000000000000, type: 0}
|
||||
- {fileID: 15106, guid: 0000000000000000f000000000000000, type: 0}
|
||||
- {fileID: 10753, guid: 0000000000000000f000000000000000, type: 0}
|
||||
- {fileID: 10770, guid: 0000000000000000f000000000000000, type: 0}
|
||||
- {fileID: 16000, guid: 0000000000000000f000000000000000, type: 0}
|
||||
- {fileID: 16001, guid: 0000000000000000f000000000000000, type: 0}
|
||||
- {fileID: 17000, guid: 0000000000000000f000000000000000, type: 0}
|
||||
m_PreloadedShaders: []
|
||||
m_SpritesDefaultMaterial: {fileID: 10754, guid: 0000000000000000f000000000000000,
|
||||
type: 0}
|
||||
m_CustomRenderPipeline: {fileID: 0}
|
||||
m_TransparencySortMode: 0
|
||||
m_TransparencySortAxis: {x: 0, y: 0, z: 1}
|
||||
m_DefaultRenderingPath: 1
|
||||
m_DefaultMobileRenderingPath: 1
|
||||
m_TierSettings:
|
||||
- serializedVersion: 5
|
||||
m_BuildTarget: 14
|
||||
m_Tier: 0
|
||||
m_Settings:
|
||||
standardShaderQuality: 0
|
||||
renderingPath: 1
|
||||
hdrMode: 2
|
||||
realtimeGICPUUsage: 25
|
||||
useReflectionProbeBoxProjection: 0
|
||||
useReflectionProbeBlending: 0
|
||||
useHDR: 0
|
||||
useDetailNormalMap: 0
|
||||
useCascadedShadowMaps: 0
|
||||
prefer32BitShadowMaps: 0
|
||||
enableLPPV: 0
|
||||
useDitherMaskForAlphaBlendedShadows: 0
|
||||
m_Automatic: 1
|
||||
m_LightmapStripping: 0
|
||||
m_FogStripping: 0
|
||||
m_InstancingStripping: 0
|
||||
m_LightmapKeepPlain: 1
|
||||
m_LightmapKeepDirCombined: 1
|
||||
m_LightmapKeepDynamicPlain: 1
|
||||
m_LightmapKeepDynamicDirCombined: 1
|
||||
m_LightmapKeepShadowMask: 1
|
||||
m_LightmapKeepSubtractive: 1
|
||||
m_FogKeepLinear: 1
|
||||
m_FogKeepExp: 1
|
||||
m_FogKeepExp2: 1
|
||||
m_AlbedoSwatchInfos: []
|
||||
m_LightsUseLinearIntensity: 0
|
||||
m_LightsUseColorTemperature: 0
|
||||
m_LogWhenShaderIsCompiled: 0
|
||||
m_AllowEnlightenSupportForUpgradedProject: 0
|
||||
295
ProjectSettings/InputManager.asset
Normal file
295
ProjectSettings/InputManager.asset
Normal file
|
|
@ -0,0 +1,295 @@
|
|||
%YAML 1.1
|
||||
%TAG !u! tag:unity3d.com,2011:
|
||||
--- !u!13 &1
|
||||
InputManager:
|
||||
m_ObjectHideFlags: 0
|
||||
serializedVersion: 2
|
||||
m_Axes:
|
||||
- serializedVersion: 3
|
||||
m_Name: Horizontal
|
||||
descriptiveName:
|
||||
descriptiveNegativeName:
|
||||
negativeButton: left
|
||||
positiveButton: right
|
||||
altNegativeButton: a
|
||||
altPositiveButton: d
|
||||
gravity: 3
|
||||
dead: 0.001
|
||||
sensitivity: 3
|
||||
snap: 1
|
||||
invert: 0
|
||||
type: 0
|
||||
axis: 0
|
||||
joyNum: 0
|
||||
- serializedVersion: 3
|
||||
m_Name: Vertical
|
||||
descriptiveName:
|
||||
descriptiveNegativeName:
|
||||
negativeButton: down
|
||||
positiveButton: up
|
||||
altNegativeButton: s
|
||||
altPositiveButton: w
|
||||
gravity: 3
|
||||
dead: 0.001
|
||||
sensitivity: 3
|
||||
snap: 1
|
||||
invert: 0
|
||||
type: 0
|
||||
axis: 0
|
||||
joyNum: 0
|
||||
- serializedVersion: 3
|
||||
m_Name: Fire1
|
||||
descriptiveName:
|
||||
descriptiveNegativeName:
|
||||
negativeButton:
|
||||
positiveButton: left ctrl
|
||||
altNegativeButton:
|
||||
altPositiveButton: mouse 0
|
||||
gravity: 1000
|
||||
dead: 0.001
|
||||
sensitivity: 1000
|
||||
snap: 0
|
||||
invert: 0
|
||||
type: 0
|
||||
axis: 0
|
||||
joyNum: 0
|
||||
- serializedVersion: 3
|
||||
m_Name: Fire2
|
||||
descriptiveName:
|
||||
descriptiveNegativeName:
|
||||
negativeButton:
|
||||
positiveButton: left alt
|
||||
altNegativeButton:
|
||||
altPositiveButton: mouse 1
|
||||
gravity: 1000
|
||||
dead: 0.001
|
||||
sensitivity: 1000
|
||||
snap: 0
|
||||
invert: 0
|
||||
type: 0
|
||||
axis: 0
|
||||
joyNum: 0
|
||||
- serializedVersion: 3
|
||||
m_Name: Fire3
|
||||
descriptiveName:
|
||||
descriptiveNegativeName:
|
||||
negativeButton:
|
||||
positiveButton: left shift
|
||||
altNegativeButton:
|
||||
altPositiveButton: mouse 2
|
||||
gravity: 1000
|
||||
dead: 0.001
|
||||
sensitivity: 1000
|
||||
snap: 0
|
||||
invert: 0
|
||||
type: 0
|
||||
axis: 0
|
||||
joyNum: 0
|
||||
- serializedVersion: 3
|
||||
m_Name: Jump
|
||||
descriptiveName:
|
||||
descriptiveNegativeName:
|
||||
negativeButton:
|
||||
positiveButton: space
|
||||
altNegativeButton:
|
||||
altPositiveButton:
|
||||
gravity: 1000
|
||||
dead: 0.001
|
||||
sensitivity: 1000
|
||||
snap: 0
|
||||
invert: 0
|
||||
type: 0
|
||||
axis: 0
|
||||
joyNum: 0
|
||||
- serializedVersion: 3
|
||||
m_Name: Mouse X
|
||||
descriptiveName:
|
||||
descriptiveNegativeName:
|
||||
negativeButton:
|
||||
positiveButton:
|
||||
altNegativeButton:
|
||||
altPositiveButton:
|
||||
gravity: 0
|
||||
dead: 0
|
||||
sensitivity: 0.1
|
||||
snap: 0
|
||||
invert: 0
|
||||
type: 1
|
||||
axis: 0
|
||||
joyNum: 0
|
||||
- serializedVersion: 3
|
||||
m_Name: Mouse Y
|
||||
descriptiveName:
|
||||
descriptiveNegativeName:
|
||||
negativeButton:
|
||||
positiveButton:
|
||||
altNegativeButton:
|
||||
altPositiveButton:
|
||||
gravity: 0
|
||||
dead: 0
|
||||
sensitivity: 0.1
|
||||
snap: 0
|
||||
invert: 0
|
||||
type: 1
|
||||
axis: 1
|
||||
joyNum: 0
|
||||
- serializedVersion: 3
|
||||
m_Name: Mouse ScrollWheel
|
||||
descriptiveName:
|
||||
descriptiveNegativeName:
|
||||
negativeButton:
|
||||
positiveButton:
|
||||
altNegativeButton:
|
||||
altPositiveButton:
|
||||
gravity: 0
|
||||
dead: 0
|
||||
sensitivity: 0.1
|
||||
snap: 0
|
||||
invert: 0
|
||||
type: 1
|
||||
axis: 2
|
||||
joyNum: 0
|
||||
- serializedVersion: 3
|
||||
m_Name: Horizontal
|
||||
descriptiveName:
|
||||
descriptiveNegativeName:
|
||||
negativeButton:
|
||||
positiveButton:
|
||||
altNegativeButton:
|
||||
altPositiveButton:
|
||||
gravity: 0
|
||||
dead: 0.19
|
||||
sensitivity: 1
|
||||
snap: 0
|
||||
invert: 0
|
||||
type: 2
|
||||
axis: 0
|
||||
joyNum: 0
|
||||
- serializedVersion: 3
|
||||
m_Name: Vertical
|
||||
descriptiveName:
|
||||
descriptiveNegativeName:
|
||||
negativeButton:
|
||||
positiveButton:
|
||||
altNegativeButton:
|
||||
altPositiveButton:
|
||||
gravity: 0
|
||||
dead: 0.19
|
||||
sensitivity: 1
|
||||
snap: 0
|
||||
invert: 1
|
||||
type: 2
|
||||
axis: 1
|
||||
joyNum: 0
|
||||
- serializedVersion: 3
|
||||
m_Name: Fire1
|
||||
descriptiveName:
|
||||
descriptiveNegativeName:
|
||||
negativeButton:
|
||||
positiveButton: joystick button 0
|
||||
altNegativeButton:
|
||||
altPositiveButton:
|
||||
gravity: 1000
|
||||
dead: 0.001
|
||||
sensitivity: 1000
|
||||
snap: 0
|
||||
invert: 0
|
||||
type: 0
|
||||
axis: 0
|
||||
joyNum: 0
|
||||
- serializedVersion: 3
|
||||
m_Name: Fire2
|
||||
descriptiveName:
|
||||
descriptiveNegativeName:
|
||||
negativeButton:
|
||||
positiveButton: joystick button 1
|
||||
altNegativeButton:
|
||||
altPositiveButton:
|
||||
gravity: 1000
|
||||
dead: 0.001
|
||||
sensitivity: 1000
|
||||
snap: 0
|
||||
invert: 0
|
||||
type: 0
|
||||
axis: 0
|
||||
joyNum: 0
|
||||
- serializedVersion: 3
|
||||
m_Name: Fire3
|
||||
descriptiveName:
|
||||
descriptiveNegativeName:
|
||||
negativeButton:
|
||||
positiveButton: joystick button 2
|
||||
altNegativeButton:
|
||||
altPositiveButton:
|
||||
gravity: 1000
|
||||
dead: 0.001
|
||||
sensitivity: 1000
|
||||
snap: 0
|
||||
invert: 0
|
||||
type: 0
|
||||
axis: 0
|
||||
joyNum: 0
|
||||
- serializedVersion: 3
|
||||
m_Name: Jump
|
||||
descriptiveName:
|
||||
descriptiveNegativeName:
|
||||
negativeButton:
|
||||
positiveButton: joystick button 3
|
||||
altNegativeButton:
|
||||
altPositiveButton:
|
||||
gravity: 1000
|
||||
dead: 0.001
|
||||
sensitivity: 1000
|
||||
snap: 0
|
||||
invert: 0
|
||||
type: 0
|
||||
axis: 0
|
||||
joyNum: 0
|
||||
- serializedVersion: 3
|
||||
m_Name: Submit
|
||||
descriptiveName:
|
||||
descriptiveNegativeName:
|
||||
negativeButton:
|
||||
positiveButton: return
|
||||
altNegativeButton:
|
||||
altPositiveButton: joystick button 0
|
||||
gravity: 1000
|
||||
dead: 0.001
|
||||
sensitivity: 1000
|
||||
snap: 0
|
||||
invert: 0
|
||||
type: 0
|
||||
axis: 0
|
||||
joyNum: 0
|
||||
- serializedVersion: 3
|
||||
m_Name: Submit
|
||||
descriptiveName:
|
||||
descriptiveNegativeName:
|
||||
negativeButton:
|
||||
positiveButton: enter
|
||||
altNegativeButton:
|
||||
altPositiveButton: space
|
||||
gravity: 1000
|
||||
dead: 0.001
|
||||
sensitivity: 1000
|
||||
snap: 0
|
||||
invert: 0
|
||||
type: 0
|
||||
axis: 0
|
||||
joyNum: 0
|
||||
- serializedVersion: 3
|
||||
m_Name: Cancel
|
||||
descriptiveName:
|
||||
descriptiveNegativeName:
|
||||
negativeButton:
|
||||
positiveButton: escape
|
||||
altNegativeButton:
|
||||
altPositiveButton: joystick button 1
|
||||
gravity: 1000
|
||||
dead: 0.001
|
||||
sensitivity: 1000
|
||||
snap: 0
|
||||
invert: 0
|
||||
type: 0
|
||||
axis: 0
|
||||
joyNum: 0
|
||||
Some files were not shown because too many files have changed in this diff Show more
Loading…
Add table
Reference in a new issue