Project: s41467-025-61475-w | Function: free_energy (python)

system: You are an expert Python programmer implementing algorithms from scientific papers.
Complete the masked function based on the algorithm description.
Follow Python best practices and maintain consistent code style.
Return **only** the complete Python function code.

user: Complete this Python function:

**DESCRIPTION:** Computes the normalizing potential φ_γ for curved neural networks with replica symmetry. Implements the mean-field free energy calculation that captures explosive phase transitions and memory capacity enhancement.

**KEY TERMS:** {
  "phi": "normalizing potential φ_γ",
  "m": "magnetization order parameter",
  "q": "overlap order parameter",
  "alpha": "memory load parameter α",
  "beta": "inverse temperature β"
}

**CONTEXT CODE:** """
GPLv3 2025 Miguel Aguilera
Clean version for unit testing - only function definitions with masks
"""

# Import required libraries
import numpy as np
from numba import njit
import os

# Configure matplotlib (LaTeX disabled for testing)
import matplotlib
matplotlib.use('Agg')  # Use non-interactive backend

# Coupling constant (used in the Hamiltonian or dynamics)
J = 1

# Define numerically stable log(cosh(x)) function
@njit
def log2cosh(x):
    ax = np.abs(x)
    return ax + np.log(1 + np.exp(-2 * ax))

# Compute the G-term in the free energy, involving Gaussian integral with log2cosh
@njit
def G(m, q, alpha, b, gamma):
    r = q / (1 - b * J * (1 - q))**2
    R = (1 / (b * J) - (1 - 2 * q)) / (1 - b * J * (1 - q))**2
    g = b * m * J
    sqrtD = b * J * np.sqrt(alpha * r)
    zmax = 6
    z0 = 0
    Nint = 1001
    x = np.linspace(-1., 1., Nint) * zmax + z0  # Integration support
    Gx = 1 / np.sqrt(2 * np.pi) * np.exp(-0.5 * x**2) * log2cosh(g + x * sqrtD)  # Integrand
    return np.sum(Gx) * (x[1] - x[0])  # Riemann sum

# Compute the free energy of the system depending on m, q, and various parameters
@njit
def free_energy(m, q, b, alpha, beta, gamma):
    """Compute the free energy of the system depending on m, q, and various parameters"""
    raise NotImplementedError("Function implementation has been masked")

Complete the function implementation replacing "raise NotImplementedError".