Skip to content

Commit

Permalink
Replace soon-to-be keyword gen
Browse files Browse the repository at this point in the history
  • Loading branch information
JoeyBF committed Oct 30, 2024
1 parent 145a87c commit 3413606
Show file tree
Hide file tree
Showing 22 changed files with 170 additions and 179 deletions.
6 changes: 3 additions & 3 deletions ext/crates/algebra/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -60,11 +60,11 @@ pub(crate) fn module_gens_from_json(
gen_to_idx.insert(name.clone(), (degree, graded_dimension[degree]));
graded_dimension[degree] += 1;
}
(graded_dimension, gen_names, move |r#gen| {
(graded_dimension, gen_names, move |g| {
gen_to_idx
.get(r#gen)
.get(g)
.copied()
.ok_or_else(|| anyhow!("Invalid generator: {gen}"))
.ok_or_else(|| anyhow!("Invalid generator: {g}"))
})
}

Expand Down
18 changes: 9 additions & 9 deletions ext/crates/algebra/src/module/finite_dimensional_module.rs
Original file line number Diff line number Diff line change
Expand Up @@ -480,8 +480,8 @@ impl<A: GeneratedAlgebra> FiniteDimensionalModule<A> {
json["type"] = Value::from("finite dimensional module");
json["gens"] = json!({});
for (i, deg_i_gens) in self.gen_names.iter_enum() {
for gen in deg_i_gens {
json["gens"][gen] = Value::from(i);
for g in deg_i_gens {
json["gens"][g] = Value::from(i);
}
}

Expand All @@ -500,15 +500,15 @@ impl<A: GeneratedAlgebra> FiniteDimensionalModule<A> {
.split_once(" = ")
.ok_or_else(|| anyhow!("Invalid action: {entry}"))?;

let (action, gen) = lhs
let (action, g) = lhs
.rsplit_once(' ')
.ok_or_else(|| anyhow!("Invalid action: {entry}"))?;

let (op_deg, op_idx) = algebra
.basis_element_from_string(action)
.ok_or_else(|| anyhow!("Invalid algebra element: {action}"))?;

let (input_deg, input_idx) = gen_to_idx(gen.trim())?;
let (input_deg, input_idx) = gen_to_idx(g.trim())?;

let row = self.action_mut(op_deg, op_idx, input_deg, input_idx);

Expand All @@ -521,18 +521,18 @@ impl<A: GeneratedAlgebra> FiniteDimensionalModule<A> {
}

for item in rhs.split(" + ") {
let (coef, gen) = match item.split_once(' ') {
Some((coef, gen)) => (
let (coef, g) = match item.split_once(' ') {
Some((coef, g)) => (
str::parse(coef)
.map_err(|_| anyhow!("Invalid item on right-hand side: {item}"))?,
gen,
g,
),
None => (1, item),
};
let (deg, idx) = gen_to_idx(gen.trim())?;
let (deg, idx) = gen_to_idx(g.trim())?;
if deg != input_deg + op_deg {
return Err(anyhow!(
"Degree of {gen} is {deg} but degree of LHS is {}",
"Degree of {g} is {deg} but degree of LHS is {}",
input_deg + op_deg
));
}
Expand Down
4 changes: 2 additions & 2 deletions ext/crates/algebra/src/module/finitely_presented_module.rs
Original file line number Diff line number Diff line change
Expand Up @@ -130,11 +130,11 @@ impl<A: Algebra> FinitelyPresentedModule<A> {
let (term, coef) = opt(digits)(term).unwrap();
let coef: u32 = coef.unwrap_or(1);

let (op, gen) = term.rsplit_once(' ').unwrap_or(("1", term));
let (op, g) = term.rsplit_once(' ').unwrap_or(("1", term));
let (op_deg, op_idx) = algebra
.basis_element_from_string(op)
.ok_or_else(|| anyhow!("Invalid term in relation: {term}"))?;
let (gen_deg, gen_idx) = gen_to_deg_idx(gen)?;
let (gen_deg, gen_idx) = gen_to_deg_idx(g)?;

if v.is_empty() {
deg = op_deg + gen_deg;
Expand Down
6 changes: 3 additions & 3 deletions ext/crates/algebra/src/steenrod_evaluator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -64,13 +64,13 @@ impl SteenrodEvaluator {
if items.is_empty() {
return Ok(result);
}
for (op, gen) in parse_module(items)? {
if let Some((deg, vec)) = result.get_mut(&gen) {
for (op, g) in parse_module(items)? {
if let Some((deg, vec)) = result.get_mut(&g) {
let (_, adem_v) = self.evaluate_algebra_node(Some(*deg), op)?;
vec.add(&adem_v, 1);
} else {
let (deg, adem_v) = self.evaluate_algebra_node(None, op)?;
result.insert(gen, (deg, adem_v));
result.insert(g, (deg, adem_v));
}
}
Ok(result)
Expand Down
2 changes: 1 addition & 1 deletion ext/crates/algebra/src/steenrod_parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -207,7 +207,7 @@ fn module_term(i: &str) -> IResult<&str, ModuleNode> {
.unwrap();

match space(module_generator)(i) {
Ok((i, gen)) => return Ok((i, vec![(prefix.unwrap_or(Scalar(1)), gen)])),
Ok((i, g)) => return Ok((i, vec![(prefix.unwrap_or(Scalar(1)), g)])),
Err(nom::Err::Error(_)) => (),
Err(e) => return Err(e),
}
Expand Down
7 changes: 5 additions & 2 deletions ext/crates/fp/benches/criterion.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
use criterion::{criterion_group, criterion_main, BatchSize, Criterion};
use fp::{matrix::Matrix, prime::ValidPrime};
use fp::{
matrix::Matrix,
prime::{Prime, ValidPrime},
};
use rand::Rng;

fn random_matrix(p: ValidPrime, dimension: usize) -> Matrix {
Expand Down Expand Up @@ -33,7 +36,7 @@ fn row_reductions(c: &mut Criterion) {
fn random_vector(p: ValidPrime, dimension: usize) -> Vec<u32> {
let mut result = Vec::with_capacity(dimension);
let mut rng = rand::thread_rng();
result.resize_with(dimension, || rng.gen::<u32>() % p);
result.resize_with(dimension, || rng.gen_range(0..p.as_u32()));
result
}

Expand Down
4 changes: 2 additions & 2 deletions ext/crates/fp/src/matrix/subquotient.rs
Original file line number Diff line number Diff line change
Expand Up @@ -133,10 +133,10 @@ impl Subquotient {
self.dimension = 0;
}

pub fn add_gen(&mut self, gen: FpSlice) {
pub fn add_gen(&mut self, g: FpSlice) {
self.gens.update_then_row_reduce(|gens_matrix| {
let mut new_row = gens_matrix.row_mut(self.dimension);
new_row.assign(gen);
new_row.assign(g);
self.quotient.reduce(new_row);
});
self.dimension = self.gens.dimension();
Expand Down
10 changes: 5 additions & 5 deletions ext/crates/sseq/src/coordinates/element.rs
Original file line number Diff line number Diff line change
Expand Up @@ -55,13 +55,13 @@ impl BidegreeElement {
self.vec
.iter_nonzero()
.map(|(i, v)| {
let gen = BidegreeGenerator::new(self.degree(), i);
let g = BidegreeGenerator::new(self.degree(), i);
let coeff_str = if v != 1 {
format!("{v} ")
} else {
String::new()
};
format!("{coeff_str}x_{gen}")
format!("{coeff_str}x_{g}")
})
.collect::<Vec<_>>()
.join(" + ")
Expand Down Expand Up @@ -91,15 +91,15 @@ impl BidegreeElement {
} else {
String::new()
};
let gen = BidegreeGenerator::s_t(
let g = BidegreeGenerator::s_t(
self.s() - 1,
opgen.generator_degree,
opgen.generator_index,
);
let gen_str = if compact {
format!("x_{gen:#}")
format!("x_{g:#}")
} else {
format!("x_{gen}")
format!("x_{g}")
};
format!("{coeff_str}{op_str}{gen_str}")
})
Expand Down
11 changes: 5 additions & 6 deletions ext/crates/sseq/src/sseq.rs
Original file line number Diff line number Diff line change
Expand Up @@ -280,8 +280,8 @@ impl<P: SseqProfile> Sseq<P> {

if r > self.differentials[x][y].len() || self.page_data[tx][ty][r - 1].is_empty() {
let (prev, cur) = self.page_data[x][y].split_borrow_mut(r - 1, r);
for gen in prev.gens() {
cur.add_gen(gen);
for g in prev.gens() {
cur.add_gen(g);
}
if r - 1 < self.differentials[x][y].len() {
differentials.push(vec![Vec::new(); self.page_data[x][y][r].dimension()]);
Expand All @@ -302,13 +302,12 @@ impl<P: SseqProfile> Sseq<P> {
source_dim + target_dim,
);

for (row, gen) in
for (row, g) in
std::iter::zip(matrix.iter_mut(), self.page_data[x][y][r - 1].gens())
{
row.slice_mut(target_dim, target_dim + source_dim)
.assign(gen);
row.slice_mut(target_dim, target_dim + source_dim).assign(g);

d.evaluate(gen, dvec.as_slice_mut());
d.evaluate(g, dvec.as_slice_mut());
row.slice_mut(0, target_dim).assign(dvec.as_slice());

drawn_differentials
Expand Down
10 changes: 5 additions & 5 deletions ext/examples/bruner.rs
Original file line number Diff line number Diff line change
Expand Up @@ -198,17 +198,17 @@ fn read_bruner_resolution(data_dir: &Path, max_n: i32) -> Result<(u32, FiniteCha
let mut entries: Vec<FpVector> = Vec::new();
let mut cur_degree: i32 = 0;

while let Some((t, gen)) = get_element(algebra, cc.module(s - 1).as_ref(), &mut f)? {
while let Some((t, g)) = get_element(algebra, cc.module(s - 1).as_ref(), &mut f)? {
if t == cur_degree {
entries.push(gen);
entries.push(g);
} else {
m.add_generators(cur_degree, entries.len(), None);
d.add_generators_from_rows(cur_degree, entries);

m.extend_by_zero(t - 1);
d.extend_by_zero(t - 1);

entries = vec![gen];
entries = vec![g];
cur_degree = t;
}
}
Expand Down Expand Up @@ -262,8 +262,8 @@ fn main() {
let matrix = hom.get_map(b.s()).hom_k(b.t());

for (i, row) in matrix.into_iter().enumerate() {
let gen = BidegreeGenerator::new(b, i);
println!("x_{gen:#} = {row:?}");
let g = BidegreeGenerator::new(b, i);
println!("x_{g:#} = {row:?}");
}
}
}
47 changes: 22 additions & 25 deletions ext/examples/define_module.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,8 @@ pub fn get_gens() -> anyhow::Result<BiVec<Vec<String>>> {
if gen_deg.is_none() {
eprintln!("This is the list of generators and degrees:");
for (i, deg_i_gens) in gens.iter_enum() {
for gen in deg_i_gens.iter() {
eprint!("({i}, {gen}) ");
for g in deg_i_gens.iter() {
eprint!("({i}, {g}) ");
}
}
eprintln!();
Expand Down Expand Up @@ -78,8 +78,8 @@ pub fn get_gens() -> anyhow::Result<BiVec<Vec<String>>> {
pub fn gens_to_json(gens: &BiVec<Vec<String>>) -> serde_json::Value {
let mut gens_json = json!({});
for (i, deg_i_gens) in gens.iter_enum() {
for gen in deg_i_gens {
gens_json[gen] = json!(i);
for g in deg_i_gens {
gens_json[g] = json!(i);
}
}
gens_json
Expand All @@ -106,8 +106,8 @@ pub fn interactive_module_define_fdmodule(
let mut module = FDModule::new(Arc::clone(&algebra), String::new(), graded_dim);

for (i, deg_i_gens) in gens.iter_enum() {
for (j, gen) in deg_i_gens.iter().enumerate() {
module.set_basis_element_name(i, j, gen.to_string());
for (j, g) in deg_i_gens.iter().enumerate() {
module.set_basis_element_name(i, j, g.to_string());
}
}

Expand Down Expand Up @@ -139,19 +139,16 @@ pub fn interactive_module_define_fdmodule(
}
for term in expr.split('+') {
let term = term.trim();
let (coef, gen) = match term.split_once(' ') {
Some((coef, gen)) => (str::parse::<u32>(coef)?, gen),
let (coef, g) = match term.split_once(' ') {
Some((coef, g)) => (str::parse::<u32>(coef)?, g),
None => (1, term),
};

if let Some(gen_idx) =
gens[output_deg].iter().position(|d| d == gen)
if let Some(gen_idx) = gens[output_deg].iter().position(|d| d == g)
{
result[gen_idx] += coef;
} else {
return Err(anyhow!(
"No generator {gen} in degree {output_deg}"
));
return Err(anyhow!("No generator {g} in degree {output_deg}"));
}
}

Expand All @@ -173,8 +170,8 @@ pub fn interactive_module_define_fdmodule(

/// Given a string representation of an element in an algebra together with a generator, multiply
/// each term on the right with the generator.
fn replace(algebra_elt: &str, gen: &str) -> String {
algebra_elt.replace('+', &format!("{gen} +")) + " " + gen
fn replace(algebra_elt: &str, g: &str) -> String {
algebra_elt.replace('+', &format!("{g} +")) + " " + g
}

pub fn interactive_module_define_fpmodule(
Expand Down Expand Up @@ -203,8 +200,8 @@ pub fn interactive_module_define_fpmodule(

let mut degree_lookup = HashMap::default();
for (i, deg_i_gens) in gens.iter_enum() {
for gen in deg_i_gens.iter() {
degree_lookup.insert(gen.clone(), i);
for g in deg_i_gens.iter() {
degree_lookup.insert(g.clone(), i);
}
}

Expand All @@ -221,11 +218,11 @@ pub fn interactive_module_define_fpmodule(

// Check that the generators exist and the terms all have the same degree
let mut deg = None;
for (gen, (op_deg, _)) in result.iter() {
for (g, (op_deg, _)) in result.iter() {
let cur_deg = op_deg
+ degree_lookup
.get(gen)
.ok_or_else(|| anyhow!("Unknown generator: {gen}"))?;
.get(g)
.ok_or_else(|| anyhow!("Unknown generator: {g}"))?;
if deg.is_none() {
deg = Some(cur_deg);
} else if deg != Some(cur_deg) {
Expand All @@ -247,7 +244,7 @@ pub fn interactive_module_define_fpmodule(
let mut milnor_relation = String::new();

let mut milnor_op = FpVector::new(p, 0);
for (gen, (op_deg, adem_op)) in relation.iter() {
for (g, (op_deg, adem_op)) in relation.iter() {
if adem_op.is_zero() {
continue;
}
Expand All @@ -258,10 +255,10 @@ pub fn interactive_module_define_fpmodule(
milnor_op.set_scratch_vector_size(adem_op.len());
ev.adem_to_milnor(&mut milnor_op, 1, *op_deg, adem_op);

adem_relation += &replace(&ev.adem.element_to_string(*op_deg, adem_op.as_slice()), gen);
adem_relation += &replace(&ev.adem.element_to_string(*op_deg, adem_op.as_slice()), g);
milnor_relation += &replace(
&ev.milnor.element_to_string(*op_deg, milnor_op.as_slice()),
gen,
g,
);
}
if !adem_relation.is_empty() {
Expand All @@ -273,8 +270,8 @@ pub fn interactive_module_define_fpmodule(
output_json["p"] = Value::from(p.as_u32());
output_json["type"] = Value::String("finitely presented module".to_owned());
for (i, deg_i_gens) in gens.iter_enum() {
for gen in deg_i_gens {
output_json["gens"][gen] = Value::from(i);
for g in deg_i_gens {
output_json["gens"][g] = Value::from(i);
}
}
output_json["adem_relations"] = Value::Array(adem_relations);
Expand Down
6 changes: 3 additions & 3 deletions ext/examples/differentials.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,9 @@ fn main() -> anyhow::Result<()> {

for b in resolution.iter_stem() {
for i in 0..resolution.number_of_gens_in_bidegree(b) {
let gen = BidegreeGenerator::new(b, i);
let boundary = resolution.boundary_string(gen, true);
println!("d x_{gen:#} = {boundary}");
let g = BidegreeGenerator::new(b, i);
let boundary = resolution.boundary_string(g, true);
println!("d x_{g:#} = {boundary}");
}
}
Ok(())
Expand Down
4 changes: 2 additions & 2 deletions ext/examples/filtration_one.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,9 @@ fn main() -> anyhow::Result<()> {
// TODO: This doesn't work with the reordered Adams basis
let products = resolution.filtration_one_product(1 << i, 0, b).unwrap();
for (idx, row) in products.into_iter().enumerate() {
let gen = BidegreeGenerator::new(b, idx);
let g = BidegreeGenerator::new(b, idx);
if !row.is_empty() {
println!("h_{i} x_{gen} = {row:?}");
println!("h_{i} x_{g} = {row:?}");
}
}
i += 1;
Expand Down
Loading

0 comments on commit 3413606

Please sign in to comment.