- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我已经实现了Point3D
结构:
use std::ops;
#[derive(Debug, PartialEq)]
pub struct Point3D {
pub x: f32,
pub y: f32,
pub z: f32,
}
impl ops::Add<&Point3D> for &Point3D {
type Output = Point3D;
fn add(self, rhs: &Point3D) -> Point3D {
Point3D {
x: self.x + rhs.x,
y: self.y + rhs.y,
z: self.z + rhs.z,
}
}
}
impl ops::Sub<&Point3D> for &Point3D {
type Output = Point3D;
fn sub(self, rhs: &Point3D) -> Point3D {
Point3D {
x: self.x - rhs.x,
y: self.y - rhs.y,
z: self.z - rhs.z,
}
}
}
impl ops::Mul<&Point3D> for &Point3D {
type Output = f32;
fn mul(self, rhs: &Point3D) -> f32 {
self.x * rhs.x + self.y * rhs.y + self.z * rhs.z
}
}
//Scalar impl of ops::Mul here
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn addition_point_3D() {
let point1 = Point3D {
x: 1.0,
y: 2.0,
z: 3.0,
};
let point2 = Point3D {
x: 4.0,
y: 5.0,
z: 6.0,
};
let result = &point1 + &point2;
assert_eq!(
result,
Point3D {
x: 5.0,
y: 7.0,
z: 9.0
},
"Testing Addition with {:?} and {:?}",
point1,
point2
);
}
#[test]
fn subtraction_point_3D() {
let point1 = Point3D {
x: 1.0,
y: 2.0,
z: 3.0,
};
let point2 = Point3D {
x: 4.0,
y: 5.0,
z: 6.0,
};
let result = &point1 - &point2;
assert_eq!(
result,
Point3D {
x: -3.0,
y: -3.0,
z: -3.0
},
"Testing Subtraction with {:?} and {:?}",
point1,
point2
);
}
#[test]
fn point3D_point3D_multiplication() {
let point1 = Point3D {
x: 1.0,
y: 2.0,
z: 3.0,
};
let point2 = Point3D {
x: 4.0,
y: 5.0,
z: 6.0,
};
let result = &point1 * &point2;
assert_eq!(
result, 32.0,
"Testing Multiplication with {:?} and {:?}",
point1, point2
);
}
/*
#[test]
fn point3D_scalar_multiplication() {
let point1 = Point3D { x: 1.0, y: 2.0, z: 3.0};
let scalar = 3.5;
let result = &point1 * &scalar;
assert_eq!(result, Point3D { x: 3.5, y: 7.0, z: 10.5 }, "Testing Multiplication with {:?} and {:?}", point1, scalar);
}
*/
}
我想在乘法特征中使用泛型,以便如果我将其传递给另一个
Point3D
类,它将实现点积,但是如果我将其传递给基本数字类型(整数,f32,无符号整数,f64),它将乘以
x
,
y
和
z
的标量值。我将如何做?
最佳答案
你的意思是那样吗?
impl ops::Mul<f32> for &Point3D {
type Output = Point3D;
fn mul(self, rhs: f32) -> Point3D {
Point3D {
x: self.x * rhs,
y: self.y * rhs,
z: self.z * rhs
}
}
}
这将允许您执行以下操作:
let point = Point3D { x: 1.0, y: 2.0, z: 3.0};
let result = &point * 4.0;
关于rust - 如何在一个结构上实现ops::Mul,以便它可以与数字类型以及另一个结构一起使用?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63255094/
我正在KERAS CNN中工作,以检测视网膜图像中的糖尿病性视网膜病变。但是,当我尝试使用此代码预处理图像..: def estimate_radius(img): mx = img[img.
为什么这样实现: T& T::operator+=(const T&) { // ... implementation ... return *this; } T operator+(cons
这是我的图像混合代码,但 cv2.addweighted() 函数有问题: import cv2 import numpy as np img1 = cv2.imread('1.png') img2
我正在设置一个搜索API,就是搜索; id、type、originCity、destinationCity、departmentDate、reason、accommodation、approvalSt
这两个特征(std::ops::Add,core::ops::Add)提供相同的功能,并且它们都使用相同的示例(都使用std::ops::Add)。他们的实现者集有所不同。 应该默认使用std::op
我要提出条件 (A 和 B)和(C 或 D 或 E 或 F) 当我尝试做的时候 where: { [Op.and]: [{ A, B, [Op.or]: [{
我对 Sequelize 有这个问题: 我正在尝试进行 3 参数研究,如果其中一个参数为空,则不应考虑该参数。 const users = await User.findAll({ where:
Herb Sutter 的 Guru of the Week #4, "Class Mechanics" ,教导重载运算符的“a op b”形式应该根据“a op= b”形式实现(参见解决方案中的第
基准代码: func BenchmarkSth(b *testing.B) { var x []int b.ResetTimer() for i := 0; i < b.N;
我在使用 sequelize 获取正确的查询时遇到问题。 我有一个表示条目 id 的数组,可以这样说 - userVacationsIds = [1,2,3] 我做了这样的第一个查询
这两个特征(std::ops::Add,core::ops::Add)提供相同的功能,并且它们都使用相同的示例(都利用 std::ops::Add)。他们的一组实现者有些不同。 是否应该默认使用 st
为什么 std::numeric 算法似乎更喜欢 op 而不是 op=?例如,这里是 std::accumulate 在 LLVM 中的实现: template inline _LIBCPP_INL
这是基准测试的代码片段: // bench_test.go package main import ( "testing" ) func BenchmarkHello(b *testing.B
我使用 Sequelize 来查询我的数据库。 我在查询中使用 Op.and 和 Op.ne: const Op = require('sequelize'); let subLevels = awa
我有一个关于链表和模板的作业,其中有带有奇怪参数的函数,这些参数是强制性的。我找不到关于它的在线文档,感谢任何提供的 Material 。 我曾尝试为 op 分配另一个地址,然后它编译得很好,但我无法
cppreference说: the behavior of every builtin compound-assignment expression E1 op= E2 (where E1 is a
当我使用 go test -v -bench=. -benchmem 运行基准测试时,我看到以下结果。 f1 10000 120860 ns/op 2433 B/o
假设我有一个sympy 表达式 e1 op e2 哪里op是任何算术比较运算符,例如 , >> from sympy import symbols, Piecewise, ccode >>> x, y
在我的玩具包中,我定义了 %+%运算符作为 paste0() 的别名.试图减少与其他包的干扰,我通过以下方式实现: `%+%` <- function(...) UseMethod("%+%") `%
我正在尝试遵循 Scott Meyers 在 More Effective C++ 的第 22 项中的建议:“考虑使用 op= 而不是独立的 op”。他建议我们可以为 operator+ 创建一个模板
我是一名优秀的程序员,十分优秀!