Skogestad-Python icon indicating copy to clipboard operation
Skogestad-Python copied to clipboard

Prefer .T over np.transpose() for better performance and cleaner syntax

Open SaFE-APIOpt opened this issue 9 months ago • 0 comments

https://github.com/alchemyst/Skogestad-Python/blob/bcdb6778a3b1adc0aed2d2f2864122c7da8f7fcd/SVD_w.py#L35 In the code: output_direction_max[count, :] = numpy.transpose(U[:, 0]) input_direction_max[count, :] = numpy.transpose(V[:, 0]) output_direction_min[count, :] = numpy.transpose(U[:, -1]) input_direction_min[count, :] = numpy.transpose(V[:, -1]) it is recommended to use .T instead of numpy.transpose().

Both are functionally equivalent, but .T is more concise and performs slightly better because it directly accesses the array's transpose attribute without going through an extra function call stack. This becomes especially beneficial in performance-critical code or high-frequency operations.

Suggested improvement: output_direction_max[count, :] = U[:, 0].T input_direction_max[count, :] = V[:, 0].T output_direction_min[count, :] = U[:, -1].T input_direction_min[count, :] = V[:, -1].T

SaFE-APIOpt avatar Mar 18 '25 08:03 SaFE-APIOpt