Reddit Programming
207 subscribers
1.22K photos
123K links
I will send you newest post from subreddit /r/programming
Download Telegram
C preprocessor in Python
https://www.reddit.com/r/programming/comments/1g7x2ak/c_preprocessor_in_python/

<!-- SC_OFF -->Hi. I've made a C language preprocessor in Python. It's nothing too fancy, but it can be useful if you work with C and need to expand macros in a controlled manner for some special purposes. The main highlight is that the expanded macros retain the original formatting, i.e., the whitespaces are preserved (well, mostly...). Feel free to use it if you want. <!-- SC_ON --> submitted by /u/solitary_black_sheep (https://www.reddit.com/user/solitary_black_sheep)
[link] (https://github.com/lubomilko/pycpp) [comments] (https://www.reddit.com/r/programming/comments/1g7x2ak/c_preprocessor_in_python/)
The empire of C++ strikes back with Safe C++ blueprint: « After two years of being beaten with the memory-safety stick, the C++ community has published a proposal to help developers write less vulnerable code. »
https://www.reddit.com/r/programming/comments/1g85xbs/the_empire_of_c_strikes_back_with_safe_c/

submitted by /u/fchung (https://www.reddit.com/user/fchung)
[link] (https://www.theregister.com/2024/09/16/safe_c_plusplus/) [comments] (https://www.reddit.com/r/programming/comments/1g85xbs/the_empire_of_c_strikes_back_with_safe_c/)
Help - eigenvalue problem optimization
https://www.reddit.com/r/programming/comments/1g8l9a3/help_eigenvalue_problem_optimization/

<!-- SC_OFF -->Hi all, I’m a student in civil engineering and I would appreciate if anyone could help me with this problem I’m trying to solve on matlab. I want to determine my k values (k1,k2 and k3) such that I get the T1,T2 and T3 values. Note that [K] - ω²[M] = 0 and ω is fcn of period, ωi=2pi/Ti. However the difference is significant. I was wondering if you have any suggestions on how I can optimize my code to get close for all the 3 periods. % Given data m1 = 33317; m2 = 20226; m3 = 19025; M = diag([m1, m2, m3]); % Mass matrix (3x3) T1_wanted = 5.2; T2_wanted = 4.0; T3_wanted = 1.17; T_wanted = [T1_wanted; T2_wanted; T3_wanted]; % Target periods tolerance = 0.02; % Desired tolerance for period difference % Number of random initial guesses to try num_guesses = 1000; % Set bounds for k1, k2, k3 to limit search space k1_lb = 100; k1_ub = 1e8; % Increased upper bounds for k1 k2_lb = 100; k2_ub = 1e8; % Increased upper bounds for k2 k3_lb = 100; k3_ub = 1e8; % Increased upper bounds for k3 % Set optimization options for fmincon (use more precise tolerances) options = optimoptions('fmincon', 'Algorithm', 'sqp', 'Display', 'off', ... 'TolFun', 1e-10, 'TolX', 1e-10, ... 'MaxIterations', 500, 'MaxFunctionEvaluations', 1000); % Call the optimize_stiffness_values function to find the optimal k1, k2, k3 [k_opt_values, opt_fun_values] = optimize_stiffness_values(num_guesses, M, T_wanted, ... k1_lb, k1_ub, k2_lb, k2_ub, k3_lb, k3_ub, options, tolerance); % Find the optimal set of stiffness values that minimizes the objective function [~, min_idx] = min(opt_fun_values); k1_opt = k_opt_values(min_idx, 1); k2_opt = k_opt_values(min_idx, 2); k3_opt = k_opt_values(min_idx, 3); % Display the optimized stiffness values disp(['Optimized stiffness k1: ', num2str(k1_opt)]); disp(['Optimized stiffness k2: ', num2str(k2_opt)]); disp(['Optimized stiffness k3: ', num2str(k3_opt)]); % Calculate the final periods with optimized stiffness values K_opt = stiffness_matrix(k1_opt, k2_opt, k3_opt); % Debugging: Check the dimensions of K_opt and M disp('Dimensions of K_opt:'); disp(size(K_opt)); % Should be 3x3 disp('Dimensions of M:'); disp(size(M)); % Should be 3x3 % Ensure K_opt and M are square and have the same dimensions assert(isequal(size(K_opt), size(M)), 'K and M must be square and have the same dimensions.'); % Solve the generalized eigenvalue problem [~, eigenvalues_opt] = eig(K_opt, M); % Solve K * phi = lambda * M * phi % Compute natural frequencies from eigenvalues omega_opt = sqrt(diag(eigenvalues_opt)); % Calculate periods T_final = 2 * pi ./ omega_opt; % Display the final periods and their difference from the target values disp('Final periods:'); disp(T_final); disp('Difference between calculated and wanted periods:'); disp(abs(T_final - T_wanted)); % ------------ Optimizing Stiffness Values Function ----------------- function [k_opt_values, opt_fun_values] = optimize_stiffness_values(num_guesses, M, T_wanted, ... k1_lb, k1_ub, k2_lb, k2_ub, k3_lb, k3_ub, options, tolerance) % Preallocate array to store the optimized stiffness values and objective function values k_opt_values = zeros(num_guesses, 3); opt_fun_values = zeros(num_guesses, 1); % Solve the optimization for multiple random initial guesses for i = 1:num_guesses % Generate random initial guesses within bounds (using rand to generate floats) k1_init = k1_lb + (k1_ub - k1_lb) * rand; % Random float between k1_lb and k1_ub k2_init = k2_lb + (k2_ub - k2_lb) * rand; % Random float between k2_lb and k2_ub k3_init = k3_lb + (k3_ub - k3_lb) * rand; % Random float between k3_lb and k3_ub % Initial guess vector k_init = [k1_init, k2_init, k3_init]; % Define the inequality constraint matrix to enforce k1 >= k2 >= k3 A = [-1 1 0; 0 -1 1]; % Matrix for k1 >= k2 >= k3 b = [0; 0]; % Right-hand side for inequality constraints % Solve the constrained optimization