Optimal transform algorithm