o0Project.cs 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6. using UnityEngine;
  7. namespace o0Project
  8. {
  9. public class Variance
  10. {
  11. List<float> _Value = new List<float>();
  12. int RecordCount;
  13. public Variance(int RecordCount = 100)
  14. {
  15. this.RecordCount = RecordCount;
  16. }
  17. public void Update(float value)
  18. {
  19. if (_Value.Count >= RecordCount)
  20. _Value.RemoveAt(0);
  21. _Value.Add(value);
  22. }
  23. public float Value
  24. {
  25. get
  26. {
  27. float average = 0;
  28. foreach (var i in _Value)
  29. average += i;
  30. average /= _Value.Count;
  31. float variance = 0;
  32. foreach (var i in _Value)
  33. variance += Mathf.Pow(average - i, 2);
  34. variance /= _Value.Count;
  35. return variance;
  36. }
  37. }
  38. public float StandardDeviation
  39. {
  40. get
  41. {
  42. return Mathf.Sqrt(Value);
  43. }
  44. }
  45. }
  46. public class Vector3f
  47. {
  48. public float x;
  49. public float y;
  50. public float z;
  51. public Vector3f() { }
  52. public Vector3f(Vector3f v)
  53. {
  54. x = v[0];
  55. y = v[1];
  56. z = v[2];
  57. }
  58. public Vector3f(float[] v)
  59. {
  60. x = v[0];
  61. y = v[1];
  62. z = v[2];
  63. }
  64. public Vector3f(float x = 0, float y = 0, float z = 0)
  65. {
  66. this.x = x;
  67. this.y = y;
  68. this.z = z;
  69. }
  70. public Vector3f assign(Vector3f v)
  71. {
  72. x = v.x;
  73. y = v.y;
  74. z = v.z;
  75. return this;
  76. }
  77. public Vector3f assign(float x, float y, float z)
  78. {
  79. this.x = x;
  80. this.y = y;
  81. this.z = z;
  82. return this;
  83. }
  84. public float length()//length of vector
  85. {
  86. return Mathf.Sqrt(x * x + y * y + z * z);
  87. }
  88. public Vector3f setNormalize()
  89. {
  90. return assign(this / length());
  91. }
  92. public Vector3f setVertical(Vector3f b)
  93. {
  94. return assign(this[1] * b[2] - this[2] * b[1],
  95. this[2] * b[0] - this[0] * b[2],
  96. this[0] * b[1] - this[1] * b[0]);
  97. }
  98. public float angle(Vector3f b)
  99. {
  100. return Mathf.Asin(Mathf.Sqrt(Mathf.Pow(this[0] - b[0], 2) + Mathf.Pow(this[1] - b[1], 2) + Mathf.Pow(this[2] - b[2], 2)) / 2) * 2;
  101. }
  102. public Vector3f setRotate(Vector3f shaft, float angle)
  103. {
  104. float cosAngle = Mathf.Cos(angle);
  105. float sinAngle = Mathf.Sin(angle);
  106. float rotateDate = (shaft[0] * this[0] + shaft[1] * this[1] + shaft[2] * this[2]) * (1 - cosAngle);
  107. Vector3f outerProduct = (new Vector3f(this)).setVertical(shaft);
  108. return assign(this[0] * cosAngle + outerProduct[0] * sinAngle + shaft[0] * rotateDate,
  109. this[1] * cosAngle + outerProduct[1] * sinAngle + shaft[1] * rotateDate,
  110. this[2] * cosAngle + outerProduct[2] * sinAngle + shaft[2] * rotateDate);
  111. }
  112. public float this[int index]
  113. {
  114. get
  115. {
  116. switch (index)
  117. {
  118. default:
  119. case 0:
  120. return x;
  121. case 1:
  122. return y;
  123. case 2:
  124. return z;
  125. }
  126. }
  127. set
  128. {
  129. switch (index)
  130. {
  131. default:
  132. case 0:
  133. x = value;
  134. break;
  135. case 1:
  136. y = value;
  137. break;
  138. case 2:
  139. z = value;
  140. break;
  141. }
  142. }
  143. }
  144. public static Vector3f operator +(Vector3f a, Vector3f b)
  145. {
  146. return new Vector3f(a.x + b.x, a.y + b.y, a.z + b.z);
  147. }
  148. public static Vector3f operator -(Vector3f a, Vector3f b)
  149. {
  150. return new Vector3f(a.x - b.x, a.y - b.y, a.z - b.z);
  151. }
  152. public static Vector3f operator *(Vector3f a, float b)
  153. {
  154. return new Vector3f(a.x * b, a.y * b, a.z * b);
  155. }
  156. public static Vector3f operator /(Vector3f a, float b)
  157. {
  158. return new Vector3f(a.x / b, a.y / b, a.z / b);
  159. }
  160. }
  161. static public class o0
  162. {
  163. public static Quaternion FormQuaternion(Vector3 Direction1InIdentity, Vector3 Direction2InIdentity, Vector3 Direction1InQuaternion, Vector3 Direction2InQuaternion,float rate)
  164. {
  165. var quaIdentity = Quaternion.LookRotation(Direction1InIdentity, Direction2InIdentity);
  166. var quaReal = Quaternion.LookRotation(Direction1InQuaternion, Direction2InQuaternion);
  167. var quaIdentity2 = Quaternion.LookRotation(Direction2InIdentity, Direction1InIdentity);
  168. var quaReal2 = Quaternion.LookRotation(Direction2InQuaternion, Direction1InQuaternion);
  169. return Quaternion.Slerp(quaIdentity * Quaternion.Inverse(quaReal), quaIdentity2 * Quaternion.Inverse(quaReal2), rate);
  170. }
  171. public static Quaternion FormQuaternion(Quaternion original, Vector3 DirectionIdentity, Vector3 Direction1New, float rate)
  172. {
  173. var global = original * Direction1New;
  174. Vector3 DirectionIdentityNormalized = DirectionIdentity.normalized;
  175. float angle = Vector3.Angle(global, DirectionIdentityNormalized);
  176. //Vector3 gVector = Vector3.RotateTowards(acceleratorGlobal, downVector, angle / 180 * Mathf.PI * Mathf.Pow(overallGVectorCredibility, 1f) * rotationUnCredibility, 1);//偏移量,从陀螺仪数据往加速计数据的偏移量
  177. Vector3 gVector = Vector3.RotateTowards(global, DirectionIdentityNormalized, angle / 180 * Mathf.PI * rate, 1);//偏移量,从陀螺仪数据往加速计数据的偏移量
  178. var newQua = new Quaternion();
  179. if (float.IsNaN(gVector.x))
  180. return original;
  181. newQua.SetFromToRotation(original * Direction1New, gVector);
  182. /*
  183. Debug.Log("startIn");
  184. Debug.Log(original * Direction1New);
  185. Debug.Log(original);
  186. Debug.Log(Direction1New);
  187. Debug.Log(gVector);
  188. Debug.Log(newQua);
  189. Debug.Log("end");/**/
  190. if (float.IsNaN(newQua.w))
  191. return original;
  192. /*
  193. try
  194. {
  195. newQua.SetFromToRotation(original * Direction1New, gVector);
  196. if(float.IsNaN(newQua.w))
  197. return original;
  198. }
  199. catch (Exception)
  200. {
  201. return original;
  202. }/**/
  203. return newQua * original;//一定要反过来乘
  204. }
  205. }
  206. static public class Extension
  207. {
  208. public static Quaternion Inverse(this Quaternion qua)
  209. {
  210. return new Quaternion(-qua.x,-qua.y,-qua.z,qua.w);
  211. }
  212. public static Quaternion ToQuaternion(this MatrixF2D m)
  213. {
  214. if(m.Width != 4 || m.Height!=4)
  215. throw new Exception("o0 FMatrix m is not 4*4 transfer matrix");
  216. Quaternion q = new Quaternion();
  217. q.w = Mathf.Sqrt(Mathf.Max(0, 1 + m[0, 0] + m[1, 1] + m[2, 2])) / 2;
  218. q.x = Mathf.Sqrt(Mathf.Max(0, 1 + m[0, 0] - m[1, 1] - m[2, 2])) / 2;
  219. q.y = Mathf.Sqrt(Mathf.Max(0, 1 - m[0, 0] + m[1, 1] - m[2, 2])) / 2;
  220. q.z = Mathf.Sqrt(Mathf.Max(0, 1 - m[0, 0] - m[1, 1] + m[2, 2])) / 2;
  221. q.x *= Mathf.Sign(q.x * (m[2, 1] - m[1, 2]));
  222. q.y *= Mathf.Sign(q.y * (m[0, 2] - m[2, 0]));
  223. q.z *= Mathf.Sign(q.z * (m[1, 0] - m[0, 1]));
  224. return q;
  225. }
  226. public static MatrixF2D ToMatrix(Vector3 right, Vector3 up, Vector3 forward, Vector3 position)
  227. {
  228. var m = MatrixF2D.Identity(4);
  229. m.SetColumn(0, new float[] { right.x,right.y,right.z,0});
  230. m.SetColumn(1, new float[] { up.x, up.y, up.z, 0 });
  231. m.SetColumn(2, new float[] { forward.x, forward.y, forward.z, 0 });
  232. m.SetColumn(3, new float[] { position.x, position.y, position.z, 0 });
  233. m[3, 3] = 1;
  234. return m;
  235. }
  236. public static MatrixF2D ToMatrix(Quaternion q)
  237. {
  238. return ToMatrix(q * Vector3.right, q * Vector3.up, q * Vector3.forward, Vector3.zero);
  239. }
  240. }
  241. public class MatrixF2D
  242. {
  243. private int _Width;
  244. public int Width { get { return _Width; } }
  245. public int Height { get { return _Element.Length / Width; } }
  246. private float[] _Element;
  247. public float[] Element { get { return _Element; } }
  248. public float this[int y, int x] {
  249. get {
  250. x %= Width;
  251. if (x < 0) x += Width;
  252. y %= Height;
  253. if (y < 0) y += Height;
  254. return _Element[x + y * Width];
  255. }
  256. set{
  257. x %= Width;
  258. if (x < 0) x += Width;
  259. y %= Height;
  260. if (y < 0) y += Height;
  261. _Element[x + y * Width] = value;
  262. }
  263. }
  264. public float[] GetRow(int y)
  265. {
  266. y %= Height;
  267. if (y < 0) y += Height;
  268. float[] l = new float[Width];
  269. for (var i = 0; i < Width; ++i)
  270. l[i] = this[y, i];
  271. return l;
  272. }
  273. public void SetRow(int y, IEnumerable<float> value)
  274. {
  275. if (value.Count() != Width)
  276. throw new Exception("o0 FMatrix width different");
  277. for (var i = 0; i < Width; ++i)
  278. this[y, i] = value.ElementAt(i);
  279. }
  280. public float[] GetColumn(int x)
  281. {
  282. x %= Width;
  283. if (x < 0) x += Width;
  284. float[] l = new float[Height];
  285. for (var i = 0; i < Height; ++i)
  286. l[i] = this[i, x];
  287. return l;
  288. }
  289. public void SetColumn(int x, IEnumerable<float> value)
  290. {
  291. if (value.Count() != Height)
  292. throw new Exception("o0 FMatrix width different");
  293. for (var i = 0; i < Height; ++i)
  294. this[i, x] = value.ElementAt(i);
  295. }
  296. public MatrixF2D(int width = 2, int height = 2)
  297. {
  298. _Width = width;
  299. _Element = new float[width * height];
  300. }
  301. public MatrixF2D(int width, float[] element)
  302. {
  303. _Width = width;
  304. _Element = new float[element.Length];
  305. for (var i = 0; i < element.Length; ++i)
  306. _Element[i] = element[i];
  307. }
  308. public MatrixF2D(MatrixF2D b)
  309. {
  310. _Width = b.Width;
  311. _Element = new float[b.Element.Length];
  312. for (var i = 0; i < _Element.Length; ++i)
  313. _Element[i] = b.Element[i];
  314. }
  315. public static MatrixF2D operator +(MatrixF2D a, MatrixF2D b)
  316. {
  317. if (a.Width != b.Width || a.Height != b.Height)
  318. throw new Exception("o0 FMatrix size different");
  319. var c = new MatrixF2D(a.Width, a.Height);
  320. for (var i = 0; i < a.Element.Length; ++i)
  321. c.Element[i] = a.Element[i] + b.Element[i];
  322. return c;
  323. }
  324. public static MatrixF2D operator -(MatrixF2D a, MatrixF2D b)
  325. {
  326. if (a.Width != b.Width || a.Height != b.Height)
  327. throw new Exception("o0 FMatrix size different");
  328. var c = new MatrixF2D(a.Width, a.Height);
  329. for (var i = 0; i < a.Element.Length; ++i)
  330. c.Element[i] = a.Element[i] - b.Element[i];
  331. return c;
  332. }
  333. public static MatrixF2D operator *(MatrixF2D a, MatrixF2D b)
  334. {
  335. if (a.Width != b.Height)
  336. throw new Exception("o0 FMatrix 当矩阵A的列数(column)等于矩阵B的行数(row)时,A与B可以相乘");
  337. var c = new MatrixF2D(b.Width,a.Height);
  338. for (var x = 0;x<c.Width;++x)
  339. for (var y = 0; y < c.Height; ++y)
  340. for (var z = 0; z < a.Width; ++z)
  341. {
  342. //c[x, y] += a[y, z] * b[z, x];
  343. c[y, x] += a[y,z] * b[z,x];
  344. }
  345. return c;
  346. }
  347. public static MatrixF2D operator *(MatrixF2D a, float b)
  348. {
  349. var c = new MatrixF2D(a);
  350. for (var x = 0; x < c.Width; ++x)
  351. for (var y = 0; y < c.Height; ++y)
  352. c[y,x] *= b;
  353. return c;
  354. }
  355. public static MatrixF2D operator *(float b, MatrixF2D a)
  356. {
  357. return a*b;
  358. }
  359. public static MatrixF2D operator /(MatrixF2D a, float b)
  360. {
  361. return a*(1/b);
  362. }
  363. public MatrixF2D T
  364. {
  365. get
  366. {
  367. var matrix = new MatrixF2D(Height, Width);
  368. for (var x = 0; x < Width; ++x)
  369. for (var y = 0; y < Height; ++y)
  370. matrix[x,y] = this[y,x];
  371. return matrix;
  372. }
  373. }
  374. public float trace//tr
  375. {
  376. get
  377. {
  378. if (Width != Height)
  379. throw new Exception("o0 FMatrix 非实对称矩阵");
  380. float x=0;
  381. for (var i = 0; i < Width; ++i)
  382. x += this[i, i];
  383. return x;
  384. }
  385. }
  386. public float det//行列式
  387. {
  388. get
  389. {
  390. if (Width != Height)
  391. throw new Exception("o0 FMatrix 非实对称矩阵");
  392. float d = 0;
  393. for (var x = 0; x < Width; ++x)
  394. {
  395. float m = 1;
  396. for (var y = 0; y < Width; ++y)
  397. m *= this[y,x + y];
  398. d += m;
  399. }
  400. for (var x = 0; x < Width; ++x)
  401. {
  402. float m = 1;
  403. for (var y = 0; y < Width; ++y)
  404. m *= this[y,x - y];
  405. d -= m;
  406. }
  407. return d;
  408. }
  409. }
  410. public MatrixF2D Inverse
  411. {
  412. get
  413. {
  414. if (Width != Height)//还需要额外判断条件
  415. throw new Exception("o0 FMatrix 非实对称矩阵");
  416. var n = Width;
  417. int i;
  418. int j;
  419. int k;
  420. var Metrix = new MatrixF2D(this);
  421. for (k = 0; k < n; k++)
  422. {
  423. for (i = 0; i < n; i++)
  424. {
  425. if (i != k) Metrix.Element[i * n + k] = -Metrix.Element[i * n + k] / Metrix.Element[k * n + k];
  426. }
  427. Metrix.Element[k * n + k] = 1 / Metrix.Element[k * n + k];
  428. for (i = 0; i < n; i++) {
  429. if (i != k) {
  430. for (j = 0; j < n; j++) {
  431. if (j != k) Metrix.Element[i * n + j] += Metrix.Element[k * n + j] * Metrix.Element[i * n + k];
  432. }
  433. }
  434. }
  435. for (j = 0; j < n; j++) { if (j != k) Metrix.Element[k * n + j] *= Metrix.Element[k * n + k]; }
  436. }
  437. return Metrix;
  438. }
  439. }
  440. public override string ToString()
  441. {
  442. var s = "";
  443. for (var y = 0; y < Height; ++y)
  444. {
  445. if (y == 0)
  446. s += "[";
  447. else
  448. s += ",\n";
  449. for (var x = 0; x < Width; ++x)
  450. {
  451. if (x == 0)
  452. s += "[";
  453. else
  454. s += ",\t";
  455. s += this[y,x].ToString("0.00000");
  456. }
  457. s += "]";
  458. }
  459. s += "]";
  460. return s;
  461. }
  462. public static MatrixF2D Identity(int size)
  463. {
  464. var m = new MatrixF2D(4,4);
  465. for (var i = 0; i < m.Width; ++i)
  466. m[i, i] = 1;
  467. return m;
  468. }
  469. }
  470. /*
  471. public class MatrixD2D
  472. {
  473. private int _Width;
  474. public int Width { get { return _Width; } }
  475. public int Height { get { return _Element.Length / Width; } }
  476. private double[] _Element;
  477. public double[] Element { get { return _Element; } }
  478. public double this[int y, int x]
  479. {
  480. get
  481. {
  482. x %= Width;
  483. if (x < 0) x += Width;
  484. y %= Height;
  485. if (y < 0) y += Height;
  486. return _Element[x + y * Width];
  487. }
  488. set
  489. {
  490. x %= Width;
  491. if (x < 0) x += Width;
  492. y %= Height;
  493. if (y < 0) y += Height;
  494. _Element[x + y * Width] = value;
  495. }
  496. }
  497. public double[] GetRow(int y)
  498. {
  499. y %= Height;
  500. if (y < 0) y += Height;
  501. double[] l = new double[Width];
  502. for (var i = 0; i < Width; ++i)
  503. l[i] = this[y, i];
  504. return l;
  505. }
  506. public void SetRow(int y, IEnumerable<float> value)
  507. {
  508. if (value.Count() != Width)
  509. throw new Exception("o0 FMatrix width different");
  510. for (var i = 0; i < Width; ++i)
  511. this[y, i] = value.ElementAt(i);
  512. }
  513. public double[] GetColumn(int x)
  514. {
  515. x %= Width;
  516. if (x < 0) x += Width;
  517. double[] l = new double[Height];
  518. for (var i = 0; i < Height; ++i)
  519. l[i] = this[i, x];
  520. return l;
  521. }
  522. public void SetColumn(int x, IEnumerable<float> value)
  523. {
  524. if (value.Count() != Height)
  525. throw new Exception("o0 FMatrix width different");
  526. for (var i = 0; i < Height; ++i)
  527. this[i, x] = value.ElementAt(i);
  528. }
  529. public MatrixD2D(int width = 2, int height = 2)
  530. {
  531. _Width = width;
  532. _Element = new double[width * height];
  533. }
  534. public MatrixD2D(int width, double[] element)
  535. {
  536. _Width = width;
  537. _Element = new double[element.Length];
  538. for (var i = 0; i < element.Length; ++i)
  539. _Element[i] = element[i];
  540. }
  541. public MatrixD2D(MatrixD2D b)
  542. {
  543. _Width = b.Width;
  544. _Element = new double[b.Element.Length];
  545. for (var i = 0; i < _Element.Length; ++i)
  546. _Element[i] = b.Element[i];
  547. }
  548. public static MatrixD2D operator +(MatrixD2D a, MatrixD2D b)
  549. {
  550. if (a.Width != b.Width || a.Height != b.Height)
  551. throw new Exception("o0 FMatrix size different");
  552. var c = new MatrixD2D(a.Width, a.Height);
  553. for (var i = 0; i < a.Element.Length; ++i)
  554. c.Element[i] = a.Element[i] + b.Element[i];
  555. return c;
  556. }
  557. public static MatrixD2D operator -(MatrixD2D a, MatrixD2D b)
  558. {
  559. if (a.Width != b.Width || a.Height != b.Height)
  560. throw new Exception("o0 FMatrix size different");
  561. var c = new MatrixD2D(a.Width, a.Height);
  562. for (var i = 0; i < a.Element.Length; ++i)
  563. c.Element[i] = a.Element[i] - b.Element[i];
  564. return c;
  565. }
  566. public static MatrixD2D operator *(MatrixD2D a, MatrixD2D b)
  567. {
  568. if (a.Width != b.Height)
  569. throw new Exception("o0 FMatrix 当矩阵A的列数(column)等于矩阵B的行数(row)时,A与B可以相乘");
  570. var c = new MatrixD2D(b.Width, a.Height);
  571. for (var x = 0; x < c.Width; ++x)
  572. for (var y = 0; y < c.Height; ++y)
  573. for (var z = 0; z < a.Width; ++z)
  574. {
  575. //c[x, y] += a[y, z] * b[z, x];
  576. c[y, x] += a[y, z] * b[z, x];
  577. }
  578. return c;
  579. }
  580. public static MatrixD2D operator *(MatrixD2D a, float b)
  581. {
  582. var c = new MatrixD2D(a);
  583. for (var x = 0; x < c.Width; ++x)
  584. for (var y = 0; y < c.Height; ++y)
  585. c[y, x] *= b;
  586. return c;
  587. }
  588. public static MatrixD2D operator *(float b, MatrixD2D a)
  589. {
  590. return a * b;
  591. }
  592. public static MatrixD2D operator /(MatrixD2D a, float b)
  593. {
  594. return a * (1 / b);
  595. }
  596. public MatrixD2D T
  597. {
  598. get
  599. {
  600. var matrix = new MatrixD2D(Height, Width);
  601. for (var x = 0; x < Width; ++x)
  602. for (var y = 0; y < Height; ++y)
  603. matrix[x, y] = this[y, x];
  604. return matrix;
  605. }
  606. }
  607. public double trace//tr
  608. {
  609. get
  610. {
  611. if (Width != Height)
  612. throw new Exception("o0 FMatrix 非实对称矩阵");
  613. double x = 0;
  614. for (var i = 0; i < Width; ++i)
  615. x += this[i, i];
  616. return x;
  617. }
  618. }
  619. public double det//行列式
  620. {
  621. get
  622. {
  623. if (Width != Height)
  624. throw new Exception("o0 FMatrix 非实对称矩阵");
  625. double d = 0;
  626. for (var x = 0; x < Width; ++x)
  627. {
  628. double m = 1;
  629. for (var y = 0; y < Width; ++y)
  630. m *= this[y, x + y];
  631. d += m;
  632. }
  633. for (var x = 0; x < Width; ++x)
  634. {
  635. double m = 1;
  636. for (var y = 0; y < Width; ++y)
  637. m *= this[y, x - y];
  638. d -= m;
  639. }
  640. return d;
  641. }
  642. }
  643. public MatrixD2D Inverse
  644. {
  645. get
  646. {
  647. if (Width != Height)//还需要额外判断条件
  648. throw new Exception("o0 FMatrix 非实对称矩阵");
  649. var n = Width;
  650. int i;
  651. int j;
  652. int k;
  653. var Metrix = new MatrixD2D(this);
  654. for (k = 0; k < n; k++)
  655. {
  656. for (i = 0; i < n; i++)
  657. {
  658. if (i != k) Metrix.Element[i * n + k] = -Metrix.Element[i * n + k] / Metrix.Element[k * n + k];
  659. }
  660. Metrix.Element[k * n + k] = 1 / Metrix.Element[k * n + k];
  661. for (i = 0; i < n; i++)
  662. {
  663. if (i != k)
  664. {
  665. for (j = 0; j < n; j++)
  666. {
  667. if (j != k) Metrix.Element[i * n + j] += Metrix.Element[k * n + j] * Metrix.Element[i * n + k];
  668. }
  669. }
  670. }
  671. for (j = 0; j < n; j++) { if (j != k) Metrix.Element[k * n + j] *= Metrix.Element[k * n + k]; }
  672. }
  673. return Metrix;
  674. }
  675. }
  676. public override string ToString()
  677. {
  678. var s = "";
  679. for (var y = 0; y < Height; ++y)
  680. {
  681. if (y == 0)
  682. s += "[";
  683. else
  684. s += ",\n";
  685. for (var x = 0; x < Width; ++x)
  686. {
  687. if (x == 0)
  688. s += "[";
  689. else
  690. s += ",\t";
  691. s += this[y, x].ToString("0.00000");
  692. }
  693. s += "]";
  694. }
  695. s += "]";
  696. return s;
  697. }
  698. public static MatrixD2D Identity(int size)
  699. {
  700. var m = new MatrixD2D(4, 4);
  701. for (var i = 0; i < m.Width; ++i)
  702. m[i, i] = 1;
  703. return m;
  704. }
  705. }/**/
  706. }