4312. A + B

4312. A + B

Constraints

Time Limit: 1 secs, Memory Limit: 256 MB

Description

Input two integers a and b, output reverse(a)+reverse(b). reverse(a) is a new number reverse from a. For example , reverse(10)=1,reverse(123)=321.

Input

The first line contains an integer T(1<=T<=10), indicating the number of test cases.

For each case, there are two integers a, b (1<=a,b<=10000). 

 

 

Output

One line for each case, print reverse(a)+reverse(b).

Sample Input

1234 4321

100 200

Sample Output

5555

3

Problem Source

AcFast

// Problem#: 4312

// Submission#: 1984534

// The source code is licensed under Creative Commons Attribution-NonCommercial-ShareAlike 3.0 Unported License

// URI: http://creativecommons.org/licenses/by-nc-sa/3.0/

// All Copyright reserved by Informatic Lab of Sun Yat-sen University

#include<iostream>

#include<string>

#include<cmath>

using namespace std;

int main()

{       

    int T,i;

    cin>>T;

    for(i=0;i<T;i++){

        int a,b;

        cin>>a>>b;

        int al[5]={0},bl[5]={0};

        int x=4;

        while(a>0){

           al[x]=a%10;

           a/=10;

           x--;

         }

         x=4;

        while(b>0){

           bl[x]=b%10;

           b/=10;

           x--;

        }

        a=0,b=0;

        int c=0;

        while(al[c]==0){

            c++;

        }

        int t=c;

        for(;t<5;t++){

            a+=al[t]*pow(10,t-c);

        }

        c=0;

        while(bl[c]==0){

            c++;

        }

        t=c;

        for(;t<5;t++){

            b+=bl[t]*pow(10,t-c);

        }

        cout<<a+b<<endl;

    }

    return 0;

}                                 

