| |
| """ |
| Validator for problem 054: Spherical 9-Design on S² |
| |
| A spherical t-design on Sⁿ⁻¹ is a finite set of points such that the average |
| of any polynomial of degree ≤ t over the points equals the integral over the sphere. |
| |
| For a spherical 9-design on S² (unit sphere in R³), the minimum number of points |
| is bounded below by (t+d-1 choose d-1) + (t+d-2 choose d-1) for d=3, t=9. |
| |
| This validator checks: |
| 1. All points are on the unit sphere S² |
| 2. The design property holds for all polynomials up to degree 9 |
| (verified by checking moment conditions) |
| |
| Expected input format: |
| {"points": [[x₁, x₂, x₃], ...]} points on S² (unit sphere in R³) |
| or [[x₁, x₂, x₃], ...] |
| """ |
|
|
| import argparse |
| from itertools import product |
| from typing import Any |
|
|
| import numpy as np |
| from scipy.special import comb |
|
|
| from . import ValidationResult, load_solution, output_result, success, failure |
|
|
|
|
| DIMENSION = 3 |
| DESIGN_DEGREE = 9 |
| TOLERANCE = 1e-8 |
|
|
|
|
| def sphere_moment(powers: tuple[int, ...]) -> float: |
| """ |
| Compute the integral of x₁^p₁ * x₂^p₂ * ... over the unit sphere. |
| |
| For the unit sphere Sⁿ⁻¹, the integral is: |
| - 0 if any power is odd |
| - Product of double factorial ratios otherwise |
| """ |
| n = len(powers) |
|
|
| |
| if any(p % 2 == 1 for p in powers): |
| return 0.0 |
|
|
| |
| |
| |
|
|
| total_degree = sum(powers) |
|
|
| |
| |
| from math import gamma, pi |
|
|
| numerator = 1.0 |
| for p in powers: |
| numerator *= gamma((p + 1) / 2) |
|
|
| denominator = gamma((n + total_degree) / 2) |
|
|
| return numerator / denominator * gamma(n / 2) / pi ** (n / 2) |
|
|
|
|
| def validate(solution: Any) -> ValidationResult: |
| """ |
| Validate a spherical 9-design on S². |
| |
| Args: |
| solution: Dict with 'points' key or list of 3D points |
| |
| Returns: |
| ValidationResult with design verification |
| """ |
| try: |
| if isinstance(solution, dict) and 'points' in solution: |
| points_data = solution['points'] |
| elif isinstance(solution, list): |
| points_data = solution |
| else: |
| return failure("Invalid format: expected dict with 'points' or list") |
|
|
| points = np.array(points_data, dtype=np.float64) |
| except (ValueError, TypeError) as e: |
| return failure(f"Failed to parse points: {e}") |
|
|
| if points.ndim != 2: |
| return failure(f"Points must be 2D array, got {points.ndim}D") |
|
|
| n, d = points.shape |
| if d != DIMENSION: |
| return failure(f"Points must be in R³, got dimension {d}") |
|
|
| if n == 0: |
| return failure("No points provided") |
|
|
| |
| norms = np.linalg.norm(points, axis=1) |
| off_sphere = np.abs(norms - 1.0) > TOLERANCE |
| if np.any(off_sphere): |
| worst_idx = np.argmax(np.abs(norms - 1.0)) |
| return failure( |
| f"Point {worst_idx} not on unit sphere: |x| = {norms[worst_idx]:.10f}", |
| off_sphere_count=int(np.sum(off_sphere)) |
| ) |
|
|
| |
| max_error = 0.0 |
| worst_monomial = None |
|
|
| for total_deg in range(DESIGN_DEGREE + 1): |
| |
| for powers in product(range(total_deg + 1), repeat=DIMENSION): |
| if sum(powers) != total_deg: |
| continue |
|
|
| |
| monomial_values = np.prod(points ** powers, axis=1) |
| point_avg = np.mean(monomial_values) |
|
|
| |
| sphere_avg = sphere_moment(powers) |
|
|
| error = abs(point_avg - sphere_avg) |
| if error > max_error: |
| max_error = error |
| worst_monomial = powers |
|
|
| if max_error > TOLERANCE: |
| return failure( |
| f"Not a {DESIGN_DEGREE}-design: max error = {max_error:.2e} at monomial {worst_monomial}", |
| max_moment_error=max_error |
| ) |
|
|
| return success( |
| f"Valid spherical {DESIGN_DEGREE}-design on S² with {n} points (max error: {max_error:.2e})", |
| dimension=DIMENSION, |
| num_points=n, |
| design_degree=DESIGN_DEGREE, |
| max_moment_error=max_error |
| ) |
|
|
|
|
| def main(): |
| parser = argparse.ArgumentParser(description='Validate spherical 9-design on S²') |
| parser.add_argument('solution', help='Solution as JSON string or path to JSON file') |
| parser.add_argument('--verbose', '-v', action='store_true', help='Verbose output') |
| args = parser.parse_args() |
|
|
| solution = load_solution(args.solution) |
| result = validate(solution) |
| output_result(result) |
|
|
|
|
| if __name__ == '__main__': |
| main() |
|
|