Ich muss Ilgpus xorShift128plus in Python übersetzen.[code]using Microsoft.VisualStudio.TestTools.UnitTesting; using ILGPU.Algorithms.Random; using System;
namespace MyTests { [TestClass] public class XorShift128PlusTests { [TestMethod] public void TestInitialization() { var rng = new XorShift128Plus(123456789UL, 987654321UL); Assert.AreEqual(123456789UL, rng.State0); Assert.AreEqual(987654321UL, rng.State1); }
[TestMethod] public void TestNextULong() { var rng = new XorShift128Plus(123456789UL, 987654321UL); var actual = rng.NextULong(); Assert.AreEqual(1035635210406904UL, actual); }
[TestMethod] public void TestNextUInt() { var rng = new XorShift128Plus(123456789UL, 987654321UL); var actual = rng.NextUInt(); Assert.AreEqual(2631329311, actual); }
[TestMethod] public void TestNext() { var rng = new XorShift128Plus(123456789UL, 987654321UL); var actual = rng.Next(); Assert.AreEqual(483845663, actual); }
[TestMethod] public void TestNextLong() { var rng = new XorShift128Plus(123456789UL, 987654321UL); var actual = rng.NextLong(); Assert.AreEqual(1035635210406904L, actual); }
[TestMethod] public void TestNextFloat() { var rng = new XorShift128Plus(123456789UL, 987654321UL); var value = rng.NextFloat(); var actual = value.Trim(6); var expected = 0.0001122839f.Trim(6);
float tolerance = 1e-7f; // Small allowable error due to float limitations Assert.IsTrue(Math.Abs(expected - actual) < tolerance, $"Expected {expected}, but got {actual}"); }
[TestMethod] public void TestNextDouble() { var rng = new XorShift128Plus(123456789UL, 987654321UL); var actual = rng.NextDouble(); Assert.AreEqual(0.000112283794502565.Trim(15), actual.Trim(15)); }
[TestMethod] public void TestShiftPeriod() { var rng = new XorShift128Plus(123456789UL, 987654321UL); var oldState0 = rng.State0; var oldState1 = rng.State1; rng.ShiftPeriod(10); Assert.IsTrue(rng.State0 != oldState0 || rng.State1 != oldState1); }
[TestMethod] public void TestNextProvider() { var rng = new XorShift128Plus(123456789UL, 987654321UL); var newRng = rng.NextProvider(); Assert.IsInstanceOfType(newRng, typeof(XorShift128Plus)); Assert.IsTrue(newRng.State0 != rng.State0 || newRng.State1 != rng.State1); }
[TestMethod] public void TestCreateProvider() { var rng = new XorShift128Plus(123456789UL, 987654321UL); var newRng = rng.CreateProvider(new Random(42)); Assert.IsInstanceOfType(newRng, typeof(XorShift128Plus)); Assert.IsTrue(newRng.State0 > 0UL && newRng.State1 > 0UL); }
[TestMethod] public void TestEquality() { var rng1 = new XorShift128Plus(123456789UL, 987654321UL); var rng2 = new XorShift128Plus(123456789UL, 987654321UL); var rng3 = new XorShift128Plus(111111111UL, 222222222UL); Assert.AreEqual(rng1, rng2); Assert.AreNotEqual(rng1, rng3); }
[TestMethod] public void TestHash() { var rng1 = new XorShift128Plus(123456789UL, 987654321UL); var rng2 = new XorShift128Plus(123456789UL, 987654321UL); Assert.AreEqual(rng1.GetHashCode(), rng2.GetHashCode()); }
[TestMethod] public void TestToString() { var rng = new XorShift128Plus(123456789UL, 987654321UL); Assert.AreEqual("[123456789, 987654321]", rng.ToString()); } } }
public static class DoubleTrimmer { public static double Trim(this double value, int decimal_places) { return Math.Round(value, decimal_places); }
public static double Clamp(this double value, double min, double max) { if (value < min) return min; if (value > max) return max; return value; }
public static float Clamp(this float value, float min, float max) { if (value < min) return min; if (value > max) return max; return value; }
public static decimal Trim(this decimal value, int decimal_places) { return Math.Round(value, decimal_places); }
public static decimal Clamp(this decimal value, decimal min, decimal max) { if (value < min) return min; if (value > max) return max; return value; } } < /code> Dann habe ich die C# -Klasse in eine Python-Klasse übersetzt: < /p> import random import struct
class XorShift128Plus: """ Implements a simple and fast xor-shift RNG. """
def __init__(self, state0: int, state1: int): if state0 == 0 and state1 == 0: raise ValueError("State must not be zero") self.state0 = state0 self.state1 = state1
@staticmethod def create(seed_random: random.Random = None): if seed_random is None: seed_random = random.Random()
def next_float(self) -> float: return self.next_long() * (1.0 / (1 float: return self.next_long() * (1.0 / (1 Dann habe ich die C# -Version des Einheitstests in ein PyTest-Modul übersetzt.import unittest from XorShift128Plus import XorShift128Plus
Ich habe eine Datenpipeline, in der ich einen gesamten Polars DataFrame vom Koreanischen ins Englische übersetzen muss. Derzeit habe ich einen funktionierenden, aber manuellen Prozess:
Ich habe gerade etwas über Expression erfahren. Als Bibliotheksautor hat mich das wirklich fasziniert; Meine Bibliotheken nutzen in großem Umfang Quellgeneratoren, um sowohl Hochleistungscode als...
Übersetzen Sie auf Moodle mit „get_string“ für eine bestimmte Sprache. Ich versuche, eine Übersetzung meines Plugin-Blocks auf Moodle in einer bestimmten Sprache zu erhalten. aber ich kann nicht....
In HTML gibt es White -Raum über und unterhalb von Texten , und es ist offensichtlich, wenn der Text groß ist. Wenn nicht, wie berechnet man den vertikalen Raum zwischen Texten in HTML, um genau den...