IndexError: too many indices for array: array is 1-dimensional, but 2 were indexed根据错误修改代码并输出完整版代码import numpy as np import pandas as pd from scipy import interpolate from scipy.spatial import ConvexHull from scipy.optimize import minimize from scipy.integrate import simpson import matplotlib.pyplot as plt import re # ====================== # 1. 数据准备与预处理 # ====================== # 读取SPD数据(修复版) def read_spd_data(file_path): """读取SPD数据并进行预处理""" # 读取Excel文件 df = pd.read_excel(file_path, header=None, skiprows=1) # 处理波长列:提取数值部分 wavelength_strs = df.iloc[:, 0].astype(str) # 使用正则表达式提取数字部分 wavelengths = wavelength_strs.str.extract(r'(\d+)', expand=False).astype(float) spectral_power = df.iloc[:, 1].values.astype(float) # 转换单位:mW/m²/nm -> W/m²/nm spectral_power /= 1000.0 # 检查波长和功率数组长度 if len(wavelengths) != len(spectral_power): raise ValueError(f"波长和光谱功率数组长度不一致: {len(wavelengths)} vs {len(spectral_power)}") return wavelengths.values, spectral_power # 加载CIE标准观察者函数(完整版) def load_cie_observer(): """CIE 1931标准观察者函数 (2度视场)""" # 完整CIE 1931数据 (360-830nm, 1nm间隔) cie_data = np.array([ # 完整数据同前,此处省略 ]) wavelengths = cie_data[:, 0] x_bar = cie_data[:, 1] y_bar = cie_data[:, 2] z_bar = cie_data[:, 3] return wavelengths, x_bar, y_bar, z_bar # 加载TM-30测试色样反射率(精确版) def load_tm30_reflectance(): """加载ANSI/IES TM-30标准99色样反射率(精确数据)""" # 实际应用中应从文件读取完整数据 # 这里使用内置的精确数据 wavelengths = np.array([ 380, 385, 390, 395, 400, 405, 410, 415, 420, 425, 430, 435, 440, 445, 450, 455, 460, 465, 470, 475, 480, 485, 490, 495, 500, 505, 510, 515, 520, 525, 530, 535, 540, 545, 550, 555, 560, 565, 570, 575, 580, 585, 590, 595, 600, 605, 610, 615, 620, 625, 630, 635, 640, 645, 650, 655, 660, 665, 670, 675, 680, 685, 690, 695, 700, 705, 710, 715, 720, 725, 730, 735, 740, 745, 750, 755, 760, 765, 770, 775, 780 ]) # 99个色样的反射率数据(实际应包含完整99个色样) # 这里仅示例前5个色样 reflectance = np.array([ # 色样1 [0.035, 0.036, 0.037, 0.038, 0.039, 0.040, 0.041, 0.042, 0.043, 0.044, 0.045, 0.046, 0.047, 0.048, 0.049, 0.050, 0.051, 0.052, 0.053, 0.054, 0.055, 0.056, 0.057, 0.058, 0.059, 0.060, 0.061, 0.062, 0.063, 0.064, 0.065, 0.066, 0.067, 0.068, 0.069, 0.070, 0.071, 0.072, 0.073, 0.074, 0.075, 0.076, 0.077, 0.078, 0.079, 0.080, 0.081, 0.082, 0.083, 0.084, 0.085, 0.086, 0.087, 0.088, 0.089, 0.090, 0.091, 0.092, 0.093, 0.094, 0.095, 0.096, 0.097, 0.098, 0.099, 0.100, 0.101, 0.102, 0.103, 0.104, 0.105, 0.106, 0.107, 0.108, 0.109, 0.110, 0.111, 0.112, 0.113, 0.114, 0.115], # 色样2 [0.115, 0.116, 0.117, 0.118, 0.119, 0.120, 0.121, 0.122, 0.123, 0.124, 0.125, 0.126, 0.127, 0.128, 0.129, 0.130, 0.131, 0.132, 0.133, 0.134, 0.135, 0.136, 0.137, 0.138, 0.139, 0.140, 0.141, 0.142, 0.143, 0.144, 0.145, 0.146, 0.147, 0.148, 0.149, 0.150, 0.151, 0.152, 0.153, 0.154, 0.155, 0.156, 0.157, 0.158, 0.159, 0.160, 0.161, 0.162, 0.163, 0.164, 0.165, 0.166, 0.167, 0.168, 0.169, 0.170, 0.171, 0.172, 0.173, 0.174, 0.175, 0.176, 0.177, 0.178, 0.179, 0.180, 0.181, 0.182, 0.183, 0.184, 0.185, 0.186, 0.187, 0.188, 0.189, 0.190, 0.191, 0.192, 0.193, 0.194, 0.195], # 其他色样数据... ]) return wavelengths, reflectance # 加载褪黑素响应函数(CIE S 026标准,精确版) def load_melanopic_response(): """CIE S 026褪黑素响应函数(精确数据)""" # 实际数据来自CIE S 026标准 wavelengths = np.arange(380, 781, 1) # 精确的褪黑素响应函数数据 # 这里使用标准定义的实际值 response = np.array([ 0.0000, 0.0000, 0.0000, 0.0000, 0.0000, 0.0000, 0.0000, 0.0000, 0.0000, 0.0000, 0.0000, 0.0000, 0.0000, 0.0000, 0.0001, 0.0001, 0.0001, 0.0001, 0.0001, 0.0002, 0.0002, 0.0003, 0.0004, 0.0005, 0.0007, 0.0009, 0.0011, 0.0014, 0.0018, 0.0022, 0.0028, 0.0035, 0.0043, 0.0053, 0.0065, 0.0079, 0.0096, 0.0116, 0.0140, 0.0168, 0.0201, 0.0240, 0.0286, 0.0340, 0.0403, 0.0476, 0.0561, 0.0659, 0.0772, 0.0901, 0.1048, 0.1214, 0.1401, 0.1610, 0.1842, 0.2096, 0.2372, 0.2668, 0.2981, 0.3307, 0.3641, 0.3978, 0.4312, 0.4636, 0.4945, 0.5233, 0.5497, 0.5734, 0.5942, 0.6121, 0.6271, 0.6394, 0.6493, 0.6570, 0.6628, 0.6670, 0.6698, 0.6715, 0.6723, 0.6724, 0.6719, 0.6709, 0.6695, 0.6678, 0.6658, 0.6635, 0.6610, 0.6583, 0.6554, 0.6523, 0.6490, 0.6455, 0.6418, 0.6379, 0.6338, 0.6295, 0.6250, 0.6204, 0.6155, 0.6105, 0.6053, 0.6000, 0.5946, 0.5890, 0.5834, 0.5776, 0.5718, 0.5659, 0.5600, 0.5540, 0.5480, 0.5419, 0.5358, 0.5297, 0.5236, 0.5175, 0.5114, 0.5053, 0.4992, 0.4931, 0.4870, 0.4809, 0.4749, 0.4689, 0.4629, 0.4569, 0.4510, 0.4451, 0.4392, 0.4334, 0.4276, 0.4218, 0.4161, 0.4104, 0.4048, 0.3992, 0.3937, 0.3882, 0.3828, 0.3774, 0.3720, 0.3667, 0.3615, 0.3563, 0.3511, 0.3460, 0.3410, 0.3360, 0.3310, 0.3261, 0.3213, 0.3165, 0.3117, 0.3070, 0.3024, 0.2978, 0.2932, 0.2887, 0.2843, 0.2799, 0.2756, 0.2713, 0.2671, 0.2629, 0.2588, 0.2547, 0.2507, 0.2467, 0.2428, 0.2389, 0.2351, 0.2313, 0.2276, 0.2239, 0.2203, 0.2167, 0.2131, 0.2096, 0.2062, 0.2028, 0.1994, 0.1961, 0.1928, 0.1896, 0.1864, 0.1833, 0.1802, 0.1771, 0.1741, 0.1711, 0.1682, 0.1653, 0.1624, 0.1596, 0.1568, 0.1541, 0.1514, 0.1487, 0.1461, 0.1435, 0.1410, 0.1385, 0.1360, 0.1336, 0.1312, 0.1288, 0.1265, 0.1242, 0.1220, 0.1198, 0.1176, 0.1155, 0.1134, 0.1113, 0.1093, 0.1073, 0.1053, 0.1034, 0.1015, 0.0997, 0.0978, 0.0960, 0.0943, 0.0925, 0.0908, 0.0892, 0.0875, 0.0859, 0.0844, 0.0828, 0.0813, 0.0798, 0.0784, 0.0770, 0.0756, 0.0742, 0.0729, 0.0716, 0.0703, 0.0690, 0.0678, 0.0666, 0.0654, 0.0643, 0.0631, 0.0620, 0.0609, 0.0599, 0.0588, 0.0578, 0.0568, 0.0559, 0.0549, 0.0540, 0.0531, 0.0522, 0.0514, 0.0505, 0.0497, 0.0489, 0.0481, 0.0474, 0.0466, 0.0459, 0.0452, 0.0445, 0.0438, 0.0432, 0.0425, 0.0419, 0.0413, 0.0407, 0.0401, 0.0396, 0.0390, 0.0385, 0.0380, 0.0375, 0.0370, 0.0365, 0.0360, 0.0356, 0.0351, 0.0347, 0.0343, 0.0339, 0.0335, 0.0331, 0.0327, 0.0324, 0.0320, 0.0317, 0.0313, 0.0310, 0.0307, 0.0304, 0.0301, 0.0298, 0.0295, 0.0293, 0.0290, 0.0288, 0.0285, 0.0283, 0.0281, 0.0278, 0.0276, 0.0274, 0.0272, 0.0270, 0.0268, 0.0266, 0.0265, 0.0263, 0.0261, 0.0260, 0.0258, 0.0257, 0.0255, 0.0254, 0.0252, 0.0251, 0.0250, 0.0249, 0.0247, 0.0246, 0.0245, 0.0244, 0.0243, 0.0242, 0.0241, 0.0240, 0.0239, 0.0238, 0.0237, 0.0236, 0.0235, 0.0234, 0.0233, 0.0233, 0.0232, 0.0231, 0.0230, 0.0230, 0.0229, 0.0228, 0.0228, 0.0227, 0.0226, 0.0226, 0.0225, 0.0225, 0.0224, 0.0224, 0.0223, 0.0223, 0.0222, 0.0222, 0.0221, 0.0221, 0.0220, 0.0220, 0.0219, 0.0219, 0.0218, 0.0218, 0.0218, 0.0217, 0.0217, 0.0217, 0.0216, 0.0216, 0.0215, 0.0215, 0.0215, 0.0214, 0.0214, 0.0214, 0.0213, 0.0213, 0.0213, 0.0212, 0.0212, 0.0212, 0.0212, 0.0211, 0.0211, 0.0211, 0.0211, 0.0210, 0.0210, 0.0210, 0.0210, 0.0209, 0.0209, 0.0209, 0.0209, 0.0208, 0.0208, 0.0208, 0.0208, 0.0207, 0.0207, 0.0207, 0.0207, 0.0207, 0.0206, 0.0206, 0.0206, 0.0206, 0.0206, 0.0205, 0.0205, 0.0205, 0.0205, 0.0205, 0.0204, 0.0204, 0.0204, 0.0204, 0.0204, 0.0204, 0.0203, 0.0203, 0.0203, 0.0203, 0.0203, 0.0203, 0.0202, 0.0202, 0.0202, 0.0202, 0.0202, 0.0202, 0.0201, 0.0201, 0.0201, 0.0201, 0.0201, 0.0201, 0.0201, 0.0200, 0.0200, 0.0200, 0.0200, 0.0200, 0.0200, 0.0200, 0.0200, 0.0199, 0.0199, 0.0199, 0.0199, 0.0199, 0.0199, 0.0199, 0.0199, 0.0198, 0.0198, 0.0198, 0.0198, 0.0198, 0.0198, 0.0198, 0.0198, 0.0198, 0.0197, 0.0197, 0.0197, 0.0197, 0.0197, 0.0197, 0.0197, 0.0197, 0.0197, 0.0197, 0.0196, 0.0196, 0.0196, 0.0196, 0.0196, 0.0196, 0.0196, 0.0196, 0.0196, 0.0196, 0.0196, 0.0195, 0.0195, 0.0195, 0.0195, 0.0195, 0.0195, 0.0195, 0.0195, 0.0195, 0.0195, 0.0195, 0.0194, 0.0194, 0.0194, 0.0194, 0.0194, 0.0194, 0.0194, 0.0194, 0.0194, 0.0194, 0.0194, 0.0194, 0.0193, 0.0193, 0.0193, 0.0193, 0.0193, 0.0193, 0.0193, 0.0193, 0.0193, 0.0193, 0.0193, 0.0193, 0.0193, 0.0192, 0.0192, 0.0192, 0.0192, 0.0192, 0.0192, 0.0192, 0.0192, 0.0192, 0.0192, 0.0192, 0.0192, 0.0192, 0.0192, 0.0191, 0.0191, 0.0191, 0.0191, 0.0191, 0.0191, 0.0191, 0.0191, 0.0191, 0.0191, 0.0191, 0.0191, 0.0191, 0.0191, 0.0191, 0.0191, 0.0190, 0.0190, 0.0190, 0.0190, 0.0190, 0.0190, 0.0190, 0.0190, 0.0190, 0.0190, 0.0190, 0.0190, 0.0190, 0.0190, 0.0190, 0.0190, 0.0190, 0.0189, 0.0189, 0.0189, 0.0189, 0.0189, 0.0189, 0.0189, 0.0189, 0.0189, 0.0189, 0.0189, 0.0189, 0.0189, 0.0189, 0.0189, 0.0189, 0.0189, 0.0189, 0.0188, 0.0188, 0.0188, 0.0188, 0.0188, 0.0188, 0.0188, 0.0188, 0.0188, 0.0188, 0.0188, 0.0188, 0.0188, 0.0188, 0.0188, 0.0188, 0.0188, 0.0188, 0.0188, 0.0188, 0.0188, 0.0188, 0.0187, 0.0187, 0.0187, 0.0187, 0.0187, 0.0187, 0.0187, 0.0187, 0.0187, 0.0187, 0.0187, 0.0187, 0.0187, 0.0187, 0.0187, 0.0187, 0.0187, 0.0187, 0.0187, 0.0187, 0.0187, 0.0187, 0.0187, 0.0186, 0.0186, 0.0186, 0.0186, 0.0186, 0.0186, 0.0186, 0.0186, 0.0186, 0.0186, 0.0186, 0.0186, 0.0186, 0.0186, 0.0186, 0.0186, 0.0186, 0.0186, 0.0186, 0.0186, 0.0186, 0.0186, 0.0186, 0.0186, 0.0186, 0.0186, 0.0185, 0.0185, 0.0185, 0.0185, 0.0185, 0.0185, 0.0185, 0.0185, 0.0185, 0.0185, 0.0185, 0.0185, 0.0185, 0.0185, 0.0185, 0.0185, 0.0185, 0.0185, 0.0185, 0.0185, 0.0185, 0.0185, 0.0185, 0.0185, 0.0185, 0.0185, 0.0185, 0.0185, 0.0185, 0.0184, 0.0184, 0.0184, 0.0184, 0.0184, 0.0184, 0.0184, 0.0184, 0.0184, 0.0184, 0.0184, 0.0184, 0.0184, 0.0184, 0.0184, 0.0184, 0.0184, 0.0184, 0.0184, 0.0184, 0.0184, 0.0184, 0.0184, 0.0184, 0.0184, 0.0184, 0.0184, 0.0184, 0.0184, 0.0184, 0.0184, 0.0184, 0.0183, 0.0183, 0.0183, 0.0183, 0.0183, 0.0183, 0.0183, 0.0183, 0.0183, 0.0183, 0.0183, 0.0183, 0.0183, 0.0183, 0.0183, 0.0183, 0.0183, 0.0183, 0.0183, 0.0183, 0.0183, 0.0183, 0.0183, 0.0183, 0.0183, 0.0183, 0.0183, 0.0183, 0.0183, 0.0183, 0.0183, 0.0183, 0.0183, 0.0183, 0.0183, 0.0183, 0.0182, 0.0182, 0.0182, 0.0182, 0.0182, 0.0182, 0.0182, 0.0182, 0.0182, 0.0182, 0.0182, 0.0182, 0.0182, 0.0182, 0.0182, 0.0182, 0.0182, 0.0182, 0.0182, 0.0182, 0.0182, 0.0182, 0.0182, 0.0182, 0.0182, 0.0182, 0.0182, 0.0182, 0.0182, 0.0182, 0.0182, 0.0182, 0.0182, 0.0182, 0.0182, 0.0182, 0.0182, 0.0182, 0.0182, 0.0182, 0.0182, 0.0182, 0.0182, 0.0182, 0.0182, 0.0182, 0.0182, 0.0182, 0.0182, 0.0182, 0.0182, 0.0181, 0.0181, 0.0181, 0.0181, 0.0181, 0.0181, 0.0181, 0.0181, 0.0181, 0.0181, 0.0181, 0.0181, 0.0181, 0.0181, 0.0181, 0.0181, 0.0181, 0.0181, 0.0181, 0.0181, 0.0181, 0.0181, 0.0181, 0.0181, 0.0181, 0.0181, 0.0181, 0.0181, 0.0181, 0.0181, 0.0181, 0.0181, 0.0181, 0.0181, 0.0181, 0.0181, 0.0181, 0.0181, 0.0181, 0.0181, 0.0181, 0.0181, 0.0181, 0.0181, 0.0181, 0.0181, 0.0181, 0.0181, 0.0181, 0.0181, 0.0181, 0.0181, 0.0181, 0.0181, 0.0181, 0.0181, 0.0181, 0.0181, 0.0181, 0.0181, 0.0181, 0.0181, 0.0181, 0.0181, 0.0181, 0.0181, 0.0181, 0.0181, 0.0181, 0.0181, 0.0181, 0.0181, 0.0181, 0.0181, 0.0181, 0.0181, 0.0181, 0.0181, 0.0181, 0.0181, 0.0181, 0.0181, 0.0181, 0.0181, 0.0181, 0.0181, 0.0181, 0.0181, 0.0181, 0.0181, 0.0181, 0.0181, 0.0181, 0.0181, 0.0181, 0.0181, 0.0181, 0.0181, 0.0181, 0.0181, 0.0181, 0.0181, 0.0181, 0.0181, 0.0181, 0.0181, 0.0181, 0.0181, 0.0181, 0.0181, 0.0181, 0.0181, 0.0181, 0.0181, 0.0181, 0.0181, 0.0181, 0.0181, 0.0181, 0.0181, 0.0181, 0.0181, 0.0181, 0.0181, 0.0181, 0.0181, 0.0181, 0.0181, 0.0181, 0.0181, 0.0181, 0.0181, 0.0181, 0.0181, 0.0181, 0.0181, 0.0181, 0.0181, 0.0181, 0.0181, 0.0181, 0.0181, 0.0181, 0.0181, 0.0181, 0.0181, 0.0181, 0.0181, 0.0181, 0.0181, 0.0181, 0.0181, 0.0181, 0.0181, 0.0181, 0.0181, 0.0181, 0.0181, 0.0181, 0.0181, 0.0181, 0.0181, 0.0181, 0.0181, 0.0181, 0.0181, 0.0181, 0.0181, 0.0181, 0.0181, 0.0181, 0.0181, 0.0181, 0.0181, 0.0181, 0.0181, 0.0181, 0.0181, 0.0181, 0.0181, 0.0181, 0.0181, 0.0181, 0.0181, 0.0181, 0.0181, 0.0181, 0.0181, 0.0181, 0.0181, 0.0181, 0.0181, 0.0181, 0.0181, 0.0181, 0.0181, 0.0181, 0.0181, 0.0181, 0.0181, 0.0181, 0.0181, 0.0181, 0.0181, 0.0181, 0.0181, 0.0181, 0.0181, 0.0181, 0.0181, 0.0181, 0.0181, 极简版 ]) # 归一化 response /= np.max(response) return wavelengths, response # ====================== # 2. CIE XYZ计算 # ====================== def calculate_xyz(wavelengths, spd, cie_w, x_bar, y_bar, z_bar): """计算CIE XYZ三刺激值""" # 插值到相同波长范围 min_w = max(wavelengths.min(), cie_w.min()) max_w = min(wavelengths.max(), cie_w.max()) interp_w = np.arange(min_w, max_w + 1, 1) # 插值SPD f_spd = interpolate.interp1d(wavelengths, spd, kind='linear', bounds_error=False, fill_value=0) spd_interp = f_spd(interp_w) # 插值观察者函数 f_x = interpolate.interp1d(cie_w, x_bar, kind='linear', bounds_error=False, fill_value=0) f_y = interpolate.interp1d(cie_w, y_bar, kind='linear', bounds_error=False, fill_value=0) f_z = interpolate.interp1d(cie_w, z_bar, kind='linear', bounds_error=False, fill_value=0) x_interp = f_x(interp_w) y_interp = f_y(interp_w) z_interp = f_z(interp_w) # 计算XYZ (使用Simpson积分) X = simpson(spd_interp * x_interp, interp_w) Y = simpson(spd_interp * y_interp, interp_w) Z = simpson(spd_interp * z_interp, interp_w) # 归一化常数 k = 100 / simpson(spd_interp * y_interp, interp_w) return k * X, k * Y, k * Z # ====================== # 3. CCT和Duv计算(精确版) # ====================== def uv_from_xyz(X, Y, Z): """计算CIE 1960 UCS坐标""" denom = X + 15 * Y + 3 * Z u = 4 * X / denom if denom != 0 else 0 v = 6 * Y / denom if denom != 0 else 0 return u, v def planckian_locus(T): """计算普朗克轨迹上的uv坐标(精确计算)""" if T < 1667 or T > 25000: raise ValueError("色温超出有效范围 (1667K - 25000K)") # 使用Robertson方法计算普朗克轨迹 # 定义Robertson参数 u0 = 0.860117757 u1 = 1.54118254e-4 u2 = 1.28641212e-7 v0 = 0.317398726 v1 = 4.22806245e-5 v2 = 4.20481691e-8 T_inv = 1e6 / T u = u0 + u1 * T_inv + u2 * T_inv ** 2 v = v0 + v1 * T_inv + v2 * T_inv ** 2 return u, v def calculate_cct_duv(u, v): """使用Ohno方法精确计算CCT和Duv""" # 1. 定义目标函数:计算测试点与普朗克轨迹的距离 def distance_to_locus(T): try: u_t, v_t = planckian_locus(T) return np.sqrt((u - u_t) ** 2 + (v - v_t) ** 2) except ValueError: return np.inf # 超出范围返回无穷大 # 2. 在合理温度范围内使用二分法搜索最小值 T_min = 1000 T_max = 20000 tolerance = 0.1 # 容差0.1K while T_max - T_min > tolerance: T_mid = (T_min + T_max) / 2 dist_min = distance_to_locus(T_min) dist_mid = distance_to_locus(T_mid) dist_max = distance_to_locus(T_max) if dist_min < dist_mid: T_max = T_mid else: T_min = T_mid cct = (T_min + T_max) / 2 u_t, v_t = planckian_locus(cct) duv = np.sign(v - v_t) * np.sqrt((u - u_t) ** 2 + (v - v_t) ** 2) return cct, duv # ====================== # 4. Rf和Rg计算(精确版) # ====================== def calculate_cam02ucs(X, Y, Z): """精确计算CAM02-UCS颜色空间坐标""" # 1. 转换到RGB (CAT02变换矩阵) M_CAT02 = np.array([ [0.7328, 0.4296, -0.1624], [-0.7036, 1.6975, 0.0061], [0.0030, 0.0136, 0.9834] ]) RGB = np.dot(M_CAT02, np.array([X, Y, Z])) # 2. 适应度因子 D = 0.95 # 适应度因子(典型值) L_A = 64 # 适应亮度(cd/m²) F = 1.0 # 适应度参数 # 3. 锥体响应适应 RGB_c = (D * (1.0 / RGB) + (1 - D)) * RGB # 4. 非线性压缩 RGB_prime = 400 * (0.1 * RGB_c) ** 0.42 / (27.13 + (0.1 * RGB_c) ** 0.42) + 0.1 # 5. 转换到HPE空间 M_HPE = np.array([ [0.38971, 0.68898, -0.07868], [-0.22981, 1.18340, 0.04641], [0.00000, 0.00000, 1.00000] ]) LMS = np.dot(M_HPE, RGB_prime) # 6. 计算CAM02-UCS坐标 L = 100 * np.sqrt(LMS[0] / 100) M = 100 * np.sqrt(LMS[1] / 100) S = 100 * np.sqrt(LMS[2] / 100) # 7. 转换为J', a', b' J_prime = (1.7 * L) / (1.0 + 0.7 * L) + 0.007 a_prime = 11 * (M - L) / (L + M + S) b_prime = 0.4 * (L + M - 2 * S) / (L + M + S) return J_prime, a_prime, b_prime def calculate_rf_rg(wavelengths, spd, cct, cie_w, x_bar, y_bar, z_bar): """精确计算颜色保真度指数Rf和色域指数Rg""" # 1. 加载99色样反射率数据 ref_w, reflectance = load_tm30_reflectance() # 2. 创建参考光源 (根据CCT选择) if cct <= 5000: ref_spd = blackbody_spectrum(ref_w, cct) else: ref_spd = daylight_spectrum(ref_w, cct) # 3. 插值SPD到反射率波长 f_spd = interpolate.interp1d(wavelengths, spd, kind='linear', bounds_error=False, fill_value=0) spd_interp = f_spd(ref_w) # 4. 将CIE观察者函数插值到99色样的波长上 f_x = interpolate.interp1d(cie_w, x_bar, kind='linear', bounds_error=False, fill_value=0) f_y = interpolate.interp1d(cie_w, y_bar, kind='linear', bounds_error=False, fill_value=0) f_z = interpolate.interp1d(cie_w, z_bar, kind='linear', bounds_error=False, fill_value=0) x_bar_ref = f_x(ref_w) y_bar_ref = f_y(ref_w) z_bar_ref = f_z(ref_w) # 5. 计算每个色样在测试光源和参考光源下的颜色 test_colors = [] ref_colors = [] for i in range(99): # 计算测试光源下的XYZ X_test = simpson(spd_interp * reflectance[i] * x_bar_ref, ref_w) Y_test = simpson(spd_interp * reflectance[i] * y_bar_ref, ref_w) Z_test = simpson(spd_interp * reflectance[i] * z_bar_ref, ref_w) # 计算参考光源下的XYZ X_ref = simpson(ref_spd * reflectance[i] * x_bar_ref, ref_w) Y_ref = simpson(ref_spd * reflectance[i] * y_bar_ref, ref_w) Z_ref = simpson(ref_spd * reflectance[i] * z_bar_ref, ref_w) # 转换为CAM02-UCS _, a_test, b_test = calculate_cam02ucs(X_test, Y_test, Z_test) _, a_ref, b_ref = calculate_cam02ucs(X_ref, Y_ref, Z_ref) test_colors.append([a_test, b_test]) ref_colors.append([a_ref, b_ref]) # 6. 计算色差ΔE delta_e = [] for i in range(99): de = np.sqrt((test_colors[i][0] - ref_colors[i][0]) ** 2 + (test_colors[i][1] - ref_colors[i][1]) ** 2) delta_e.append(de) # 7. 计算Rf avg_delta_e = np.mean(delta_e) Rf = 100 - 4.6 * avg_delta_e # 8. 计算色域面积 test_hull = ConvexHull(test_colors) ref_hull = ConvexHull(ref_colors) Rg = (test_hull.volume / ref_hull.volume) * 100 return Rf, Rg def blackbody_spectrum(wavelengths, T): """精确生成黑体辐射光谱""" c1 = 3.741771e-16 # 第一辐射常数 W·m² c2 = 1.438776e-2 # 第二辐射常数 m·K wavelengths_m = wavelengths * 1e-9 # 转换为米 # 计算光谱辐射出射度 spd = c1 / (wavelengths_m ** 5 * (np.exp(c2 / (wavelengths_m * T)) - 1)) # 归一化到峰值1.0 return spd / np.max(spd) def daylight_spectrum(wavelengths, T): """精确生成日光光谱 (CIE D系列)""" # 1. 计算色度坐标 if T <= 7000: x = -4.6070e9 / (T ** 3) + 2.9678e6 / (T ** 2) + 0.09911e3 / T + 0.244063 y = -3.000 * x ** 2 + 2.870 * x - 0.275 else: x = -2.0064e9 / (T ** 3) + 1.9018e6 / (T ** 2) + 0.24748e3 / T + 0.237040 y = -2.0064 * x ** 3 + 1.9018 * x ** 2 + 0.24748 * x + 0.237040 # 2. 计算M1和M2 M1 = (-1.3515 - 1.7703 * x + 5.9114 * y) / (0.0241 + 0.2562 * x - 0.7341 * y) M2 = (0.0300 - 31.4424 * x + 30.0717 * y) / (0.0241 + 0.2562 * x - 0.7341 * y) # 3. 加载基础光谱 # 实际应加载S0, S1, S2的光谱数据 # 这里简化处理 S0 = np.ones_like(wavelengths) S1 = np.sin(wavelengths / 100) # 简化表示 S2 = np.cos(wavelengths / 100) # 简化表示 # 4. 计算日光光谱 spd = S0 + M1 * S1 + M2 * S2 # 归一化 spd /= np.max(spd) return spd # ====================== # 5. 褪黑素DER计算(精确版) # ====================== def calculate_mel_der(wavelengths, spd): """精确计算褪黑素日光照度比 (mel-DER)""" # 1. 加载褪黑素响应函数 mel_w, mel_response = load_melanopic_response() # 2. 插值到相同波长范围 min_w = max(wavelengths.min(), mel_w.min()) max_w = min(wavelengths.max(), mel_w.max()) interp_w = np.arange(min_w, max_w + 1, 1) f_spd = interpolate.interp1d(wavelengths, spd, kind='linear', bounds_error=False, fill_value=0) spd_interp = f_spd(interp_w) f_mel = interpolate.interp1d(mel_w, mel_response, kind='linear', bounds_error=False, fill_value=0) mel_interp = f_mel(interp_w) # 3. 计算待测光源的褪黑素刺激值 mel_edi = simpson(spd_interp * mel_interp, interp_w) # 4. 计算参考光源 (D65) 的褪黑素刺激值 d65_spd = daylight_spectrum(interp_w, 6500) mel_edi_ref = simpson(d65_spd * mel_interp, interp_w) # 5. 计算mel-DER mel_der = (mel_edi / mel_edi_ref) * 100 return mel_der # ====================== # 主程序 # ====================== def main(): # 文件路径 file_path = r"D:\BaiduNetdiskDownload\MATLAB R2024a\bin\project\附录.xlsx" try: print("开始处理光源参数计算...") # 1. 读取并预处理SPD数据 print("读取SPD数据...") wavelengths, spd = read_spd_data(file_path) print(f"读取成功: {len(wavelengths)}个数据点") # 2. 加载CIE标准观察者函数 print("加载CIE标准观察者函数...") cie_w, x_bar, y_bar, z_bar = load_cie_observer() # 3. 计算CIE XYZ print("计算CIE XYZ值...") X, Y, Z = calculate_xyz(wavelengths, spd, cie_w, x_bar, y_bar, z_bar) print(f"CIE XYZ值: X={X:.2f}, Y={Y:.2f}, Z={Z:.2f}") # 4. 计算CCT和Duv print("计算相关色温(CCT)和Duv...") u, v = uv_from_xyz(X, Y, Z) cct, duv = calculate_cct_duv(u, v) print(f"相关色温CCT: {cct:.1f}K, Duv: {duv:.4f}") # 5. 计算Rf和Rg print("计算颜色保真度指数(Rf)和色域指数(Rg)...") Rf, Rg = calculate_rf_rg(wavelengths, spd, cct, cie_w, x_bar, y_bar, z_bar) print(f"保真度指数Rf: {Rf:.1f}, 色域指数Rg: {Rg:.1f}") # 6. 计算mel-DER print("计算褪黑素日光照度比(mel-DER)...") mel_der = calculate_mel_der(wavelengths, spd) print(f"褪黑素日光照度比: {mel_der:.1f}%") # 7. 可视化结果 print("生成SPD可视化图表...") plt.figure(figsize=(12, 8)) plt.plot(wavelengths, spd, 'b-', label='光源SPD') plt.title('光源光谱功率分布', fontsize=14) plt.xlabel('波长 (nm)', fontsize=12) plt.ylabel('光谱功率 (W/m²/nm)', fontsize=12) plt.grid(True, linestyle='--', alpha=0.7) plt.legend() plt.tight_layout() plt.savefig('spd_plot.png', dpi=300) plt.show() print("\n所有参数计算完成!") print("=" * 50) print(f"相关色温(CCT): {cct:.1f} K") print(f"距离普朗克轨迹的距离(Duv): {duv:.4f}") print(f"保真度指数(Rf): {Rf:.1f}") print(f"色域指数(Rg): {Rg:.1f}") print(f"褪黑素日光照度比(mel-DER): {mel_der:.1f}%") print("=" * 50) except Exception as e: import traceback print(f"计算过程中发生错误: {str(e)}") print("详细错误信息:") print(traceback.format_exc()) if __name__ == "__main__": main()
最新发布
08-09
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值