Skip to content

[Ready for review] Refactor polymorphism#2

Merged
dance858 merged 10 commits intomainfrom
refactor-polymorphism
Jan 3, 2026
Merged

[Ready for review] Refactor polymorphism#2
dance858 merged 10 commits intomainfrom
refactor-polymorphism

Conversation

@dance858
Copy link
Collaborator

@dance858 dance858 commented Jan 2, 2026

Cleaned up elementwise univariate and affine atoms, and implemented better polymorphism. Also refactored hstack to precompute sparsity pattern, which is pretty nice.

@dance858 dance858 changed the title [WIP] Refactor polymorphism [Ready for review] Refactor polymorphism Jan 3, 2026
Copy link
Collaborator

@Transurgeon Transurgeon left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM!
had a few questions, but feel free to merge after. Looks great!

Comment on lines -17 to -33
if (!node->value)
{
free(node);
return NULL;
}

// node->left = NULL;
// node->right = NULL;
// node->forward = NULL;
// node->jacobian = NULL;
// node->jacobian_init = NULL;
// node->eval_jacobian = NULL;
// node->is_affine = NULL;
// node->dwork = NULL;
// node->iwork = NULL;
// node->CSR_work = NULL;
node->var_id = -1;
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I am wondering how this code was working previously? Maybe not important, but it would be nice if you could clarify.
My understanding is that these fields were just being initialized then populated later.
Now instead, they must be passed as arguments to the init_expr constructor.

1. power should be double
2. can we reuse calculations, like in hessian of logistic
3. more tests for chain rule elementwise univariate hessian
4. in the refactor, add consts
Copy link
Collaborator

@Transurgeon Transurgeon Jan 3, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

is this done? Did you mean to add the type hints (I am not sure if that's what we call them) for the arguments to functions?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I changed from "int p" to "double p" in power_expr, so now it's the correct data type

Comment on lines +26 to +27
// free_csr_matrix(lin_node->A_csr);
free_csc_matrix(lin_node->A_csc);
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why do we have both csc and csr matrices? Could you explain.. so that I understand the comment above?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The chain rule implementation becomes much more efficient if we can both traverse columns and rows of A fast. So this is just for efficiency (and also for simplifying the code).

Comment on lines +37 to +38
init_expr(node, A->m, 1, u->n_vars, forward, NULL, NULL, is_affine,
free_type_data);
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I see where init_expr is being used now, please ignore my comments above!

Comment on lines +42 to +45
/* Initialize type-specific fields */
lin_node->A_csr = new_csr_matrix(A->m, A->n, A->nnz);
copy_csr_matrix(A, lin_node->A_csr);
lin_node->A_csc = csr_to_csc(A);
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't understand the changes here. Why are we converting csr to csc?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We store A both in CSC and CSR for efficient chain rule implementation

@dance858 dance858 merged commit 4698872 into main Jan 3, 2026
9 checks passed
@dance858 dance858 deleted the refactor-polymorphism branch January 4, 2026 07:40
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants