Fixed new Tensor to be everything (Scalar, Vector, Matrix and above)
This commit is contained in:
parent
934a40fe1a
commit
f37a196b15
59
src/Base.zig
59
src/Base.zig
@ -3,34 +3,39 @@ const std = @import("std");
|
||||
// Adjust these imports to match your actual file names
|
||||
const Dimensions = @import("Dimensions.zig");
|
||||
const Scales = @import("Scales.zig");
|
||||
const Scalar = @import("Quantity.zig").Scalar;
|
||||
const Tensor = @import("Tensor.zig").Tensor;
|
||||
|
||||
fn PhysicalConstant(comptime d: Dimensions.ArgOpts, comptime val: f64, comptime s: Scales.ArgOpts) type {
|
||||
return struct {
|
||||
const dims = Dimensions.init(d);
|
||||
const scales = Scales.init(s);
|
||||
pub const dims = Dimensions.init(d);
|
||||
pub const scales = Scales.init(s);
|
||||
|
||||
/// Instantiates the constant into a specific numeric type.
|
||||
pub fn Of(comptime T: type) Scalar(T, d, s) {
|
||||
return .{ .data = @splat(@as(T, @floatCast(val))) };
|
||||
pub fn Of(comptime T: type) Tensor(T, d, s, &.{1}) {
|
||||
const casted_val: T = switch (@typeInfo(T)) {
|
||||
.float => @floatCast(val),
|
||||
.int => @intFromFloat(val),
|
||||
else => @compileError("Unsupported type for PhysicalConstant"),
|
||||
};
|
||||
return Tensor(T, d, s, &.{1}).splat(casted_val);
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
fn BaseScalar(comptime d: Dimensions.ArgOpts) type {
|
||||
return struct {
|
||||
const dims = Dimensions.init(d);
|
||||
pub const dims = Dimensions.init(d);
|
||||
|
||||
/// Creates a Scalar of this dimension using default scales.
|
||||
/// Example: const V = Quantities.Velocity.Base(f32);
|
||||
/// Example: const V = Quantities.Velocity.Of(f32);
|
||||
pub fn Of(comptime T: type) type {
|
||||
return Scalar(T, d, .{});
|
||||
return Tensor(T, d, .{}, &.{1});
|
||||
}
|
||||
|
||||
/// Creates a Scalar of this dimension using custom scales.
|
||||
/// Example: const Kmh = Quantities.Velocity.Scaled(f32, Scales.init(.{ .L = .k, .T = .hour }));
|
||||
/// Example: const Kmh = Quantities.Velocity.Scaled(f32, .{ .L = .k, .T = .hour });
|
||||
pub fn Scaled(comptime T: type, comptime s: Scales.ArgOpts) type {
|
||||
return Scalar(T, d, s);
|
||||
return Tensor(T, d, s, &.{1});
|
||||
}
|
||||
};
|
||||
}
|
||||
@ -107,7 +112,7 @@ pub const ElectricCapacitance = BaseScalar(.{ .T = 4, .L = -2, .M = -1, .I = 2 }
|
||||
pub const ElectricImpedance = ElectricResistance;
|
||||
pub const MagneticFlux = BaseScalar(.{ .M = 1, .L = 2, .T = -2, .I = -1 });
|
||||
pub const MagneticDensity = BaseScalar(.{ .M = 1, .T = -2, .I = -1 });
|
||||
pub const MagneticStrength = BaseScalar(.{ .L = -1, .I = 1 }); // Fixed typo from MagneticStrengh
|
||||
pub const MagneticStrength = BaseScalar(.{ .L = -1, .I = 1 });
|
||||
pub const MagneticMoment = BaseScalar(.{ .L = 2, .I = 1 });
|
||||
|
||||
// ==========================================
|
||||
@ -140,7 +145,7 @@ pub const ThermalHeat = Energy;
|
||||
pub const ThermalWork = Energy;
|
||||
pub const ThermalCapacity = BaseScalar(.{ .M = 1, .L = 2, .T = -2, .Tr = -1 });
|
||||
pub const ThermalCapacityPerMass = BaseScalar(.{ .L = 2, .T = -2, .Tr = -1 });
|
||||
pub const ThermalFluxDensity = BaseScalar(.{ .M = 1, .T = -3 }); // Fixed typo from ThermalluxDensity
|
||||
pub const ThermalFluxDensity = BaseScalar(.{ .M = 1, .T = -3 });
|
||||
pub const ThermalConductance = BaseScalar(.{ .M = 1, .L = 2, .T = -3, .Tr = -1 });
|
||||
pub const ThermalConductivity = BaseScalar(.{ .M = 1, .L = 1, .T = -3, .Tr = -1 });
|
||||
pub const ThermalResistance = BaseScalar(.{ .M = -1, .L = -2, .T = 3, .Tr = 1 });
|
||||
@ -152,20 +157,24 @@ pub const ThermalEntropy = BaseScalar(.{ .M = 1, .L = 2, .T = -2, .Tr = -1 });
|
||||
// ==========================================
|
||||
pub const Frequency = BaseScalar(.{ .T = -1 });
|
||||
pub const Viscosity = BaseScalar(.{ .M = 1, .L = -1, .T = -1 });
|
||||
pub const SurfaceTension = BaseScalar(.{ .M = 1, .T = -2 }); // Corrected from MT-2a
|
||||
pub const SurfaceTension = BaseScalar(.{ .M = 1, .T = -2 });
|
||||
|
||||
// ==========================================
|
||||
// Tests
|
||||
// ==========================================
|
||||
|
||||
test "BaseQuantities - Core dimensions instantiation" {
|
||||
// Basic types via generic wrappers
|
||||
const M = Meter.Of(f32);
|
||||
const distance = M.splat(100);
|
||||
try std.testing.expectEqual(100.0, distance.value());
|
||||
try std.testing.expectEqual(100.0, distance.data[0]);
|
||||
try std.testing.expectEqual(1, M.dims.get(.L));
|
||||
try std.testing.expectEqual(0, M.dims.get(.T));
|
||||
|
||||
// Test specific scale variants
|
||||
const Kmh = Speed.Scaled(f32, .{ .L = .k, .T = .hour });
|
||||
const speed = Kmh.splat(120);
|
||||
try std.testing.expectEqual(120.0, speed.value());
|
||||
try std.testing.expectEqual(120.0, speed.data[0]);
|
||||
try std.testing.expectEqual(.k, @TypeOf(speed).scales.get(.L));
|
||||
try std.testing.expectEqual(.hour, @TypeOf(speed).scales.get(.T));
|
||||
}
|
||||
@ -176,12 +185,12 @@ test "BaseQuantities - Kinematics equations" {
|
||||
|
||||
// Velocity = Distance / Time
|
||||
const v = d.div(t);
|
||||
try std.testing.expectEqual(25.0, v.value());
|
||||
try std.testing.expectEqual(25.0, v.data[0]);
|
||||
try std.testing.expect(Speed.dims.eql(@TypeOf(v).dims));
|
||||
|
||||
// Acceleration = Velocity / Time
|
||||
const a = v.div(t);
|
||||
try std.testing.expectEqual(12.5, a.value());
|
||||
try std.testing.expectEqual(12.5, a.data[0]);
|
||||
try std.testing.expect(Acceleration.dims.eql(@TypeOf(a).dims));
|
||||
}
|
||||
|
||||
@ -193,13 +202,13 @@ test "BaseQuantities - Dynamics (Force and Work)" {
|
||||
|
||||
// Force = mass * acceleration
|
||||
const f = m.mul(a);
|
||||
try std.testing.expectEqual(98, f.value());
|
||||
try std.testing.expectEqual(98, f.data[0]);
|
||||
try std.testing.expect(Force.dims.eql(@TypeOf(f).dims));
|
||||
|
||||
// Energy (Work) = Force * distance
|
||||
const distance = Meter.Of(f32).splat(5.0);
|
||||
const energy = f.mul(distance);
|
||||
try std.testing.expectEqual(490, energy.value());
|
||||
try std.testing.expectEqual(490, energy.data[0]);
|
||||
try std.testing.expect(Energy.dims.eql(@TypeOf(energy).dims));
|
||||
}
|
||||
|
||||
@ -209,26 +218,26 @@ test "BaseQuantities - Electric combinations" {
|
||||
|
||||
// Charge = Current * time
|
||||
const charge = current.mul(time);
|
||||
try std.testing.expectEqual(6.0, charge.value());
|
||||
try std.testing.expectEqual(6.0, charge.data[0]);
|
||||
try std.testing.expect(ElectricCharge.dims.eql(@TypeOf(charge).dims));
|
||||
}
|
||||
|
||||
test "Constants - Initialization and dimension checks" {
|
||||
// Speed of Light
|
||||
const c = Constants.SpeedOfLight.Of(f64);
|
||||
try std.testing.expectEqual(299792458.0, c.value());
|
||||
try std.testing.expectEqual(299792458.0, c.data[0]);
|
||||
try std.testing.expectEqual(1, @TypeOf(c).dims.get(.L));
|
||||
try std.testing.expectEqual(-1, @TypeOf(c).dims.get(.T));
|
||||
|
||||
// Electron Mass (verifying scale as well)
|
||||
const me = Constants.ElectronMass.Of(f64);
|
||||
try std.testing.expectEqual(9.1093837139e-31, me.value());
|
||||
try std.testing.expectEqual(9.1093837139e-31, me.data[0]);
|
||||
try std.testing.expectEqual(1, @TypeOf(me).dims.get(.M));
|
||||
try std.testing.expectEqual(.k, @TypeOf(me).scales.get(.M)); // Should be scaled to kg
|
||||
|
||||
// Boltzmann Constant (Complex derived dimensions)
|
||||
const kb = Constants.Boltzmann.Of(f64);
|
||||
try std.testing.expectEqual(1.380649e-23, kb.value());
|
||||
try std.testing.expectEqual(1.380649e-23, kb.data[0]);
|
||||
try std.testing.expectEqual(1, @TypeOf(kb).dims.get(.M));
|
||||
try std.testing.expectEqual(2, @TypeOf(kb).dims.get(.L));
|
||||
try std.testing.expectEqual(-2, @TypeOf(kb).dims.get(.T));
|
||||
@ -237,7 +246,7 @@ test "Constants - Initialization and dimension checks" {
|
||||
|
||||
// Vacuum Permittivity
|
||||
const eps0 = Constants.VacuumPermittivity.Of(f64);
|
||||
try std.testing.expectEqual(8.8541878188e-12, eps0.value());
|
||||
try std.testing.expectEqual(8.8541878188e-12, eps0.data[0]);
|
||||
try std.testing.expectEqual(-1, @TypeOf(eps0).dims.get(.M));
|
||||
try std.testing.expectEqual(-3, @TypeOf(eps0).dims.get(.L));
|
||||
try std.testing.expectEqual(4, @TypeOf(eps0).dims.get(.T));
|
||||
@ -245,7 +254,7 @@ test "Constants - Initialization and dimension checks" {
|
||||
|
||||
// Fine Structure Constant (Dimensionless)
|
||||
const alpha = Constants.FineStructure.Of(f64);
|
||||
try std.testing.expectEqual(0.0072973525643, alpha.value());
|
||||
try std.testing.expectEqual(0.0072973525643, alpha.data[0]);
|
||||
try std.testing.expectEqual(0, @TypeOf(alpha).dims.get(.M));
|
||||
try std.testing.expectEqual(0, @TypeOf(alpha).dims.get(.L));
|
||||
}
|
||||
|
||||
@ -1,5 +1,4 @@
|
||||
const std = @import("std");
|
||||
const hlp = @import("helper.zig");
|
||||
const Dimensions = @import("Dimensions.zig");
|
||||
const Dimension = @import("Dimensions.zig").Dimension;
|
||||
|
||||
@ -99,7 +98,7 @@ data: std.EnumArray(Dimension, UnitScale),
|
||||
pub fn init(comptime init_val: ArgOpts) Self {
|
||||
comptime var s = Self{ .data = std.EnumArray(Dimension, UnitScale).initFill(.none) };
|
||||
inline for (std.meta.fields(@TypeOf(init_val))) |f| {
|
||||
if (comptime hlp.isInt(@TypeOf(@field(init_val, f.name))))
|
||||
if (comptime @typeInfo(@TypeOf(@field(init_val, f.name))) == .comptime_int)
|
||||
s.data.set(@field(Dimension, f.name), @enumFromInt(@field(init_val, f.name)))
|
||||
else
|
||||
s.data.set(@field(Dimension, f.name), @field(init_val, f.name));
|
||||
|
||||
File diff suppressed because it is too large
Load Diff
@ -1,97 +0,0 @@
|
||||
const std = @import("std");
|
||||
|
||||
pub fn isInt(comptime T: type) bool {
|
||||
return @typeInfo(T) == .int or @typeInfo(T) == .comptime_int;
|
||||
}
|
||||
|
||||
pub fn printSuperscript(writer: *std.Io.Writer, n: i32) !void {
|
||||
if (n == 0) return;
|
||||
var val = n;
|
||||
if (val < 0) {
|
||||
try writer.writeAll("\u{207B}");
|
||||
val = -val;
|
||||
}
|
||||
var buf: [12]u8 = undefined;
|
||||
const str = std.fmt.bufPrint(&buf, "{d}", .{val}) catch return;
|
||||
for (str) |c| {
|
||||
const s = switch (c) {
|
||||
'0' => "\u{2070}",
|
||||
'1' => "\u{00B9}",
|
||||
'2' => "\u{00B2}",
|
||||
'3' => "\u{00B3}",
|
||||
'4' => "\u{2074}",
|
||||
'5' => "\u{2075}",
|
||||
'6' => "\u{2076}",
|
||||
'7' => "\u{2077}",
|
||||
'8' => "\u{2078}",
|
||||
'9' => "\u{2079}",
|
||||
else => unreachable,
|
||||
};
|
||||
try writer.writeAll(s);
|
||||
}
|
||||
}
|
||||
|
||||
const Scales = @import("Scales.zig");
|
||||
const Dimensions = @import("Dimensions.zig");
|
||||
const Dimension = @import("Dimensions.zig").Dimension;
|
||||
|
||||
pub fn finerScales(comptime T1: type, comptime T2: type) Scales {
|
||||
const d1: Dimensions = T1.dims;
|
||||
const d2: Dimensions = T2.dims;
|
||||
const s1: Scales = T1.scales;
|
||||
const s2: Scales = T2.scales;
|
||||
comptime var out = Scales.initFill(.none);
|
||||
inline for (std.enums.values(Dimension)) |dim| {
|
||||
const scale1 = comptime s1.get(dim);
|
||||
const scale2 = comptime s2.get(dim);
|
||||
out.set(dim, if (comptime d1.get(dim) == 0 and d2.get(dim) == 0)
|
||||
.none
|
||||
else if (comptime d1.get(dim) == 0)
|
||||
scale2
|
||||
else if (comptime d2.get(dim) == 0)
|
||||
scale1
|
||||
else if (comptime scale1.getFactor() > scale2.getFactor())
|
||||
scale2
|
||||
else
|
||||
scale1);
|
||||
}
|
||||
comptime return out;
|
||||
}
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
// RHS normalisation helpers
|
||||
// ---------------------------------------------------------------------------
|
||||
|
||||
const Quantity = @import("Quantity.zig").Quantity;
|
||||
|
||||
/// Returns true if `T` is a `Scalar_` type (has `dims`, `scales`, and `value`).
|
||||
pub fn isScalarType(comptime T: type) bool {
|
||||
return @typeInfo(T) == .@"struct" and
|
||||
@hasDecl(T, "ISQUANTITY") and
|
||||
@field(T, "ISQUANTITY");
|
||||
}
|
||||
|
||||
/// Resolve the Scalar type that `rhs` will be treated as.
|
||||
///
|
||||
/// Accepted rhs types:
|
||||
/// - Any `Scalar_` type → returned as-is
|
||||
/// - `comptime_int` / `comptime_float` → dimensionless `Scalar_(BaseT, {}, {})`
|
||||
/// - `BaseT` (the scalar's value type) → dimensionless `Scalar_(BaseT, {}, {})`
|
||||
///
|
||||
/// Everything else is a compile error, including other int/float types.
|
||||
pub fn rhsQuantityType(comptime ValueType: type, N: usize, comptime RhsT: type) type {
|
||||
if (comptime isScalarType(RhsT)) return RhsT;
|
||||
if (comptime RhsT == comptime_int or RhsT == comptime_float or RhsT == ValueType)
|
||||
return Quantity(ValueType, N, .{}, .{});
|
||||
@compileError(
|
||||
"rhs must be a Scalar, " ++ @typeName(ValueType) ++
|
||||
", comptime_int, or comptime_float; got " ++ @typeName(RhsT),
|
||||
);
|
||||
}
|
||||
|
||||
/// Convert `rhs` to its normalised Scalar form (see `rhsScalarType`).
|
||||
pub inline fn toRhsQuantity(comptime BaseT: type, N: usize, rhs: anytype) rhsQuantityType(BaseT, N, @TypeOf(rhs)) {
|
||||
if (comptime isScalarType(@TypeOf(rhs))) return rhs;
|
||||
const DimLess = Quantity(BaseT, N, .{}, .{});
|
||||
return DimLess{ .data = @splat(@as(BaseT, rhs)) };
|
||||
}
|
||||
@ -1,15 +1,13 @@
|
||||
const std = @import("std");
|
||||
|
||||
pub const Vector = @import("Quantity.zig").Vector;
|
||||
pub const Scalar = @import("Quantity.zig").Scalar;
|
||||
pub const Tensor = @import("Tensor.zig").Tensor;
|
||||
pub const Dimensions = @import("Dimensions.zig");
|
||||
pub const Scales = @import("Scales.zig");
|
||||
pub const Base = @import("Base.zig");
|
||||
|
||||
test {
|
||||
_ = @import("Quantity.zig");
|
||||
_ = @import("Tensor.zig");
|
||||
_ = @import("Dimensions.zig");
|
||||
_ = @import("Scales.zig");
|
||||
_ = @import("Base.zig");
|
||||
_ = @import("helper.zig");
|
||||
}
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user