Привет! Переведи это в Python using MathNet.Numerics.LinearAlgebra;…
- User
Привет! Переведи это в Python
using MathNet.Numerics.LinearAlgebra;
Console.WriteLine("Для уравнения вида ax+by+c=z"); Console.WriteLine("Введите коэффициент a:"); var aCoef = double.Parse(Console.ReadLine()); Console.WriteLine("Введите коэффициент b:"); var bCoef = double.Parse(Console.ReadLine()); Console.WriteLine("Введите коэффициент c:"); var zCoef = double.Parse(Console.ReadLine());
// Создание точек int dotsCount = 4; Random rnd = new Random(); double[,] dots = new double[3, dotsCount]; for (int i = 0; i < dotsCount; i++) { dots[0, i] = rnd.NextDouble(); dots[1, i] = rnd.NextDouble(); dots[2, i] = aCoef * dots[0, i] + bCoef * dots[1, i] + zCoef; }
// Наименьшие квадраты double x = 0, y = 0, z = 0, xx = 0, yy = 0, xy = 0, xz = 0, yz = 0; for (int i = 0; i < dotsCount; i++) { x += dots[0, i]; y += dots[1, i]; z += dots[2, i]; xx += dots[0, i] * dots[0, i]; yy += dots[1, i] * dots[1, i]; xy += dots[0, i] * dots[1, i]; xz += dots[0, i] * dots[2, i]; yz += dots[1, i] * dots[2, i]; }
// матрица коэффициентов double[,] a = { { xx, xy, x }, { xy, yy, y }, { x, y, dotsCount } };
// вектор правых частей double[] b = { xz, yz, z };
// Решение системы var aM = Matrix.Build.DenseOfArray(a); var bM = Vector.Build.Dense(b); var res = aM.Solve(bM).ToArray(); res = res.Select(x => Math.Round(x, 5)).ToArray(); // Округление до 5го знака
// Вывод Console.WriteLine("Исходное уравнение:"); Console.WriteLine("+{bCoef}y " : "+{zCoef}" : "{res[0]}x " + (res[1] > 0 ? "{res[1]}y ") + (res[2] > 0 ? "{res[2]}") + " = z"); Console.ReadKey();
- Assistant
from math import isqrt import random
print("Для уравнения вида ax+by+c=z") aCoef = float(input("Введите коэффициент a:")) bCoef = float(input("Введите коэффициент b:")) zCoef = float(input("Введите коэффициент c:"))
Создание точек
dotsCount = 4 dots = [[0]*dotsCount for _ in range(3)] for i in range(dotsCount): dots[0][i] = random.random() dots[1][i] = random.random() dots[2][i] = aCoef * dots[0][i] + bCoef * dots[1][i] + zCoef
Наименьшие квадраты
x = y = z = xx = yy = xy = xz = yz = 0 for i in range(dotsCount): x += dots[0][i] y += dots[1][i] z += dots[2][i] xx += dots[0][i] * dots[0][i] yy += dots[1][i] * dots[1][i] xy += dots[0][i] * dots[1][i] xz += dots[0][i] * dots[2][i] yz += dots[1][i] * dots[2][i]
матрица коэффициентов
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.