Advertisement
Guest User

Untitled

a guest
Oct 23rd, 2019
107
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.14 KB | None | 0 0
  1. import std.math : sqrt, abs;
  2. import std.format : format;
  3. import std.stdio : writeln, writefln;
  4.  
  5.  
  6. struct Point {
  7. real x, y, z;
  8. this(real[] arr...) @safe pure nothrow {
  9. this.x = arr[0];
  10. this.y = arr[1];
  11. this.z = arr[2];
  12. }
  13. string toString() {
  14. return format!"(%s, %s, %s)"(this.x, this.y, this.z);
  15. }
  16. }
  17.  
  18. struct Vector {
  19. real x, y, z;
  20. this(real[] arr...) @safe pure nothrow {
  21. this.x = arr[0];
  22. this.y = arr[1];
  23. this.z = arr[2];
  24. }
  25. this(in Point begin, in Point end) @safe pure nothrow {
  26. static foreach(ch; [ 'x', 'y', 'z' ])
  27. mixin("this." ~ ch ~ " = end." ~ ch ~ " - begin." ~ ch ~ ';');
  28. }
  29. real length() const @safe @nogc pure nothrow {
  30. return sqrt(this.x ^^ 2 + this.y ^^ 2 + this.z ^^ 2);
  31. }
  32. real squareLength() const @safe @nogc pure nothrow {
  33. return this.x ^^ 2 + this.y ^^ 2 + this.z ^^ 2;
  34. }
  35. ref Vector opUnary(string op)() {
  36. static if (op == "-") {
  37. static foreach (str; [ "this.x", "this.y", "this.z"])
  38. mixin(str ~ " = " ~ '-' ~ str ~ ';');
  39. return this;
  40. }
  41. else static if (op == "+")
  42. return this;
  43. else
  44. static assert(0, "Unary operator " ~ op ~ " is not supported for Vector.");
  45. }
  46. Vector opBinary(string op)(auto ref Vector other) const {
  47. static if (op == "+")
  48. return Vector(this.x + other.x, this.y + other.y, this.z + other.z);
  49. else static if (op == "-")
  50. return Vector(this.x - other.x, this.y - other.y, this.z - other.z);
  51. else static if (op == "*")
  52. return Vector(this.y * other.z - this.z * other.y, this.z * other.x - this.x * other.z, this.x * other.y - this.y * other.x);
  53. else
  54. static assert(0, "Binary operator " ~ op ~ " is not supported for Vector.");
  55. }
  56. Vector opBinary(string op, T : real)(auto ref T c) const {
  57. static if (op == "*")
  58. return Vector(c * this.x, c * this.y, c * this.z);
  59. else static if (op == "/")
  60. return Vector(this.x / c, this.y / c, this.z / c);
  61. else
  62. static assert(0, "Binary operator " ~ op ~ " is not supported for Vector.");
  63. }
  64. bool opEquals()(auto ref const Vector v) const {
  65. return this.x == v.x && this.y == v.y && this.z == v.z;
  66. }
  67. bool opCast(T : bool)() const {
  68. return this.x != 0 || this.y != 0 || this.z != 0;
  69. }
  70. string toString() {
  71. return format!"{%s, %s, %s}"(this.x, this.y, this.z);
  72. }
  73. }
  74.  
  75. real scalarProduct()(auto ref Vector v1, auto ref Vector v2) @safe @nogc pure nothrow {
  76. return v1.x * v2.x + v1.y * v2.y + v1.z * v2.z;
  77. }
  78.  
  79. /* x1/x2 = y1/y2 = z1/z2 */
  80. bool isCollinear()(auto ref Vector v1, auto ref Vector v2) @safe pure nothrow {
  81. return (v1.x * v2.y == v2.x * v1.y) && (v2.y * v1.z == v1.y * v2.z);
  82. }
  83.  
  84. struct Equation {
  85. real a, b, c, d;
  86. this(real[] arr...) @safe pure nothrow {
  87. this.a = arr[0];
  88. this.b = arr[1];
  89. this.c = arr[2];
  90. this.d = arr[3];
  91. }
  92. }
  93.  
  94. struct Determinant {
  95. real value;
  96. this(real[][] vals) @safe pure nothrow {
  97. this.value = vals[0][0] * (vals[1][1] * vals[2][2] - vals[1][2] * vals[2][1])
  98. - vals[0][1] * (vals[1][0] * vals[2][2] - vals[1][2] * vals[2][0]) + vals[0][2] * (vals[1][0] * vals[2][1] - vals[1][1] * vals[2][0]);
  99. }
  100. }
  101.  
  102. @safe unittest {
  103. auto d1 = Determinant([ [1, 1, 1], [2, 2, 2], [22, 40, 0] ]),
  104. d2 = Determinant([ [1, 0, 0], [2, 2, 0], [33, 3, 3] ]);
  105. assert(d1.value == 0);
  106. assert(d2.value == 6);
  107. }
  108.  
  109. struct SystemOfEquations {
  110. private:
  111. Equation[] eqs;
  112. public:
  113. this(Equation[] arr...) @safe pure nothrow {
  114. assert(arr.length == 3);
  115. this.eqs = arr.dup;
  116. }
  117. Point solve() @safe pure nothrow {
  118. auto det = Determinant([ [eqs[0].a, eqs[0].b, eqs[0].c], [eqs[1].a, eqs[1].b, eqs[1].c], [eqs[2].a, eqs[2].b, eqs[2].c] ]),
  119. det_x = Determinant([ [eqs[0].d, eqs[0].b, eqs[0].c], [eqs[1].d, eqs[1].b, eqs[1].c], [eqs[2].d, eqs[2].b, eqs[2].c] ]),
  120. det_y = Determinant([ [eqs[0].a, eqs[0].d, eqs[0].c], [eqs[1].a, eqs[1].d, eqs[1].c], [eqs[2].a, eqs[2].d, eqs[2].c] ]),
  121. det_z = Determinant([ [eqs[0].a, eqs[0].b, eqs[0].d], [eqs[1].a, eqs[1].b, eqs[1].d], [eqs[2].a, eqs[2].b, eqs[2].d] ]);
  122. return Point(det_x.value / det.value, det_y.value / det.value, det_z.value / det.value);
  123. }
  124. }
  125.  
  126. int main(string[] args) {
  127. auto A = Point(0, 0, -1),
  128. B = Point(0, 1, 0),
  129. D = Point(0, 2, -3),
  130. A1 = Point(1, 0, 2);
  131. byte a = 4, b = -1;
  132. double cos_phi = - 0.5;
  133. /* Task 1 */
  134. writeln("Задание 1.");
  135. auto l = (b + 1) ^^ 2 + (a + 1) ^^ 2 + 2 * (a + 1) * (b + 1) * cos_phi;
  136. writefln!"Ответ: |m + n| = sqrt(%s) = %s"(l, sqrt(l));
  137. /* Task 2 */
  138. writeln("\nЗадание 2.");
  139. auto AB = Vector(A, B),
  140. AM = (AB * a) / (a + 1);
  141. writefln!"AB = %s, AM = %s"(AB, AM);
  142. writefln!"Ответ: M(%s, %s, %s)"(AM.x + A.x, AM.y + A.y, AM.z + A.z);
  143. /* Task 3 */
  144. writeln("\nЗадание 3.");
  145. auto AD = Vector(A, D);
  146. writefln!"AD = %s"(AD);
  147. if (!AB.isCollinear(AD))
  148. writeln("Ответ: можно.");
  149. writefln!"|AD| = sqrt(%s) = %s, |AB| = sqrt(%s) = %s"(AD.squareLength, AD.length, AB.squareLength, AB.length);
  150. /* Task 4 */
  151. auto d1 = AB - AD,
  152. d2 = AB + AD;
  153. writeln("\nЗадание 4.");
  154. writefln!"Диагонали: d1 = AB - AD = %s, d2 = AB + AD = %s"(d1, d2);
  155. writefln!"|d1| = sqrt(%s) = %s, |d2| = sqrt(%s) = %s"(d1. squareLength, d1.length, d2.squareLength, d2.length);
  156. auto angle_cos = scalarProduct(d1, d2) / (d1.length * d2.length);
  157. writefln!"Ответ: arccos(%s/sqrt(%s)"(abs(scalarProduct(d1, d2)), d1.squareLength * d2.squareLength);
  158. /* Task 5 */
  159. auto s = AD * AB;
  160. writefln!"\nЗадание 5.\nОтвет: S = sqrt(%s) = %s"(s.squareLength, s.length);
  161. /* Task 6 */
  162. writeln("\nЗадание 6.");
  163. auto AA1 = Vector(A, A1);
  164. writefln!"AA1 = %s"(AA1);
  165. auto V = scalarProduct(s, AA1);
  166. writefln!"Ответ: AB x AD * AA1 = %s"(V);
  167. /* Task 7 */
  168. writeln("\nЗадание 7.");
  169. writefln!"Пусть s = AD x AB. Тогда s = %s."(s);
  170. auto tmp = scalarProduct(s, AA1);
  171. writefln!"s * AA1 = %s"(tmp);
  172. auto p = tmp / s.length;
  173. writefln!"|AH| = пр_(s)(AA1) = %s/sqrt(%s) = %s"(tmp, s.squareLength, p);
  174. writefln!"Длина s: sqrt(%s) = %s\nорт AH: %s"(s.squareLength, s.length, s / s.length);
  175. auto AH = s / s.length * p;
  176. writefln!"Ответ: AH = %s"(AH);
  177. /* Task 8 */
  178. writeln("\nЗадание 8.");
  179. auto eq1 = Equation(AB.x, AD.x, AA1.x, AH.x),
  180. eq2 = Equation(AB.y, AD.y, AA1.y, AH.y),
  181. eq3 = Equation(AB.z, AD.z, AA1.z, AH.z);
  182. auto sys = SystemOfEquations(eq1, eq2, eq3);
  183. auto tmp1 = sys.solve;
  184. writefln!"AH = %s, AB = %s, AD = %s, AA1 = %s"(AH, AB, AD, AA1);
  185. writefln!"Ответ: AH = %s * AB + %s * AD + %s * AA1"(tmp1.x, tmp1.y, tmp1.z);
  186. /* Task 9 */
  187. writeln("\nЗадание 9.");
  188. auto var = scalarProduct(AH, AA1);
  189. writefln!"Ответ: %s/sqrt(%s) = %s"(tmp, AA1.squareLength, tmp/AA1.length);
  190. return 0;
  191. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement