1 | /* Copyright (C) 2011 Jan Källman
|
---|
2 | *
|
---|
3 | * This library is free software; you can redistribute it and/or
|
---|
4 | * modify it under the terms of the GNU Lesser General Public
|
---|
5 | * License as published by the Free Software Foundation; either
|
---|
6 | * version 2.1 of the License, or (at your option) any later version.
|
---|
7 |
|
---|
8 | * This library is distributed in the hope that it will be useful,
|
---|
9 | * but WITHOUT ANY WARRANTY; without even the implied warranty of
|
---|
10 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
|
---|
11 | * See the GNU Lesser General Public License for more details.
|
---|
12 | *
|
---|
13 | * The GNU Lesser General Public License can be viewed at http://www.opensource.org/licenses/lgpl-license.php
|
---|
14 | * If you unfamiliar with this license or have questions about it, here is an http://www.gnu.org/licenses/gpl-faq.html
|
---|
15 | *
|
---|
16 | * All code and executables are provided "as is" with no warranty either express or implied.
|
---|
17 | * The author accepts no liability for any damage or loss of business that this product may cause.
|
---|
18 | *
|
---|
19 | * Code change notes:
|
---|
20 | *
|
---|
21 | * Author Change Date
|
---|
22 | *******************************************************************************
|
---|
23 | * Mats Alm Added 2015-01-11
|
---|
24 | *******************************************************************************/
|
---|
25 | using System;
|
---|
26 | using System.Collections.Generic;
|
---|
27 | using System.Linq;
|
---|
28 | using System.Text;
|
---|
29 | using MathObj = System.Math;
|
---|
30 |
|
---|
31 | namespace OfficeOpenXml.FormulaParsing.Excel.Functions.Math
|
---|
32 | {
|
---|
33 | /// <summary>
|
---|
34 | /// Thanks to the guys in this thread: http://stackoverflow.com/questions/2840798/c-sharp-math-class-question
|
---|
35 | /// </summary>
|
---|
36 | public static class MathHelper
|
---|
37 | {
|
---|
38 | // Secant
|
---|
39 | public static double Sec(double x)
|
---|
40 | {
|
---|
41 | return 1 / MathObj.Cos(x);
|
---|
42 | }
|
---|
43 |
|
---|
44 | // Cosecant
|
---|
45 | public static double Cosec(double x)
|
---|
46 | {
|
---|
47 | return 1 / MathObj.Sin(x);
|
---|
48 | }
|
---|
49 |
|
---|
50 | // Cotangent
|
---|
51 | public static double Cotan(double x)
|
---|
52 | {
|
---|
53 | return 1 / MathObj.Tan(x);
|
---|
54 | }
|
---|
55 |
|
---|
56 | // Inverse Sine
|
---|
57 | public static double Arcsin(double x)
|
---|
58 | {
|
---|
59 | return MathObj.Atan(x / MathObj.Sqrt(-x * x + 1));
|
---|
60 | }
|
---|
61 |
|
---|
62 | // Inverse Cosine
|
---|
63 | public static double Arccos(double x)
|
---|
64 | {
|
---|
65 | return MathObj.Atan(-x / MathObj.Sqrt(-x * x + 1)) + 2 * MathObj.Atan(1);
|
---|
66 | }
|
---|
67 |
|
---|
68 |
|
---|
69 | // Inverse Secant
|
---|
70 | public static double Arcsec(double x)
|
---|
71 | {
|
---|
72 | return 2 * MathObj.Atan(1) - MathObj.Atan(MathObj.Sign(x) / MathObj.Sqrt(x * x - 1));
|
---|
73 | }
|
---|
74 |
|
---|
75 | // Inverse Cosecant
|
---|
76 | public static double Arccosec(double x)
|
---|
77 | {
|
---|
78 | return MathObj.Atan(MathObj.Sign(x) / MathObj.Sqrt(x * x - 1));
|
---|
79 | }
|
---|
80 |
|
---|
81 | // Inverse Cotangent
|
---|
82 | public static double Arccotan(double x)
|
---|
83 | {
|
---|
84 | return 2 * MathObj.Atan(1) - MathObj.Atan(x);
|
---|
85 | }
|
---|
86 |
|
---|
87 | // Hyperbolic Sine
|
---|
88 | public static double HSin(double x)
|
---|
89 | {
|
---|
90 | return (MathObj.Exp(x) - MathObj.Exp(-x)) / 2;
|
---|
91 | }
|
---|
92 |
|
---|
93 | // Hyperbolic Cosine
|
---|
94 | public static double HCos(double x)
|
---|
95 | {
|
---|
96 | return (MathObj.Exp(x) + MathObj.Exp(-x)) / 2;
|
---|
97 | }
|
---|
98 |
|
---|
99 | // Hyperbolic Tangent
|
---|
100 | public static double HTan(double x)
|
---|
101 | {
|
---|
102 | return (MathObj.Exp(x) - MathObj.Exp(-x)) / (MathObj.Exp(x) + MathObj.Exp(-x));
|
---|
103 | }
|
---|
104 |
|
---|
105 | // Hyperbolic Secant
|
---|
106 | public static double HSec(double x)
|
---|
107 | {
|
---|
108 | return 2 / (MathObj.Exp(x) + MathObj.Exp(-x));
|
---|
109 | }
|
---|
110 |
|
---|
111 | // Hyperbolic Cosecant
|
---|
112 | public static double HCosec(double x)
|
---|
113 | {
|
---|
114 | return 2 / (MathObj.Exp(x) - MathObj.Exp(-x));
|
---|
115 | }
|
---|
116 |
|
---|
117 | // Hyperbolic Cotangent
|
---|
118 | public static double HCotan(double x)
|
---|
119 | {
|
---|
120 | return (MathObj.Exp(x) + MathObj.Exp(-x)) / (MathObj.Exp(x) - MathObj.Exp(-x));
|
---|
121 | }
|
---|
122 |
|
---|
123 | // Inverse Hyperbolic Sine
|
---|
124 | public static double HArcsin(double x)
|
---|
125 | {
|
---|
126 | return MathObj.Log(x + MathObj.Sqrt(x * x + 1));
|
---|
127 | }
|
---|
128 |
|
---|
129 | // Inverse Hyperbolic Cosine
|
---|
130 | public static double HArccos(double x)
|
---|
131 | {
|
---|
132 | return MathObj.Log(x + MathObj.Sqrt(x * x - 1));
|
---|
133 | }
|
---|
134 |
|
---|
135 | // Inverse Hyperbolic Tangent
|
---|
136 | public static double HArctan(double x)
|
---|
137 | {
|
---|
138 | return MathObj.Log((1 + x) / (1 - x)) / 2;
|
---|
139 | }
|
---|
140 |
|
---|
141 | // Inverse Hyperbolic Secant
|
---|
142 | public static double HArcsec(double x)
|
---|
143 | {
|
---|
144 | return MathObj.Log((MathObj.Sqrt(-x * x + 1) + 1) / x);
|
---|
145 | }
|
---|
146 |
|
---|
147 | // Inverse Hyperbolic Cosecant
|
---|
148 | public static double HArccosec(double x)
|
---|
149 | {
|
---|
150 | return MathObj.Log((MathObj.Sign(x) * MathObj.Sqrt(x * x + 1) + 1) / x);
|
---|
151 | }
|
---|
152 |
|
---|
153 | // Inverse Hyperbolic Cotangent
|
---|
154 | public static double HArccotan(double x)
|
---|
155 | {
|
---|
156 | return MathObj.Log((x + 1) / (x - 1)) / 2;
|
---|
157 | }
|
---|
158 |
|
---|
159 | // Logarithm to base N
|
---|
160 | public static double LogN(double x, double n)
|
---|
161 | {
|
---|
162 | return MathObj.Log(x) / MathObj.Log(n);
|
---|
163 | }
|
---|
164 | }
|
---|
165 | }
|
---|