Changeset 1334 for applications/robust
- Timestamp:
- 04/18/11 09:35:41 (14 years ago)
- Files:
-
- 1 modified
Legend:
- Unmodified
- Added
- Removed
-
applications/robust/robustlib.h
r1331 r1334 1791 1791 if(conditions.size()-2-number_of_parameters>=0) 1792 1792 { 1793 1794 1793 1795 mat sample_mat; 1794 1796 map<double,toprow*> ordered_toprows; … … 1805 1807 while(sample_mat.cols()<n) 1806 1808 { 1809 cout << "*************************************" << endl; 1810 1807 1811 double rnumber = randu()*sum_a; 1808 1812 … … 1810 1814 1811 1815 // This could be more efficient (log n), but map::upper_bound() doesn't let me dereference returned iterator 1816 int toprow_count = 0; 1812 1817 toprow* sampled_toprow; 1813 1818 for(map<double,toprow*>::iterator top_ref = ordered_toprows.begin();top_ref!=ordered_toprows.end();top_ref++) 1814 1819 { 1815 1820 // cout << "CDF:"<< (*top_ref).first << endl; 1821 toprow_count++; 1816 1822 1817 1823 if((*top_ref).first >= rnumber) … … 1822 1828 } 1823 1829 1830 cout << "Toprow/Count: " << toprow_count << "/" << ordered_toprows.size() << endl; 1824 1831 // cout << &sampled_toprow << ";"; 1825 1832 … … 1828 1835 set<simplex*>::iterator s_ref; 1829 1836 double sum_b = 0; 1837 int simplex_count = 0; 1830 1838 for(s_ref = sampled_toprow->triangulation.begin();s_ref!=sampled_toprow->triangulation.end();s_ref++) 1831 1839 { 1840 simplex_count++; 1841 1832 1842 sum_b += (*s_ref)->probability; 1833 1843 … … 1836 1846 } 1837 1847 1848 cout << "Simplex/Count: " << simplex_count << "/" << sampled_toprow->triangulation.size() << endl; 1838 1849 // cout << &(*tri_ref) << endl; 1839 1850 … … 1903 1914 for(set<vertex*>::iterator vert_ref = ++(*s_ref)->vertices.begin();vert_ref!=(*s_ref)->vertices.end();vert_ref++) 1904 1915 { 1905 jacobian.set_row(row_count,(*vert_ref)->get_coordinates()-base_vert->get_coordinates()); 1916 vec relative_coords = (*vert_ref)->get_coordinates()-base_vert->get_coordinates(); 1917 1918 jacobian.set_row(row_count,relative_coords); 1906 1919 1907 1920 row_count++; 1908 1921 } 1909 1922 1923 cout << "Jacobian: " << jacobian << endl; 1924 1925 cout << "Gradient before trafo:" << gradient << endl; 1926 1927 gradient = jacobian*gradient; 1928 1929 // vec normal_gradient = gradient/sqrt(gradient*gradient); 1910 1930 // cout << gradient << endl; 1911 // cout << jacobian << endl; 1912 1913 gradient = jacobian*gradient; 1914 1915 //cout << gradient << endl; 1916 1917 mat rotation_matrix = eye(dimension); 1918 1919 double squared_length = 1; 1931 // cout << normal_gradient << endl; 1932 // cout << sqrt(gradient*gradient) << endl; 1933 1934 mat rotation_matrix = eye(dimension); 1935 1936 1920 1937 1921 1938 for(int i = 1;i<dimension;i++) 1922 1939 { 1923 double t = gradient[i]/sqrt(squared_length); 1924 1925 double sin_theta = t/sqrt(1+pow(t,2)); 1926 double cos_theta = 1/sqrt(1+pow(t,2)); 1940 vec x_axis = zeros(dimension); 1941 x_axis.set(0,1); 1942 1943 x_axis = rotation_matrix*x_axis; 1944 1945 double t = gradient[i]/gradient*x_axis; 1946 1947 double sin_theta = sign(gradient[i])*t/sqrt(1+pow(t,2)); 1948 double cos_theta = sign(gradient*x_axis)/sqrt(1+pow(t,2)); 1927 1949 1928 1950 mat partial_rotation = eye(dimension); … … 1934 1956 partial_rotation.set(i,0,-sin_theta); 1935 1957 1936 rotation_matrix = rotation_matrix*partial_rotation; 1937 1938 squared_length += pow(gradient[i],2); 1958 rotation_matrix = rotation_matrix*partial_rotation; 1959 1939 1960 } 1940 1961 1941 1962 // cout << rotation_matrix << endl; 1942 // cout<< gradient << endl;1963 cout << "Gradient after trafo:" << gradient << endl; 1943 1964 1944 1965 vec minima = numeric_limits<double>::max()*ones(number_of_parameters); … … 1947 1968 vertex* min_grad; 1948 1969 vertex* max_grad; 1949 1970 1950 1971 for(set<vertex*>::iterator vert_ref = (*s_ref)->vertices.begin();vert_ref!=(*s_ref)->vertices.end();vert_ref++) 1951 1972 { … … 1978 1999 } 1979 2000 1980 vec sample_coordinates; 2001 vec sample_coordinates; 1981 2002 1982 2003 for(int j = 0;j<number_of_parameters;j++) … … 1990 2011 double pos_value = sampled_toprow->condition_sum.right(sampled_toprow->condition_sum.size()-1)*max_grad->get_coordinates()-sampled_toprow->condition_sum[0]; 1991 2012 2013 vec new_gradient = rotation_matrix*gradient; 2014 2015 cout << "New gradient(should have only first component nonzero):" << new_gradient << endl; 2016 2017 cout << "Max: " << maxima[0] << " Min: " << minima[0] << " Grad:" << new_gradient[0] << endl; 1992 2018 1993 //coordinate = log(rnumber*(exp(sampled_toprow->condition_sum.size()-1)*max_grad->get_coordinates()-sampled_toprow->condition_sum[0])-exp(sampled_toprow->condition_sum.right(sampled_toprow->condition_sum.size()-1)*min_grad->get_coordinates()-sampled_toprow->condition_sum[0]))+ 2019 double log_bracket = rnumber*exp(new_gradient[0]*maxima[0]/sigma)+(1-rnumber)*exp(new_gradient[0]*minima[0]/sigma); 2020 2021 coordinate = log(log_bracket); 1994 2022 } 1995 2023 }